VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstVBoxAPIWin.cpp@ 29788

Last change on this file since 29788 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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

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