SDK w/ VHD files

Discussions related to using the OSE version of VirtualBox.
Post Reply
emadns
Posts: 4
Joined: 26. Sep 2011, 17:56
Primary OS: MS Windows 7
VBox Version: OSE self-compiled
Guest OSses: Windows 7

SDK w/ VHD files

Post by emadns »

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);
}
}
emadns
Posts: 4
Joined: 26. Sep 2011, 17:56
Primary OS: MS Windows 7
VBox Version: OSE self-compiled
Guest OSses: Windows 7

Re: SDK w/ VHD files

Post by emadns »

I was helped through the mailing list and the fix was that I need to call get_Machine of ISession object and then call AddDiskController and AttachDevice on that machine object
saji
Posts: 4
Joined: 21. Feb 2012, 15:19

Re: SDK w/ VHD files

Post by saji »

Hi,

Thanks for Sharing the information.
Post Reply