VirtualBox

Ticket #12051: Support.cpp

File Support.cpp, 2.3 KB (added by MVS, 11 years ago)

C++ code to create VM in virtual box

Line 
1// Support.cpp : Defines the entry point for the console application.
2//
3
4#include "stdafx.h"
5#include <iostream>
6#include <string>
7#include "VirtualBox.h"
8
9using namespace std;
10
11BOOL createVM(IVirtualBox *virtualBox)
12{
13 IMachine *iMachine = NULL; // GuestMachine Object
14 HRESULT rc; //result
15
16 /* Initialize the COM subsystem. */
17 CoInitialize(NULL);
18
19 /* Instantiate the VirtualBox root object. */
20 rc = CoCreateInstance(CLSID_VirtualBox, /* the VirtualBox base object */
21 NULL, /* no aggregation */
22 CLSCTX_LOCAL_SERVER, /* the object lives in a server process on this machine */
23 IID_IVirtualBox, /* IID of the interface */
24 (void**)&virtualBox);
25
26 if (!SUCCEEDED(rc))
27 {
28 printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
29 }
30
31 //BSTR settingsFileName = SysAllocString(L"H:\\VitualVHD's\\Xp32\\xp32.vhd");
32 BSTR m_MachineName = SysAllocString(L"Sample");
33 BSTR machineName = ::SysAllocString(m_MachineName);
34 BSTR m_OSName = SysAllocString(L"Windows7");
35 BSTR type = ::SysAllocString(m_OSName);
36
37 IGuestOSType* os = NULL;
38 rc = virtualBox->GetGuestOSType(type, &os);
39 BSTR guid = NULL;
40 int abc = os->get_Id(&guid);
41 rc = S_FALSE;
42 rc = virtualBox->CreateMachine(NULL,machineName,NULL,guid,NULL,&iMachine);
43 return TRUE;
44}
45
46int main(int argc, char *argv[])
47{
48 HRESULT rc;
49 IVirtualBox *virtualBox;
50
51 wstring machineName = L"Mohan_XP";
52 wstring machineOS = L"XP";
53 ULONG machineRAM = 512;
54 wstring MachineVhdToUse = L"H:\\Disk2_full.vmdk";
55 wstring MachineChipSet = L"PIIX3";
56 BOOL MachineIOapic = TRUE;
57
58 do
59 {
60 /* Initialize the COM subsystem. */
61 CoInitialize(NULL);
62
63 /* Instantiate the VirtualBox root object. */
64 rc = CoCreateInstance(CLSID_VirtualBox, /* the VirtualBox base object */
65 NULL, /* no aggregation */
66 CLSCTX_LOCAL_SERVER, /* the object lives in a server process on this machine */
67 IID_IVirtualBox, /* IID of the interface */
68 (void**)&virtualBox);
69
70 if (!SUCCEEDED(rc))
71 {
72 printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
73 break;
74 }
75
76 createVM(virtualBox);
77
78 /* Release the VirtualBox object. */
79 virtualBox->Release();
80
81 } while (0);
82
83 CoUninitialize();
84 return 0;
85}

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy