[vbox-dev] SDK and VHD
Emad Steitieh
emadns at yahoo.com
Tue Sep 27 12:22:51 PDT 2011
Hello Experts,
I am trying to write a sample code to open a VHD file for a bootable drive and run it. I tried the vhd file inside VB and it is working fine. I wrote the following code:
I omitted the error handling code for simplicity. Basically the code fails at AddStorageController and AttachDevice functions. The parameter to the function is the path for a vhd file. I am trying to register the vhd file and then run it using the server. Why it is failing? What am I doing wrong?
void LaunchVHD(BSTR bstrPath)
{
HRESULT hr;
IMachine *machine = NULL;
IVirtualBox *virtualBox = NULL;
CoInitialize(NULL);
/* 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 guid = NULL;
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, machineName, 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");
hr = machine->SaveSettings();
hr = virtualBox->RegisterMachine(machine);
machine->Release();
hr = virtualBox->FindMachine(machineName, &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);
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.virtualbox.org/pipermail/vbox-dev/attachments/20110927/392d97a8/attachment-0001.html
More information about the vbox-dev
mailing list