我正在尝试使用 VirtualBox SDK 以编程方式创建虚拟机。我正在使用来自 VirtualBox SDK for Windows 7 的 mscom 示例来执行此操作,但我一直在创建硬盘。下面采样的 CreateHardDisk 方法返回错误。从提供的文档中,我无法理解如何执行此操作。
bool createMachine(IVirtualBox *virtualBox)
{
HRESULT rc;
bool retVal = false;
IMachine *machine = NULL;
BSTR machineName = SysAllocString(L"TestVM");
rc = virtualBox->FindMachine(machineName, &machine);
if (FAILED(rc))
{
//no machine found with this name so we create one
BSTR osTypeName = SysAllocString(L"Ubuntu");
rc = virtualBox->CreateMachine(NULL, machineName, NULL, osTypeName, NULL, &machine );
if(FAILED(rc))
{
printf("creation of test machine failed\n");
return false;
}
printf("created TestVM virtual machine\n");
//allocate 512 RAM
rc = machine->put_MemorySize(512);
if(FAILED(rc))
{
printf("put_MemorySize failed\n");
}
//create and allocate hdd
IMedium* medium = NULL;
BSTR format = SysAllocString(L"VDI");
BSTR location = SysAllocString(L"test.vdi");
//BSTR("VDI"), BSTR("TestHardDisk.vdi")
rc = virtualBox->CreateHardDisk(format, location, &medium);
if(FAILED(rc))//Here it returns false
{
//return false;
printf("CreateHardDisk failed\n");
}
//save the settings for the virtual machine
rc = machine->SaveSettings();
if(FAILED(rc))
{
return false;
printf("saveSettings failed\n");
}
//registers machine to the Pond server(Virtual Box)
rc = virtualBox->RegisterMachine(machine);
if(FAILED(rc))
{
return false;
printf("register machine failed\n");
}
SAFE_RELEASE(progress);
SysFreeString(osTypeName);
SysFreeString(format);
SysFreeString(location);
}
SysFreeString(machineName);
SAFE_RELEASE(machine);
return true;
}