[vbox-dev] SDK and VHD

Huihong Luo huisinro at yahoo.com
Tue Sep 27 12:37:39 PDT 2011

Please refer to the following code to get clues
/* about disk number
If you have an IDE HD and a SATA HD, the IDE will be disk 0 and the
SATA will be disk 1. Add another IDE HD and the IDEs will be
disks 0 & 1 and the SATA disk will be disk 2. This is just how the
disks are ordered in hardware, and has no bearing on the operation
of XP. Drives will boot in the order designated in the BIOS. The
disk the boot.ini file is located on will be disk 0 for XP booting.
// configure vm with default settings
HRESULT ConfigureVM(
IN IVirtualBox *pVbox, 
IN ISession* session,
IN IMachine* machine,
IN ULONG numHarddisks,
IN IMedium** hds,
IN ULONG ramSize,
IN BOOL ioapic,
IN PWSTR guestOsType,
IN PWSTR description 
BSTR uuid; 
BSTR disk_uuid;
IMachine *pMutableMachine = NULL;
IBIOSSettings *biosSettings = NULL;
if (ramSize == 0)
ramSize = GetMemorySize()/3; // /2; // 50% seems to be too high, vm issues errors sometimes

do { // use a while loop so we can break out of loop in case of error
WCHAR descriptionStr[1024];
rc = machine->get_Id(&uuid);
rc = machine->LockMachine (session, LockType_Shared);
/* get the mutable session machine */
rc = session->get_Machine(&pMutableMachine);
if (description == NULL)
// add some description, os info and date
struct tm *local_time; 
__time64_t raw_time;
_time64( &raw_time ); /* Get time as long integer. */
local_time = _localtime64( &raw_time ); /* Convert to local time. */

wcscpy(descriptionStr, L"VMLite virtual machine created by VMLite Workstation.\n");
swprintf(descriptionStr + wcslen(descriptionStr), L"Date: %s\n", _wasctime(local_time));
TCHAR szOS[256];
if( GetHostOSDisplayString( szOS, sizeof(szOS) ) ) { 
swprintf(descriptionStr + wcslen(descriptionStr), L"Host OS: %s\n", szOS ); 
description = descriptionStr;
rc = pMutableMachine->put_Description(BStr(description));

/* Create default storage controllers */
BStr ideCtrName (L"IDE Controller");
BStr floppyCtrName (L"Floppy Controller");

ComPtr<IStorageController> controller = NULL;
rc = pMutableMachine->AddStorageController (ideCtrName, StorageBus_IDE, controller.asOutParam());
rc = pMutableMachine->AddStorageController (floppyCtrName, StorageBus_Floppy, controller.asOutParam());
LONG channel, device;
for (ULONG i=0; i<numHarddisks; i++)
IMedium* hd = hds[i];
//rc = hd->get_Id(&disk_uuid);

if (i == 0)
// system disk must be the first one
channel = 0;
device = 0;
else if (i == 1)
channel = 0;
device = 1;
else if (i == 2)
channel = 1;
device = 1;
//rc = pMutableMachine->AttachHardDisk(disk_uuid, BStr(L"IDE"), channel, device);
rc = pMutableMachine->AttachDevice(ideCtrName, channel, device, DeviceType_HardDisk, hd);
if (i < 3) // IDE allows only 3 disks
// add an empty floppy
rc = pMutableMachine->AttachDevice(floppyCtrName, 0, 0, DeviceType_Floppy, NULL); 
rc = ConfigureDVD(pVbox, pMutableMachine);
rc = pMutableMachine->get_BIOSSettings(&biosSettings);
BStr osTypeId(guestOsType);
// To learn about the various identifiers
// that can be used here, use VBoxManage list ostypes. see above
//rc = pMutableMachine->put_OSTypeId(osTypeId);
rc = pMutableMachine->put_MemorySize(ramSize);
ComPtr<IGuestOSType> osType;
rc = pVbox->GetGuestOSType (osTypeId, osType.asOutParam());
ULONG vram;
rc = osType->get_RecommendedVRAM(&vram);
rc = pMutableMachine->put_VRAMSize(vram);
rc = biosSettings->put_ACPIEnabled(TRUE);

rc = biosSettings->put_IOAPICEnabled(ioapic);
if (biosSettings)
/* no need to do it, vbox will handle it automatically
rc = ConfigureNetwork(pMutableMachine);
// put least important at end
// configure audio
rc = ConfigureAudio(pMutableMachine);
//CHECK_ERROR_BREAK(rc); // continue even if no sound

InsertBridgedNetworkAdapters(pVbox, pMutableMachine);
/* commit changes */
rc = pMutableMachine->SaveSettings();
} while (0);
return rc;

--- On Tue, 9/27/11, Huihong Luo <huisinro at yahoo.com> wrote:

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

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/c69b50af/attachment-0001.html 

More information about the vbox-dev mailing list