Using LaunchVMProcess() to start a VM, Segmentation fault

Discussion about using the VirtualBox API, Tutorials, Samples.

Using LaunchVMProcess() to start a VM, Segmentation fault

Postby wuzf » 27. Jun 2013, 09:58

I want to start a VM use LaunchVMProcess(),it failed, error: Segmentation fault.
host system:ubuntu12.10
language:c++
code:
Code: Select all   Expand viewCollapse view
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

Postby wuzf » 27. Jun 2013, 10:58

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   Expand viewCollapse view
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

Postby wuzf » 28. Jun 2013, 05:48

I get machine state is 4(aborted), and seccion state is 2(locked).
wuzf
 
Posts: 15
Joined: 30. May 2013, 10:57

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

Postby noteirak » 29. Jun 2013, 09:51

Code: Select all   Expand viewCollapse view
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://kamax.io/hbox/
Manage your VirtualBox infrastructure the free way!
noteirak
Site Moderator
 
Posts: 5198
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7


Return to The VirtualBox API

Who is online

Users browsing this forum: No registered users and 2 guests