1 | /* $Id: tstVBoxAPIWin.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * tstVBoxAPIWin - sample program to illustrate the VirtualBox
|
---|
5 | * COM API for machine management on Windows.
|
---|
6 | It only uses standard C/C++ and COM semantics,
|
---|
7 | * no additional VBox classes/macros/helpers. To
|
---|
8 | * make things even easier to follow, only the
|
---|
9 | * standard Win32 API has been used. Typically,
|
---|
10 | * C++ developers would make use of Microsoft's
|
---|
11 | * ATL to ease development.
|
---|
12 | */
|
---|
13 |
|
---|
14 | /*
|
---|
15 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
16 | *
|
---|
17 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
18 | * available from http://www.virtualbox.org. This file is free software;
|
---|
19 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
20 | * General Public License (GPL) as published by the Free Software
|
---|
21 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
22 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
23 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
24 | */
|
---|
25 |
|
---|
26 | /*
|
---|
27 | * PURPOSE OF THIS SAMPLE PROGRAM
|
---|
28 | * ------------------------------
|
---|
29 | *
|
---|
30 | * This sample program is intended to demonstrate the minimal code necessary
|
---|
31 | * to use VirtualBox COM API for learning puroses only. The program uses pure
|
---|
32 | * Win32 API and doesn't have any extra dependencies to let you better
|
---|
33 | * understand what is going on when a client talks to the VirtualBox core
|
---|
34 | * using the COM framework.
|
---|
35 | *
|
---|
36 | * However, if you want to write a real application, it is highly recommended
|
---|
37 | * to use our MS COM XPCOM Glue library and helper C++ classes. This way, you
|
---|
38 | * will get at least the following benefits:
|
---|
39 | *
|
---|
40 | * a) better portability: both the MS COM (used on Windows) and XPCOM (used
|
---|
41 | * everywhere else) VirtualBox client application from the same source code
|
---|
42 | * (including common smart C++ templates for automatic interface pointer
|
---|
43 | * reference counter and string data management);
|
---|
44 | * b) simpler XPCOM initialization and shutdown (only a single method call
|
---|
45 | * that does everything right).
|
---|
46 | *
|
---|
47 | * Currently, there is no separate sample program that uses the VirtualBox MS
|
---|
48 | * COM XPCOM Glue library. Please refer to the sources of stock VirtualBox
|
---|
49 | * applications such as the VirtualBox GUI frontend or the VBoxManage command
|
---|
50 | * line frontend.
|
---|
51 | */
|
---|
52 |
|
---|
53 |
|
---|
54 | #include <stdio.h>
|
---|
55 | #include <iprt/win/windows.h> /* Avoid -Wall warnings. */
|
---|
56 | #include "VirtualBox.h"
|
---|
57 |
|
---|
58 | #define SAFE_RELEASE(x) \
|
---|
59 | if (x) { \
|
---|
60 | x->Release(); \
|
---|
61 | x = NULL; \
|
---|
62 | }
|
---|
63 |
|
---|
64 | int listVMs(IVirtualBox *virtualBox)
|
---|
65 | {
|
---|
66 | HRESULT rc;
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * First we have to get a list of all registered VMs
|
---|
70 | */
|
---|
71 | SAFEARRAY *machinesArray = NULL;
|
---|
72 |
|
---|
73 | rc = virtualBox->get_Machines(&machinesArray);
|
---|
74 | if (SUCCEEDED(rc))
|
---|
75 | {
|
---|
76 | IMachine **machines;
|
---|
77 | rc = SafeArrayAccessData(machinesArray, (void **) &machines);
|
---|
78 | if (SUCCEEDED(rc))
|
---|
79 | {
|
---|
80 | for (ULONG i = 0; i < machinesArray->rgsabound[0].cElements; ++i)
|
---|
81 | {
|
---|
82 | BSTR str;
|
---|
83 |
|
---|
84 | rc = machines[i]->get_Name(&str);
|
---|
85 | if (SUCCEEDED(rc))
|
---|
86 | {
|
---|
87 | printf("Name: %S\n", str);
|
---|
88 | SysFreeString(str);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | SafeArrayUnaccessData(machinesArray);
|
---|
93 | }
|
---|
94 |
|
---|
95 | SafeArrayDestroy(machinesArray);
|
---|
96 | }
|
---|
97 |
|
---|
98 | return 0;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | int testErrorInfo(IVirtualBox *virtualBox)
|
---|
103 | {
|
---|
104 | HRESULT rc;
|
---|
105 |
|
---|
106 | /* Try to find a machine that doesn't exist */
|
---|
107 | IMachine *machine = NULL;
|
---|
108 | BSTR machineName = SysAllocString(L"Foobar");
|
---|
109 |
|
---|
110 | rc = virtualBox->FindMachine(machineName, &machine);
|
---|
111 |
|
---|
112 | if (FAILED(rc))
|
---|
113 | {
|
---|
114 | IErrorInfo *errorInfo;
|
---|
115 |
|
---|
116 | rc = GetErrorInfo(0, &errorInfo);
|
---|
117 |
|
---|
118 | if (FAILED(rc))
|
---|
119 | printf("Error getting error info! rc=%#lx\n", rc);
|
---|
120 | else
|
---|
121 | {
|
---|
122 | BSTR errorDescription = NULL;
|
---|
123 |
|
---|
124 | rc = errorInfo->GetDescription(&errorDescription);
|
---|
125 |
|
---|
126 | if (FAILED(rc) || !errorDescription)
|
---|
127 | printf("Error getting error description! rc=%#lx\n", rc);
|
---|
128 | else
|
---|
129 | {
|
---|
130 | printf("Successfully retrieved error description: %S\n", errorDescription);
|
---|
131 |
|
---|
132 | SysFreeString(errorDescription);
|
---|
133 | }
|
---|
134 |
|
---|
135 | errorInfo->Release();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | SAFE_RELEASE(machine);
|
---|
140 | SysFreeString(machineName);
|
---|
141 |
|
---|
142 | return 0;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | int testStartVM(IVirtualBox *virtualBox)
|
---|
147 | {
|
---|
148 | HRESULT rc;
|
---|
149 |
|
---|
150 | /* Try to start a VM called "WinXP SP2". */
|
---|
151 | IMachine *machine = NULL;
|
---|
152 | BSTR machineName = SysAllocString(L"WinXP SP2");
|
---|
153 |
|
---|
154 | rc = virtualBox->FindMachine(machineName, &machine);
|
---|
155 |
|
---|
156 | if (FAILED(rc))
|
---|
157 | {
|
---|
158 | IErrorInfo *errorInfo;
|
---|
159 |
|
---|
160 | rc = GetErrorInfo(0, &errorInfo);
|
---|
161 |
|
---|
162 | if (FAILED(rc))
|
---|
163 | printf("Error getting error info! rc=%#lx\n", rc);
|
---|
164 | else
|
---|
165 | {
|
---|
166 | BSTR errorDescription = NULL;
|
---|
167 |
|
---|
168 | rc = errorInfo->GetDescription(&errorDescription);
|
---|
169 |
|
---|
170 | if (FAILED(rc) || !errorDescription)
|
---|
171 | printf("Error getting error description! rc=%#lx\n", rc);
|
---|
172 | else
|
---|
173 | {
|
---|
174 | printf("Successfully retrieved error description: %S\n", errorDescription);
|
---|
175 |
|
---|
176 | SysFreeString(errorDescription);
|
---|
177 | }
|
---|
178 |
|
---|
179 | SAFE_RELEASE(errorInfo);
|
---|
180 | }
|
---|
181 | }
|
---|
182 | else
|
---|
183 | {
|
---|
184 | ISession *session = NULL;
|
---|
185 | IConsole *console = NULL;
|
---|
186 | IProgress *progress = NULL;
|
---|
187 | BSTR sessiontype = SysAllocString(L"gui");
|
---|
188 | BSTR guid;
|
---|
189 |
|
---|
190 | do
|
---|
191 | {
|
---|
192 | rc = machine->get_Id(&guid); /* Get the GUID of the machine. */
|
---|
193 | if (!SUCCEEDED(rc))
|
---|
194 | {
|
---|
195 | printf("Error retrieving machine ID! rc=%#lx\n", rc);
|
---|
196 | break;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /* Create the session object. */
|
---|
200 | rc = CoCreateInstance(CLSID_Session, /* the VirtualBox base object */
|
---|
201 | NULL, /* no aggregation */
|
---|
202 | CLSCTX_INPROC_SERVER, /* the object lives in the current process */
|
---|
203 | IID_ISession, /* IID of the interface */
|
---|
204 | (void**)&session);
|
---|
205 | if (!SUCCEEDED(rc))
|
---|
206 | {
|
---|
207 | printf("Error creating Session instance! rc=%#lx\n", rc);
|
---|
208 | break;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* Start a VM session using the delivered VBox GUI. */
|
---|
212 | rc = machine->LaunchVMProcess(session, sessiontype,
|
---|
213 | NULL, &progress);
|
---|
214 | if (!SUCCEEDED(rc))
|
---|
215 | {
|
---|
216 | printf("Could not open remote session! rc=%#lx\n", rc);
|
---|
217 | break;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* Wait until VM is running. */
|
---|
221 | printf("Starting VM, please wait ...\n");
|
---|
222 | rc = progress->WaitForCompletion(-1);
|
---|
223 |
|
---|
224 | /* Get console object. */
|
---|
225 | session->get_Console(&console);
|
---|
226 |
|
---|
227 | /* Bring console window to front. */
|
---|
228 | machine->ShowConsoleWindow(0);
|
---|
229 |
|
---|
230 | printf("Press enter to power off VM and close the session...\n");
|
---|
231 | getchar();
|
---|
232 |
|
---|
233 | /* Power down the machine. */
|
---|
234 | rc = console->PowerDown(&progress);
|
---|
235 |
|
---|
236 | /* Wait until VM is powered down. */
|
---|
237 | printf("Powering off VM, please wait ...\n");
|
---|
238 | rc = progress->WaitForCompletion(-1);
|
---|
239 |
|
---|
240 | /* Close the session. */
|
---|
241 | rc = session->UnlockMachine();
|
---|
242 |
|
---|
243 | } while (0);
|
---|
244 |
|
---|
245 | SAFE_RELEASE(console);
|
---|
246 | SAFE_RELEASE(progress);
|
---|
247 | SAFE_RELEASE(session);
|
---|
248 | SysFreeString(guid);
|
---|
249 | SysFreeString(sessiontype);
|
---|
250 | SAFE_RELEASE(machine);
|
---|
251 | }
|
---|
252 |
|
---|
253 | SysFreeString(machineName);
|
---|
254 |
|
---|
255 | return 0;
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | int main()
|
---|
260 | {
|
---|
261 | /* Initialize the COM subsystem. */
|
---|
262 | CoInitialize(NULL);
|
---|
263 |
|
---|
264 | /* Instantiate the VirtualBox root object. */
|
---|
265 | IVirtualBoxClient *virtualBoxClient;
|
---|
266 | HRESULT rc = CoCreateInstance(CLSID_VirtualBoxClient, /* the VirtualBoxClient object */
|
---|
267 | NULL, /* no aggregation */
|
---|
268 | CLSCTX_INPROC_SERVER, /* the object lives in the current process */
|
---|
269 | IID_IVirtualBoxClient, /* IID of the interface */
|
---|
270 | (void**)&virtualBoxClient);
|
---|
271 | if (SUCCEEDED(rc))
|
---|
272 | {
|
---|
273 | IVirtualBox *virtualBox;
|
---|
274 | rc = virtualBoxClient->get_VirtualBox(&virtualBox);
|
---|
275 | if (SUCCEEDED(rc))
|
---|
276 | {
|
---|
277 | listVMs(virtualBox);
|
---|
278 |
|
---|
279 | testErrorInfo(virtualBox);
|
---|
280 |
|
---|
281 | /* Enable the following line to get a VM started. */
|
---|
282 | //testStartVM(virtualBox);
|
---|
283 |
|
---|
284 | /* Release the VirtualBox object. */
|
---|
285 | virtualBox->Release();
|
---|
286 | virtualBoxClient->Release();
|
---|
287 | }
|
---|
288 | else
|
---|
289 | printf("Error creating VirtualBox instance! rc=%#lx\n", rc);
|
---|
290 | }
|
---|
291 |
|
---|
292 | CoUninitialize();
|
---|
293 | return 0;
|
---|
294 | }
|
---|
295 |
|
---|