[vbox-dev] SDK and VHD

Huihong Luo huisinro at yahoo.com
Tue Sep 27 12:35:14 PDT 2011

you need a mutable machine instance
I will post some of my code soon

--- On Tue, 9/27/11, Emad Steitieh <emadns at yahoo.com> wrote:

From: Emad Steitieh <emadns at yahoo.com>
Subject: [vbox-dev] SDK and VHD
To: "vbox-dev at virtualbox.org" <vbox-dev at virtualbox.org>
Date: Tuesday, September 27, 2011, 12:22 PM

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)
 IMachine *machine = NULL;
 IVirtualBox *virtualBox = 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 */
 BSTR guid = NULL;
 BSTR strMachineName = ::SysAllocString(L"SampleMachine");
 IGuestOSType* os = NULL;
 BSTR type = ::SysAllocString(L"unknown");
 hr = virtualBox->GetGuestOSType(type, &os);
 BSTR null = ::SysAllocString(L"00000000-0000-0000-0000-000000000000");
 hr = virtualBox->CreateMachine(NULL, machineName, guid, null, TRUE, &machine);
 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);
 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 */
 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. */
 /* Bring console window to front. */

-----Inline Attachment Follows-----

vbox-dev mailing list
vbox-dev at www.virtualbox.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.virtualbox.org/pipermail/vbox-dev/attachments/20110927/d9be9135/attachment-0001.html 

More information about the vbox-dev mailing list