<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;"><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=yiv1656608970>
<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=plainMail>_______________________________________________<BR>vbox-dev mailing list<BR><A href="http://us.mc1603.mail.yahoo.com/mc/compose?to=vbox-dev@www.virtualbox.org" ymailto="mailto:vbox-dev@www.virtualbox.org">vbox-dev@www.virtualbox.org</A><BR><A href="https://www.virtualbox.org/mailman/listinfo/vbox-dev" target=_blank>https://www.virtualbox.org/mailman/listinfo/vbox-dev</A><BR></DIV></BLOCKQUOTE></td></tr></table>