VirtualBox

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

Last change on this file since 33238 was 31070, checked in by vboxsync, 14 years ago

Main: rename ISession::close() to ISession::unlockMachine(); API documentation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1/* $Id: tstVBoxAPIWin.cpp 31070 2010-07-23 16:00:09Z 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-2007 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 signle 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 "VirtualBox.h"
56
57#define SAFE_RELEASE(x) \
58 if (x) { \
59 x->Release(); \
60 x = NULL; \
61 }
62
63int listVMs(IVirtualBox *virtualBox)
64{
65 HRESULT rc;
66
67 /*
68 * First we have to get a list of all registered VMs
69 */
70 SAFEARRAY *machinesArray = NULL;
71
72 rc = virtualBox->get_Machines(&machinesArray);
73 if (SUCCEEDED(rc))
74 {
75 IMachine **machines;
76 rc = SafeArrayAccessData (machinesArray, (void **) &machines);
77 if (SUCCEEDED(rc))
78 {
79 for (ULONG i = 0; i < machinesArray->rgsabound[0].cElements; ++i)
80 {
81 BSTR str;
82
83 rc = machines[i]->get_Name(&str);
84 if (SUCCEEDED(rc))
85 {
86 printf("Name: %S\n", str);
87 SysFreeString(str);
88 }
89 }
90
91 SafeArrayUnaccessData (machinesArray);
92 }
93
94 SafeArrayDestroy (machinesArray);
95 }
96
97 return 0;
98}
99
100
101int testErrorInfo(IVirtualBox *virtualBox)
102{
103 HRESULT rc;
104
105 /* Try to find a machine that doesn't exist */
106 IMachine *machine = NULL;
107 BSTR machineName = SysAllocString(L"Foobar");
108
109 rc = virtualBox->FindMachine(machineName, &machine);
110
111 if (FAILED(rc))
112 {
113 IErrorInfo *errorInfo;
114
115 rc = GetErrorInfo(0, &errorInfo);
116
117 if (FAILED(rc))
118 printf("Error getting error info! rc = 0x%x\n", rc);
119 else
120 {
121 BSTR errorDescription = NULL;
122
123 rc = errorInfo->GetDescription(&errorDescription);
124
125 if (FAILED(rc) || !errorDescription)
126 printf("Error getting error description! rc = 0x%x\n", rc);
127 else
128 {
129 printf("Successfully retrieved error description: %S\n", errorDescription);
130
131 SysFreeString(errorDescription);
132 }
133
134 errorInfo->Release();
135 }
136 }
137
138 SAFE_RELEASE(machine);
139 SysFreeString(machineName);
140
141 return 0;
142}
143
144
145int testStartVM(IVirtualBox *virtualBox)
146{
147 HRESULT rc;
148
149 /* Try to start a VM called "WinXP SP2". */
150 IMachine *machine = NULL;
151 BSTR machineName = SysAllocString(L"WinXP SP2");
152
153 rc = virtualBox->FindMachine(machineName, &machine);
154
155 if (FAILED(rc))
156 {
157 IErrorInfo *errorInfo;
158
159 rc = GetErrorInfo(0, &errorInfo);
160
161 if (FAILED(rc))
162 printf("Error getting error info! rc = 0x%x\n", rc);
163 else
164 {
165 BSTR errorDescription = NULL;
166
167 rc = errorInfo->GetDescription(&errorDescription);
168
169 if (FAILED(rc) || !errorDescription)
170 printf("Error getting error description! rc = 0x%x\n", rc);
171 else
172 {
173 printf("Successfully retrieved error description: %S\n", errorDescription);
174
175 SysFreeString(errorDescription);
176 }
177
178 SAFE_RELEASE(errorInfo);
179 }
180 }
181 else
182 {
183 ISession *session = NULL;
184 IConsole *console = NULL;
185 IProgress *progress = NULL;
186 BSTR sessiontype = SysAllocString(L"gui");
187 BSTR guid;
188
189 do
190 {
191 rc = machine->get_Id(&guid); /* Get the GUID of the machine. */
192 if (!SUCCEEDED(rc))
193 {
194 printf("Error retrieving machine ID! rc = 0x%x\n", rc);
195 break;
196 }
197
198 /* Create the session object. */
199 rc = CoCreateInstance(CLSID_Session, /* the VirtualBox base object */
200 NULL, /* no aggregation */
201 CLSCTX_INPROC_SERVER, /* the object lives in a server process on this machine */
202 IID_ISession, /* IID of the interface */
203 (void**)&session);
204 if (!SUCCEEDED(rc))
205 {
206 printf("Error creating Session instance! rc = 0x%x\n", rc);
207 break;
208 }
209
210 /* Start a VM session using the delivered VBox GUI. */
211 rc = machine->LaunchVMProcess(session, sessiontype,
212 NULL, &progress);
213 if (!SUCCEEDED(rc))
214 {
215 printf("Could not open remote session! rc = 0x%x\n", rc);
216 break;
217 }
218
219 /* Wait until VM is running. */
220 printf ("Starting VM, please wait ...\n");
221 rc = progress->WaitForCompletion (-1);
222
223 /* Get console object. */
224 session->get_Console(&console);
225
226 /* Bring console window to front. */
227 machine->ShowConsoleWindow(0);
228
229 printf ("Press enter to power off VM and close the session...\n");
230 getchar();
231
232 /* Power down the machine. */
233 rc = console->PowerDown(&progress);
234
235 /* Wait until VM is powered down. */
236 printf ("Powering off VM, please wait ...\n");
237 rc = progress->WaitForCompletion (-1);
238
239 /* Close the session. */
240 rc = session->UnlockMachine();
241
242 } while (0);
243
244 SAFE_RELEASE(console);
245 SAFE_RELEASE(progress);
246 SAFE_RELEASE(session);
247 SysFreeString(guid);
248 SysFreeString(sessiontype);
249 SAFE_RELEASE(machine);
250 }
251
252 SysFreeString(machineName);
253
254 return 0;
255}
256
257
258int main(int argc, char *argv[])
259{
260 HRESULT rc;
261 IVirtualBox *virtualBox;
262
263 do
264 {
265 /* Initialize the COM subsystem. */
266 CoInitialize(NULL);
267
268 /* Instantiate the VirtualBox root object. */
269 rc = CoCreateInstance(CLSID_VirtualBox, /* the VirtualBox base object */
270 NULL, /* no aggregation */
271 CLSCTX_LOCAL_SERVER, /* the object lives in a server process on this machine */
272 IID_IVirtualBox, /* IID of the interface */
273 (void**)&virtualBox);
274
275 if (!SUCCEEDED(rc))
276 {
277 printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
278 break;
279 }
280
281 listVMs(virtualBox);
282
283 testErrorInfo(virtualBox);
284
285 /* Enable the following line to get a VM started. */
286 //testStartVM(virtualBox);
287
288 /* Release the VirtualBox object. */
289 virtualBox->Release();
290
291 } while (0);
292
293 CoUninitialize();
294 return 0;
295}
296
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