C++ 使用VirtualBox SDK中的CreateHardDisk方法

C++ 使用VirtualBox SDK中的CreateHardDisk方法,c++,visual-studio-2010,visual-c++,virtualbox,C++,Visual Studio 2010,Visual C++,Virtualbox,我正在尝试使用VirtualBox SDK以编程方式创建虚拟机。我正在使用VirtualBox SDK for Windows 7中的mscom示例来实现这一点,我一直在创建硬盘。下面采样的CreateHardDisk方法返回错误。从提供的文档中,我无法理解如何做到这一点 bool createMachine(IVirtualBox *virtualBox) { HRESULT rc; bool retVal = false; IMachine *machine = NULL; BSTR ma

我正在尝试使用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;
}