使用VB SDK在虚拟框中创建VM

时间:2012-01-10 12:49:45

标签: c++

我需要使用VB SDK中的API在虚拟框中创建VM。我能够创建机器,但创建控制器和为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_SCSI, &cont);处失败。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以将AddStorageController添加到可变机器,从session.machine获取机器对象并在mutablemachine上添加

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);