2

由于我无法在官方的 VirtualBox 板上获得帮助,所以我想问一下你们中是否有人知道如何处理 VirtualBox API。 以下问题仅出现在我的 2 台 Win 8 x64 计算机上,但不在我的 Win 7 x86 笔记本电脑上!我不知道为什么,但正如维基百科所说,微软在 Win 8 中的 COM 发生了很大变化。在 Win 7 上启动可执行文件一切正常!

基本通信工作正常,我可以检索所有可用的虚拟机,因此 Com 连接至少部分工作。然而,一旦我想启动一个虚拟机,我就会得到我不明白的奇怪错误。这是输出:

控制台输出

我正在使用带有 sdk 的 VirtualBox 4.2,这是源代码(主要改编自示例):

#include <stdio.h>
#include <tchar.h>

#include "VirtualBox.h"

#define SAFE_RELEASE(x) \
    if (x) { \
        x->Release(); \
        x = NULL; \
    }

int listVMs(IVirtualBox *virtualBox)
{
    HRESULT rc;

    /*
     * First we have to get a list of all registered VMs
     */
    SAFEARRAY *machinesArray = NULL;

    rc = virtualBox->get_Machines(&machinesArray);
    if (SUCCEEDED(rc))
    {
        IMachine **machines;
        rc = SafeArrayAccessData (machinesArray, (void **) &machines);
        if (SUCCEEDED(rc))
        {
            for (ULONG i = 0; i < machinesArray->rgsabound[0].cElements; ++i)
            {
                BSTR str;

                rc = machines[i]->get_Name(&str);
                if (SUCCEEDED(rc))
                {
                    printf("Available Machine: %S\n", str);
                    SysFreeString(str);
                }
            }

            SafeArrayUnaccessData (machinesArray);
        }

        SafeArrayDestroy (machinesArray);
    }

    return 0;
}

int testErrorInfo(IVirtualBox *virtualBox)
{
    HRESULT rc;

    /* Try to find a machine that doesn't exist */
    IMachine *machine = NULL;
    BSTR machineName = SysAllocString(L"Test Machine");

    rc = virtualBox->FindMachine(machineName, &machine);

    if (FAILED(rc))
    {
        IErrorInfo *errorInfo;

        rc = GetErrorInfo(0, &errorInfo);

        if (FAILED(rc))
            printf("Error getting error info! rc = 0x%x\n", rc);
        else
        {
            BSTR errorDescription = NULL;

            rc = errorInfo->GetDescription(&errorDescription);

            if (FAILED(rc) || !errorDescription)
                printf("Error getting error description! rc = 0x%x\n", rc);
            else
            {
                printf("Successfully retrieved error description: %S\n", errorDescription);

                SysFreeString(errorDescription);
            }

            errorInfo->Release();
        }
    }
    else
        printf("No Errors \n");

    SAFE_RELEASE(machine);
    SysFreeString(machineName);

    return 0;
}


int testStartVM(IVirtualBox *virtualBox, OLECHAR *MachineName)
{
    HRESULT rc;

    /* Try to start a VM*/
    IMachine *machine = NULL;
    BSTR machineName = SysAllocString(MachineName);

    rc = virtualBox->FindMachine(machineName, &machine);

    if (FAILED(rc))
    {
        IErrorInfo *errorInfo;

        rc = GetErrorInfo(0, &errorInfo);

        if (FAILED(rc))
            printf("Error getting error info! rc = 0x%x\n", rc);
        else
        {
            BSTR errorDescription = NULL;

            rc = errorInfo->GetDescription(&errorDescription);

            if (FAILED(rc) || !errorDescription)
                printf("Error getting error description! rc = 0x%x\n", rc);
            else
            {
                printf("Successfully retrieved error description: %S\n", errorDescription);

                SysFreeString(errorDescription);
            }

            SAFE_RELEASE(errorInfo);
        }
    }
    else
    {
        ISession *session = NULL;
        IConsole *console = NULL;
        IProgress *progress = NULL;
        BSTR sessiontype = SysAllocString(L"gui");
        BSTR guid;

        do
        {
            rc = machine->get_Id(&guid); /* Get the GUID of the machine. */
            if (!SUCCEEDED(rc))
            {
                printf("Error retrieving machine ID! rc = 0x%x\n", rc);
                break;
            }
            /* Create the session object. */
            testErrorInfo(virtualBox);
            printf("GetLastError : %d\n", GetLastError());
            rc = CoCreateInstance(CLSID_Session,        //the VirtualBox base object 
                                  NULL,                 //no aggregation 
                                  CLSCTX_INPROC_SERVER, //the object lives in a server process on this machine 
                                  IID_ISession,         //IID of the interface 
                                  (void**)&session);
            /*
            //
            //
            //Error in CoCreateInstance
            //
            //
            */
            if (!SUCCEEDED(rc))
            {
                printf("Error creating Session instance! rc = 0x%x\n", rc);
                printf("GetLastError : %d\n", GetLastError());
                break;
            }

            /* Start a VM session using the delivered VBox GUI. */
            rc = machine->LaunchVMProcess(session, sessiontype,
                                          NULL, &progress);
            if (!SUCCEEDED(rc))
            {
                printf("Could not open remote session! rc = 0x%x\n", rc);
                break;
            }

            /* Wait until VM is running. */
            printf ("Starting VM, please wait ...\n");
            rc = progress->WaitForCompletion (-1);

            /* Get console object. */
            session->get_Console(&console);

            /* Bring console window to front. */
            machine->ShowConsoleWindow(0);

            printf ("Press enter to power off VM and close the session...\n");
            getchar();

            /* Power down the machine. */
            rc = console->PowerDown(&progress);

            /* Wait until VM is powered down. */
            printf ("Powering off VM, please wait ...\n");
            rc = progress->WaitForCompletion (-1);

            /* Close the session. */
            rc = session->UnlockMachine();

        } while (0);

        SAFE_RELEASE(console);
        SAFE_RELEASE(progress);
        SAFE_RELEASE(session);
        SysFreeString(guid);
        SysFreeString(sessiontype);
        SAFE_RELEASE(machine);
    }

    SysFreeString(machineName);

    return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL);

    IVirtualBox *virtualBox;
    HRESULT rc = CoCreateInstance(CLSID_VirtualBox,
                          NULL,
                          CLSCTX_LOCAL_SERVER,
                          IID_IVirtualBox,
                          (void**)&virtualBox);

    if (!SUCCEEDED(rc))
    {
        printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
        getchar();
        return 0;
    }
    else
    {
        printf("Successfully created VirtualBox instance \n");
        testErrorInfo(virtualBox);
        listVMs(virtualBox);
    }

    testStartVM(virtualBox, L"Full Tilt");

    virtualBox->Release();

    CoUninitialize();

    getchar();
    return 0;
}

这意味着它在调用时崩溃

    rc = CoCreateInstance(CLSID_Session,        //the VirtualBox base object 
                          NULL,                 //no aggregation 
                          CLSCTX_INPROC_SERVER, //the object lives in a server process on this machine 
                          IID_ISession,         //IID of the interface 
                          (void**)&session);

它抛出;

0x80040154 aka REGDB_E_CLASSNOTREG

GetLastError 返回:

ERROR_SXS_KEY_NOT_FOUND
14007 (0x36B7)
The requested lookup key was not found in any active activation context.

我尝试了很多东西,但无法解决问题。任何形式的帮助都非常感谢:)

谢谢!

4

0 回答 0