2012-01-10 152 views
1

我需要使用VB SDK中的API在虛擬框中創建虛擬機。我能夠創建機器,但爲虛擬機創建控制器和添加設備讓我感到困擾。你能幫我解決嗎?我已經給出了我正在使用的代碼使用VB SDK在虛擬框中創建虛擬機

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);。我做錯了什麼?

+0

是HRESULT返回什麼? – 2012-01-10 12:52:24

+0

我幫不了你,但我認爲programmaticaly產卵本地虛擬機只是真棒;-)程序員的終極錘子;)祝你好運! – Offirmo 2012-01-10 16:00:01

+0

嗨@RowlandShaw,我得到的返回值是-2135228414 – user1071321 2012-01-13 05:52:24

回答

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