host system:ubuntu12.10
language:c++
code:
Code: Select all
void StartVM(IVirtualBox* virtualBox, const char* machineID)
{
if(virtualBox == NULL || machineID == NULL)
{
return;
}
NS_ConvertASCIItoUTF16 machineIDutf16(machineID);
nsresult rc;
//first get need to start machine
IMachine* machine = NULL;
rc = virtualBox->FindMachine(ToNewUnicode(machineIDutf16), &machine);
if(NS_FAILED(rc) || machine == NULL)
{
printf("Error: Failed to find VM.\n");
return;
}
nsCOMPtr<ISession> session;
{
nsCOMPtr<nsIComponentManager> manager;
rc = NS_GetComponentManager(getter_AddRefs(manager));
if(NS_FAILED(rc))
{
printf("Error: could not get component manager! rc=%08X\n", rc);
return;
}
rc = manager->CreateInstanceByContractID(NS_SESSION_CONTRACTID,
nsnull,
NS_GET_IID(ISession),
getter_AddRefs(session));
if(NS_FAILED(rc))
{
printf("Error, could not instantiate session object! rc=0x%x\n", rc);
return;
}
rc = machine->LockMachine(session, LockType_Write);
if(NS_FAILED(rc))
{
printf("Error, could not lock the machine for the session! rc=0x%x\n", rc);
return;
}
rc = session->GetMachine(&machine);
if (NS_FAILED(rc))
{
printf("Error, could not get machine session! rc=0x%x\n", rc);
return;
}
nsCOMPtr <IProgress> progress;
NS_ConvertASCIItoUTF16 type("gui");
printf("begin to launch vm\n");
rc = machine->LaunchVMProcess(session, ToNewUnicode(type), NULL, getter_AddRefs(progress));
if(NS_FAILED(rc))
{
printf("Error: Could not open session, rc=%08x\n", rc);
}
printf("Starting VM, please waitting...\n");
rc = progress->WaitForCompletion(-1);
PRInt32 resultCode;
progress->GetResultCode(&resultCode);
if(NS_FAILED(rc) || NS_FAILED(resultCode))
{
printf("Error: failed to launch vm, rc=%08x\n", NS_FAILED(rc)? rc : resultCode);
}
session->UnlockMachine();
}
}
env VBOX_XPCOM_HOME=/home/VirtualBox/ LD_LIBRARY_PATH=/home/VirtualBox/ ./MyVBoxManager
when it run to rc = progress->WaitForCompletion(-1); Segmentation fault
I what wrong? and Who can give me some advice.
Thanks!