<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;"><DIV>Please refer to the following code to get clues</DIV>
<DIV> </DIV><FONT color=#008000 size=2><FONT color=#008000 size=2>
<DIV>/* about disk number</DIV>
<DIV>If you have an IDE HD and a SATA HD, the IDE will be disk 0 and the</DIV>
<DIV>SATA will be disk 1. Add another IDE HD and the IDEs will be</DIV>
<DIV>disks 0 & 1 and the SATA disk will be disk 2. This is just how the</DIV>
<DIV>disks are ordered in hardware, and has no bearing on the operation</DIV>
<DIV>of XP. Drives will boot in the order designated in the BIOS. The</DIV>
<DIV>disk the boot.ini file is located on will be disk 0 for XP booting.</DIV>
<DIV>*/</DIV>
<DIV>// configure vm with default settings</DIV></FONT></FONT><FONT size=2>
<DIV>HRESULT ConfigureVM(</DIV>
<DIV>IN IVirtualBox *pVbox, </DIV>
<DIV>IN ISession* session,</DIV>
<DIV>IN IMachine* machine,</DIV>
<DIV>IN ULONG numHarddisks,</DIV>
<DIV>IN IMedium** hds,</DIV>
<DIV>IN ULONG ramSize,</DIV>
<DIV>IN BOOL ioapic,</DIV>
<DIV>IN PWSTR guestOsType,</DIV>
<DIV>IN PWSTR description </DIV>
<DIV>)</DIV>
<DIV>{</DIV>
<DIV>HRESULT rc;</DIV>
<DIV>BSTR uuid; </DIV>
<DIV>BSTR disk_uuid;</DIV>
<DIV>IMachine *pMutableMachine = NULL;</DIV>
<DIV>IBIOSSettings *biosSettings = NULL;</DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (ramSize == 0)</DIV>
<DIV>ramSize = GetMemorySize()/3; </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// /2; // 50% seems to be too high, vm issues errors sometimes</DIV></FONT></FONT><FONT size=2>
<DIV></DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>do</FONT></FONT><FONT size=2> { </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// use a while loop so we can break out of loop in case of error</DIV></FONT></FONT><FONT size=2>
<DIV>WCHAR descriptionStr[1024];</DIV>
<DIV>rc = machine->get_Id(&uuid);</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV>rc = machine->LockMachine (session, LockType_Shared);</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>/* get the mutable session machine */</DIV></FONT></FONT><FONT size=2>
<DIV>rc = session->get_Machine(&pMutableMachine);</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (description == NULL)</DIV>
<DIV>{</DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// add some description, os info and date</DIV></FONT></FONT><FONT size=2>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>struct</FONT></FONT><FONT size=2> tm *local_time; </DIV>
<DIV>__time64_t raw_time;</DIV>
<DIV>_time64( &raw_time ); </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>/* Get time as long integer. */</DIV></FONT></FONT><FONT size=2>
<DIV>local_time = _localtime64( &raw_time ); </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>/* Convert to local time. */</DIV></FONT></FONT><FONT size=2>
<DIV></DIV>
<DIV>wcscpy(descriptionStr, L</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"VMLite virtual machine created by VMLite Workstation.\n"</FONT></FONT><FONT size=2>);</DIV>
<DIV>swprintf(descriptionStr + wcslen(descriptionStr), L</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Date: %s\n"</FONT></FONT><FONT size=2>, _wasctime(local_time));</DIV>
<DIV>TCHAR szOS[256];</DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2>( GetHostOSDisplayString( szOS, </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>sizeof</FONT></FONT><FONT size=2>(szOS) ) ) { </DIV>
<DIV>swprintf(descriptionStr + wcslen(descriptionStr), L</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Host OS: %s\n"</FONT></FONT><FONT size=2>, szOS ); </DIV>
<DIV>}</DIV>
<DIV>description = descriptionStr;</DIV>
<DIV>}</DIV>
<DIV>rc = pMutableMachine->put_Description(BStr(description));</DIV>
<DIV></DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>/* Create default storage controllers */</DIV></FONT></FONT><FONT size=2>
<DIV>BStr ideCtrName (L</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"IDE Controller"</FONT></FONT><FONT size=2>);</DIV>
<DIV>BStr floppyCtrName (L</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Floppy Controller"</FONT></FONT><FONT size=2>);</DIV>
<DIV></DIV>
<DIV>ComPtr<IStorageController> controller = NULL;</DIV>
<DIV>rc = pMutableMachine->AddStorageController (ideCtrName, StorageBus_IDE, controller.asOutParam());</DIV>
<DIV>rc = pMutableMachine->AddStorageController (floppyCtrName, StorageBus_Floppy, controller.asOutParam());</DIV>
<DIV>LONG channel, device;</DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>for</FONT></FONT><FONT size=2> (ULONG i=0; i<numHarddisks; i++)</DIV>
<DIV>{</DIV>
<DIV>IMedium* hd = hds[i];</DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//rc = hd->get_Id(&disk_uuid);</DIV></FONT></FONT><FONT size=2>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//CHECK_ERROR_BREAK(rc);</DIV></FONT></FONT><FONT size=2>
<DIV></DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (i == 0)</DIV>
<DIV>{</DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// system disk must be the first one</DIV></FONT></FONT><FONT size=2>
<DIV>channel = 0;</DIV>
<DIV>device = 0;</DIV>
<DIV>}</DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>else</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (i == 1)</DIV>
<DIV>{</DIV>
<DIV>channel = 0;</DIV>
<DIV>device = 1;</DIV>
<DIV>}</DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>else</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (i == 2)</DIV>
<DIV>{</DIV>
<DIV>channel = 1;</DIV>
<DIV>device = 1;</DIV>
<DIV>}</DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//rc = pMutableMachine->AttachHardDisk(disk_uuid, BStr(L"IDE"), channel, device);</DIV></FONT></FONT><FONT size=2>
<DIV>rc = pMutableMachine->AttachDevice(ideCtrName, channel, device, DeviceType_HardDisk, hd);</DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (i < 3) </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// IDE allows only 3 disks</DIV></FONT></FONT><FONT size=2>
<DIV>{</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV>}</DIV>
<DIV>}</DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// add an empty floppy</DIV></FONT></FONT><FONT size=2>
<DIV>rc = pMutableMachine->AttachDevice(floppyCtrName, 0, 0, DeviceType_Floppy, NULL); </DIV>
<DIV>rc = ConfigureDVD(pVbox, pMutableMachine);</DIV>
<DIV>rc = pMutableMachine->get_BIOSSettings(&biosSettings);</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV>BStr osTypeId(guestOsType);</DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// To learn about the various identifiers</DIV></FONT></FONT><FONT size=2>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// that can be used here, use VBoxManage list ostypes. see above</DIV></FONT></FONT><FONT size=2>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//rc = pMutableMachine->put_OSTypeId(osTypeId);</DIV></FONT></FONT><FONT size=2>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//CHECK_ERROR_BREAK(rc);</DIV></FONT></FONT><FONT size=2>
<DIV>rc = pMutableMachine->put_MemorySize(ramSize);</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV>ComPtr<IGuestOSType> osType;</DIV>
<DIV>rc = pVbox->GetGuestOSType (osTypeId, osType.asOutParam());</DIV>
<DIV>ULONG vram;</DIV>
<DIV>rc = osType->get_RecommendedVRAM(&vram);</DIV>
<DIV>rc = pMutableMachine->put_VRAMSize(vram);</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV>rc = biosSettings->put_ACPIEnabled(TRUE);</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV></DIV>
<DIV>rc = biosSettings->put_IOAPICEnabled(ioapic);</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (biosSettings)</DIV>
<DIV>biosSettings->Release(); </DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>/* no need to do it, vbox will handle it automatically</DIV>
<DIV>rc = ConfigureNetwork(pMutableMachine);</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV>*/</DIV></FONT></FONT><FONT size=2>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// put least important at end</DIV></FONT></FONT><FONT size=2>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>// configure audio</DIV></FONT></FONT><FONT size=2>
<DIV>rc = ConfigureAudio(pMutableMachine);</DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//CHECK_ERROR_BREAK(rc); // continue even if no sound</DIV></FONT></FONT><FONT size=2>
<DIV></DIV>
<DIV>InsertBridgedNetworkAdapters(pVbox, pMutableMachine);</DIV>
<DIV></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>/* commit changes */</DIV></FONT></FONT><FONT size=2>
<DIV>rc = pMutableMachine->SaveSettings();</DIV>
<DIV>CHECK_ERROR_BREAK(rc);</DIV>
<DIV>session->UnlockMachine();</DIV>
<DIV>pMutableMachine->Release(); </DIV>
<DIV>} </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>while</FONT></FONT><FONT size=2> (0);</DIV>
<DIV></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>return</FONT></FONT><FONT size=2> rc;</DIV>
<DIV>}</DIV>
<DIV></FONT><BR><BR>--- On <B>Tue, 9/27/11, Huihong Luo <I><huisinro@yahoo.com></I></B> wrote:<BR></DIV>
<BLOCKQUOTE style="BORDER-LEFT: rgb(16,16,255) 2px solid; PADDING-LEFT: 5px; MARGIN-LEFT: 5px"><BR>From: Huihong Luo <huisinro@yahoo.com><BR>Subject: Re: [vbox-dev] SDK and VHD<BR>To: "vbox-dev@virtualbox.org" <vbox-dev@virtualbox.org>, "Emad Steitieh" <emadns@yahoo.com><BR>Date: Tuesday, September 27, 2011, 12:35 PM<BR><BR>
<DIV id=yiv801353074>
<TABLE border=0 cellSpacing=0 cellPadding=0>
<TBODY>
<TR>
<TD vAlign=top>
<DIV>you need a mutable machine instance</DIV>
<DIV> </DIV>
<DIV>I will post some of my code soon<BR><BR>--- On <B>Tue, 9/27/11, Emad Steitieh <I><emadns@yahoo.com></I></B> wrote:<BR></DIV>
<BLOCKQUOTE style="BORDER-LEFT: rgb(16,16,255) 2px solid; PADDING-LEFT: 5px; MARGIN-LEFT: 5px"><BR>From: Emad Steitieh <emadns@yahoo.com><BR>Subject: [vbox-dev] SDK and VHD<BR>To: "vbox-dev@virtualbox.org" <vbox-dev@virtualbox.org><BR>Date: Tuesday, September 27, 2011, 12:22 PM<BR><BR>
<DIV id=yiv801353074>
<DIV style="BACKGROUND-COLOR: #fff; FONT-FAMILY: times new roman, new york, times, serif; COLOR: #000; 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></DIV><BR>-----Inline Attachment Follows-----<BR><BR>
<DIV class=yiv801353074plainMail>_______________________________________________<BR>vbox-dev mailing list<BR><A href="http://us.mc1603.mail.yahoo.com/mc/compose?to=vbox-dev@www.virtualbox.org" rel=nofollow target=_blank>vbox-dev@www.virtualbox.org</A><BR><A href="https://www.virtualbox.org/mailman/listinfo/vbox-dev" rel=nofollow target=_blank>https://www.virtualbox.org/mailman/listinfo/vbox-dev</A><BR></DIV></BLOCKQUOTE></TD></TR></TBODY></TABLE></DIV></BLOCKQUOTE></td></tr></table>