<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div>Hello Experts,</div><div>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:</div><div>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?</div><div><br>void LaunchVHD(BSTR bstrPath)<br>{<br> HRESULT hr;</div><div> IMachine *machine = NULL;<br> IVirtualBox *virtualBox = NULL;</div><div> CoInitialize(NULL);</div><div> /* Instantiate the VirtualBox root object. */<br> hr = CoCreateInstance(CLSID_VirtualBox, /* the VirtualBox base object
 */<br> NULL, /* no aggregation */<br> CLSCTX_LOCAL_SERVER, /* the object lives in a server process on this machine */<br> IID_IVirtualBox, /* IID of the interface */<br> (void**)&virtualBox);</div><div> BSTR guid = NULL;<br> BSTR strMachineName = ::SysAllocString(L"SampleMachine");<br> IGuestOSType* os = NULL;<br> BSTR type = ::SysAllocString(L"unknown");<br> hr = virtualBox->GetGuestOSType(type, &os);<br> os->get_Id(&guid);<br> os->Release();<br> BSTR null = ::SysAllocString(L"00000000-0000-0000-0000-000000000000");<br> hr = virtualBox->CreateMachine(NULL, machineName, guid, null, TRUE, &machine);<br> ::SysFreeString(null);<br> if (SUCCEEDED(hr))<br> {<br> IMedium* medium = NULL;<br> ISession *session = NULL;<br> IConsole *console = NULL;<br> IProgress *progress = NULL;<br> BSTR sessiontype =
 SysAllocString(L"gui");<br> hr = machine->SaveSettings();<br> hr = virtualBox->RegisterMachine(machine);<br> machine->Release();<br> hr = virtualBox->FindMachine(machineName, &machine);<br> hr = virtualBox->OpenMedium(bstrPath, DeviceType_HardDisk, AccessMode_ReadWrite,<br> TRUE, &medium);<br> /* Create the session object. */<br> hr = CoCreateInstance(CLSID_Session, /* the VirtualBox base object */<br> NULL, /* no aggregation */<br> CLSCTX_INPROC_SERVER, /* the object lives in a server process on this machine */<br> IID_ISession, /* IID of the interface */<br> (void**)&session);<br> hr = machine->LockMachine(session, LockType_Write);<br> IStorageController* cont = NULL;<br> BSTR loc = ::SysAllocString(L"BusLogic");<br> hr = machine->AddStorageController(loc, StorageBus_SCSI, &cont);<br> hr = machine->AttachDevice(loc, 0, 0,
 DeviceType_HardDisk, medium);</div><div><br> /* Start a VM session using the delivered VBox GUI. */<br> hr = machine->LaunchVMProcess(session, sessiontype,<br> NULL, &progress);<br> /* Wait until VM is running. */<br> hr = progress->WaitForCompletion (-1);<br> /* Get console object. */<br> session->get_Console(&console);<br> /* Bring console window to front. */<br> machine->ShowConsoleWindow(0);<br> }<br>}<br></div></div></body></html>