Using LaunchVMProcess() to start a VM, Segmentation fault

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Using LaunchVMProcess() to start a VM, Segmentation fault

Post by wuzf »

I want to start a VM use LaunchVMProcess(),it failed, error: Segmentation fault.
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();
    }
}
I compile code and generated executable a file MyVBoxManager;
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!
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Re: Using LaunchVMProcess() to start a VM, Segmentation faul

Post by wuzf »

Sorrory, The above is wrong, please look this:
I want to start a VM by LaunchVMProcess() ,it failed.
host system:ubuntu
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))
    {
        printf("Error: Failed to get start VM. rc = %08x\n", rc);
        return;
    }
    else if(machine == NULL)
    {
        printf("Error: Get start VM isn NULL\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: failed to LaunchVMProcess, rc=%08x\n", rc);
        }
        else
        {
            printf("Starting VM, please waitting...\n");
            rc = progress->WaitForCompletion(-1);
            PRInt32 resultCode;
            progress->GetResultCode(&resultCode);
            if(NS_FAILED(resultCode))
            {
                printf("Error: failed to launch vm, rc=%08x\n", resultCode);
            }
        }
        session->UnlockMachine();
    }
}
Error: failed to LaunchVMProcess, rc=80bb0007 Current object state prohibits operation.

where i do wrong,and Who can give me some advice.
Thanks!
wuzf
Posts: 15
Joined: 30. May 2013, 10:57

Re: Using LaunchVMProcess() to start a VM, Segmentation faul

Post by wuzf »

I get machine state is 4(aborted), and seccion state is 2(locked).
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Using LaunchVMProcess() to start a VM, Segmentation faul

Post by noteirak »

Code: Select all

rc = machine->LockMachine(session, LockType_Write);
You must use LockType_Shared, since the process running the VM will get Write lock, as stated in the SDK
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply