[vbox-dev] SDK and VHD

Huihong Luo huisinro at yahoo.com
Tue Sep 27 19:37:39 GMT 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 
)
{
HRESULT rc;
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);
CHECK_ERROR_BREAK(rc);
rc = machine->LockMachine (session, LockType_Shared);
CHECK_ERROR_BREAK(rc);
/* get the mutable session machine */
rc = session->get_Machine(&pMutableMachine);
CHECK_ERROR_BREAK(rc);
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);
//CHECK_ERROR_BREAK(rc);

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
{
CHECK_ERROR_BREAK(rc);
}
}
// add an empty floppy
rc = pMutableMachine->AttachDevice(floppyCtrName, 0, 0, DeviceType_Floppy, NULL); 
rc = ConfigureDVD(pVbox, pMutableMachine);
rc = pMutableMachine->get_BIOSSettings(&biosSettings);
CHECK_ERROR_BREAK(rc);
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);
//CHECK_ERROR_BREAK(rc);
rc = pMutableMachine->put_MemorySize(ramSize);
CHECK_ERROR_BREAK(rc);
ComPtr<IGuestOSType> osType;
rc = pVbox->GetGuestOSType (osTypeId, osType.asOutParam());
ULONG vram;
rc = osType->get_RecommendedVRAM(&vram);
rc = pMutableMachine->put_VRAMSize(vram);
CHECK_ERROR_BREAK(rc);
rc = biosSettings->put_ACPIEnabled(TRUE);
CHECK_ERROR_BREAK(rc);

rc = biosSettings->put_IOAPICEnabled(ioapic);
CHECK_ERROR_BREAK(rc);
if (biosSettings)
biosSettings->Release(); 
/* no need to do it, vbox will handle it automatically
rc = ConfigureNetwork(pMutableMachine);
CHECK_ERROR_BREAK(rc);
*/
// 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();
CHECK_ERROR_BREAK(rc);
session->UnlockMachine();
pMutableMachine->Release(); 
} 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)
{
 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);
 }
}

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


_______________________________________________
vbox-dev mailing list
vbox-dev at www.virtualbox.org
https://www.virtualbox.org/mailman/listinfo/vbox-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.virtualbox.org/pipermail/vbox-dev/attachments/20110927/c69b50af/attachment.html>


More information about the vbox-dev mailing list