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);
。我做错了什么?
是HRESULT返回什么? – 2012-01-10 12:52:24
我帮不了你,但我认为programmaticaly产卵本地虚拟机只是真棒;-)程序员的终极锤子;)祝你好运! – Offirmo 2012-01-10 16:00:01
嗨@RowlandShaw,我得到的返回值是-2135228414 – user1071321 2012-01-13 05:52:24