VirtualBox

Ticket #20915: simple_vbox_test.cpp

File simple_vbox_test.cpp, 6.3 KB (added by mbono, 3 years ago)

example program that reproduce the issue

Line 
1#include "VirtualBox.h"
2
3#include <chrono>
4#include <string>
5#include <thread>
6
7#define SAFE_RELEASE(x) \
8 if (x) \
9 { \
10 x->Release(); \
11 x = NULL; \
12 }
13
14void printErrorInfo()
15{
16 IErrorInfo *errorInfo;
17 HRESULT rc = GetErrorInfo(0, &errorInfo);
18 if (FAILED(rc))
19 {
20 printf("Failed to get error info! rc = 0x%x\n", rc);
21 return;
22 }
23
24 BSTR errorDescription = NULL;
25
26 rc = errorInfo->GetDescription(&errorDescription);
27
28 if (FAILED(rc) || !errorDescription)
29 {
30 printf("Error getting error description! rc = 0x%x\n", rc);
31 return;
32 }
33
34 printf("Successfully retrieved error description: %S\n", errorDescription);
35 SysFreeString(errorDescription);
36 SAFE_RELEASE(errorInfo);
37}
38
39int testStartVM(IVirtualBox *virtualBox, const wchar_t *name, const wchar_t *sessionTypeName)
40{
41 HRESULT rc;
42
43 /* Try to start a VM called "WinXP SP2". */
44 IMachine *machine = NULL;
45 BSTR machineName = SysAllocString(name);
46
47 rc = virtualBox->FindMachine(machineName, &machine);
48
49 if (FAILED(rc))
50 {
51 printErrorInfo();
52 }
53 else
54 {
55 ISession *session = NULL;
56 IConsole *console = NULL;
57 IProgress *progress = NULL;
58 BSTR sessiontype = SysAllocString(sessionTypeName);
59 BSTR guid;
60
61 do
62 {
63 rc = machine->get_Id(&guid); /* Get the GUID of the machine. */
64 if (!SUCCEEDED(rc))
65 {
66 printf("Error retrieving machine ID! rc = 0x%x\n", rc);
67 printErrorInfo();
68 break;
69 }
70
71 /* Create the session object. */
72 rc = CoCreateInstance(CLSID_Session, /* the VirtualBox base object */
73 NULL, /* no aggregation */
74 CLSCTX_INPROC_SERVER, /* the object lives in the current process */
75 IID_ISession, /* IID of the interface */
76 (void **)&session);
77 if (!SUCCEEDED(rc))
78 {
79 printf("Error creating Session instance! rc = 0x%x\n", rc);
80 printErrorInfo();
81 break;
82 }
83
84 /* Start a VM session using the delivered VBox GUI. */
85 rc = machine->LaunchVMProcess(session, sessiontype, NULL, &progress);
86 if (!SUCCEEDED(rc))
87 {
88 printf("Could not open remote session! rc = 0x%x\n", rc);
89 printErrorInfo();
90 break;
91 }
92
93 /* Wait until VM is running. */
94 printf("Starting VM, please wait ...\n");
95 rc = progress->WaitForCompletion(-1);
96 if (!SUCCEEDED(rc))
97 {
98 printf("Progress wait for LaunchVMProcess completion fail! rc = 0x%x\n", rc);
99 printErrorInfo();
100 break;
101 }
102
103 /* Get console object. */
104 session->get_Console(&console);
105
106 /* Bring console window to front. */
107 machine->ShowConsoleWindow(0);
108
109 while (true)
110 {
111 MachineState state;
112 rc = machine->get_State(&state);
113 if (FAILED(rc))
114 {
115 printf("Faile to get machine state! rc = 0x%x\n", rc);
116 printErrorInfo();
117 break;
118 }
119
120 printf("Machine state == %d\n", state);
121 if (state == MachineState_Running)
122 {
123 break;
124 }
125 }
126
127 /* Power down the machine. */
128 rc = console->PowerDown(&progress);
129 if (!SUCCEEDED(rc))
130 {
131 printf("Console PowerDown fail! rc = 0x%x\n", rc);
132 printErrorInfo();
133 break;
134 }
135
136 /* Wait until VM is powered down. */
137 printf("Powering off VM, please wait ...\n");
138 rc = progress->WaitForCompletion(-1);
139 if (!SUCCEEDED(rc))
140 {
141 printf("Progress wait for PowerDown fail! rc = 0x%x\n", rc);
142 printErrorInfo();
143 break;
144 }
145
146 /* Close the session. */
147 rc = session->UnlockMachine();
148 if (!SUCCEEDED(rc))
149 {
150 printf("Cannot unlock machine! rc = 0x%x\n", rc);
151 printErrorInfo();
152 break;
153 }
154
155 } while (0);
156
157 SAFE_RELEASE(console);
158 SAFE_RELEASE(progress);
159 SAFE_RELEASE(session);
160 SysFreeString(guid);
161 SysFreeString(sessiontype);
162
163 // wait for unlocked
164 printf("Wait for machine unlocked ...\n");
165 while (true)
166 {
167 SessionState sessionState;
168 rc = machine->get_SessionState(&sessionState);
169 if (FAILED(rc))
170 {
171 printf("Cannot get session state! rc = 0x%x\n", rc);
172 printErrorInfo();
173 break;
174 }
175
176 if (sessionState == SessionState_Unlocked)
177 {
178 break;
179 }
180 }
181 SAFE_RELEASE(machine);
182 }
183
184 SysFreeString(machineName);
185
186 return 0;
187}
188
189int main(int argc, char **argv)
190{
191 /* Initialize the COM subsystem. */
192 CoInitialize(NULL);
193
194 /* Instantiate the VirtualBox root object. */
195 IVirtualBoxClient *virtualBoxClient;
196 HRESULT rc = CoCreateInstance(CLSID_VirtualBoxClient, /* the VirtualBoxClient object */
197 NULL, /* no aggregation */
198 CLSCTX_INPROC_SERVER, /* the object lives in the current process */
199 IID_IVirtualBoxClient, /* IID of the interface */
200 (void **)&virtualBoxClient);
201 if (SUCCEEDED(rc))
202 {
203 IVirtualBox *virtualBox;
204 rc = virtualBoxClient->get_VirtualBox(&virtualBox);
205 if (SUCCEEDED(rc))
206 {
207 for (int i = 0; i < 10; ++i)
208 {
209 printf("Test run %d\n", i);
210 testStartVM(virtualBox, L"TESTING", L"headless");
211 }
212
213 /* Release the VirtualBox object. */
214 virtualBox->Release();
215 virtualBoxClient->Release();
216 }
217 else
218 {
219 printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
220 printErrorInfo();
221 }
222 }
223
224 CoUninitialize();
225 return 0;
226}

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette