C++ 使用VB SDK在虚拟盒中创建虚拟机

C++ 使用VB SDK在虚拟盒中创建虚拟机,c++,C++,我需要使用VB SDK中的API在虚拟盒中创建VM。我能够创建这台机器,但是为虚拟机创建控制器和添加设备让我非常烦恼。你能帮我一下吗?我已经给出了我正在使用的代码 CoInitialize(NULL); IMachine *machine = NULL; IVirtualBox *virtualBox = NULL; BSTR guid = NULL; HRESULT hr; /* Instantiate the VirtualBox root object. */ hr = CoCreat

我需要使用VB SDK中的API在虚拟盒中创建VM。我能够创建这台机器,但是为虚拟机创建控制器和添加设备让我非常烦恼。你能帮我一下吗?我已经给出了我正在使用的代码

CoInitialize(NULL);

IMachine *machine = NULL;
IVirtualBox *virtualBox = NULL;
BSTR guid = NULL;
HRESULT hr;

/* Instantiate the VirtualBox root object. */
hr = CoCreateInstance(CLSID_VirtualBox, /* the VirtualBox base object */
    NULL, /* no aggregation */
    CLSCTX_LOCAL_SERVER, /* the object lives in a server process on this machine */
    IID_IVirtualBox, /* IID of the interface */
    (void**)&virtualBox);
BSTR strMachineName = ::SysAllocString(L"SampleMachine");
IGuestOSType* os = NULL;
BSTR type = ::SysAllocString(L"unknown");
hr = virtualBox->GetGuestOSType(type, &os);
os->get_Id(&guid);
os->Release();
BSTR null = ::SysAllocString(L"00000000-0000-0000-0000-000000000000");
hr = virtualBox->CreateMachine(NULL, strMachineName, guid, null, TRUE, &machine);
::SysFreeString(null);

if (SUCCEEDED(hr))
{
    IMedium* medium = NULL;
    ISession *session = NULL;
    IConsole *console = NULL;
    IProgress *progress = NULL;
    BSTR sessiontype = SysAllocString(L"gui");
    BSTR bstrPath = SysAllocString(L"I:\\VHD\\Local_disk_(C).vhd");
    hr = machine->SaveSettings();
    hr = virtualBox->RegisterMachine(machine);
    machine->Release();
    hr = virtualBox->FindMachine(strMachineName, &machine);
    hr = virtualBox->OpenMedium(bstrPath, DeviceType_HardDisk, AccessMode_ReadWrite,
        TRUE, &medium);
    /* Create the session object. */
    hr = 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);
    hr = machine->LockMachine(session, LockType_Write);
    IStorageController* cont = NULL;
    BSTR loc = ::SysAllocString(L"BusLogic");
    hr = machine->AddStorageController(loc, StorageBus_SCSI, &cont);
    hr = machine->AttachDevice(loc, 0, 0, DeviceType_HardDisk, medium);
    /* Start a VM session using the delivered VBox GUI. */
    hr = machine->LaunchVMProcess(session, sessiontype,
        NULL, &progress);
    /* Wait until VM is running. */
    hr = progress->WaitForCompletion (-1);
    /* Get console object. */
    session->get_Console(&console);
    /* Bring console window to front. */
    machine->ShowConsoleWindow(0);

代码在hr=machine->AddStorageController(loc、StorageBus\u SCSI和cont)失败。我做错了什么?

您可以向可变机器添加AddStorageController,从session.machine获取机器对象,然后在可变机器上进行添加

hr = machine->LockMachine(session, LockType_Write);
    IStorageController* cont = NULL;
    BSTR loc = ::SysAllocString(L"BusLogic");
    hr = machine->AddStorageController(loc, StorageBus_SCSI, &cont);
改为

 hr = machine->LockMachine(session, LockType_Write);
    Imachine *mutableMachine = NULL;
    hr= session->get_Machine(&mutableMachine );
    IStorageController* cont = NULL;
    BSTR loc = ::SysAllocString(L"BusLogic");
    hr = mutableMachine ->AddStorageController(loc, StorageBus_SCSI, &cont);

HRESULT返回了什么?我帮不了你,但我认为programmaticaly生成本地VM真是太棒了;-)程序员的终极之锤;)祝你好运您好@RowlandShaw,我得到的返回值是-2135228414H,我能帮上忙吗。。提前谢谢。帮帮我