VirtualBox

source: vbox/trunk/src/VBox/Main/cbinding/tstXPCOMCGlue.c@ 28823

Last change on this file since 28823 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 Author Date Id Revision
File size: 10.5 KB
Line 
1/* $Revision: 28800 $ */
2/** @file tstXPCOMCGlue.c
3 * Demonstrator program to illustrate use of C bindings of Main API.
4 *
5 * Linux only at the moment due to shared library magic in the Makefile.
6 */
7
8/*
9 * Copyright (C) 2009 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#include "VBoxXPCOMCGlue.h"
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27
28static void listVMs(IVirtualBox *virtualBox, ISession *session);
29static void startVM(IVirtualBox *virtualBox, ISession *session, PRUnichar *id);
30
31/**
32 * List the registered VMs.
33 *
34 * @param virtualBox ptr to IVirtualBox object
35 * @param session ptr to ISession object
36 */
37static void listVMs(IVirtualBox *virtualBox, ISession *session)
38{
39 nsresult rc;
40 IMachine **machines = NULL;
41 PRUint32 machineCnt = 0;
42 PRUint32 i;
43 unsigned start_id;
44
45 /*
46 * Get the list of all registered VMs.
47 */
48
49 rc = virtualBox->vtbl->GetMachines(virtualBox, &machineCnt, &machines);
50 if (NS_FAILED(rc))
51 {
52 fprintf(stderr, "could not get list of machines, rc=%08x\n",
53 (unsigned)rc);
54 return;
55 }
56
57 if (machineCnt == 0)
58 {
59 printf("\tNo VMs\n");
60 return;
61 }
62
63 printf("VM List:\n\n");
64
65 /*
66 * Iterate through the collection.
67 */
68
69 for (i = 0; i < machineCnt; ++i)
70 {
71 IMachine *machine = machines[i];
72 PRBool isAccessible = PR_FALSE;
73
74 printf("\tMachine #%u\n", (unsigned)i);
75
76 if (!machine)
77 {
78 printf("\t(skipped, NULL)\n");
79 continue;
80 }
81
82 machine->vtbl->GetAccessible(machine, &isAccessible);
83
84 if (isAccessible)
85 {
86 PRUnichar *machineNameUtf16;
87 char *machineName;
88
89 machine->vtbl->GetName(machine, &machineNameUtf16);
90 g_pVBoxFuncs->pfnUtf16ToUtf8(machineNameUtf16,&machineName);
91 printf("\tName: %s\n", machineName);
92
93 g_pVBoxFuncs->pfnUtf8Free(machineName);
94 g_pVBoxFuncs->pfnComUnallocMem(machineNameUtf16);
95 }
96 else
97 {
98 printf("\tName: <inaccessible>\n");
99 }
100
101
102 {
103 PRUnichar *uuidUtf16 = NULL;
104 char *uuidUtf8 = NULL;
105
106 machine->vtbl->GetId(machine, &uuidUtf16);
107 g_pVBoxFuncs->pfnUtf16ToUtf8(uuidUtf16, &uuidUtf8);
108 printf("\tUUID: %s\n", uuidUtf8);
109
110 g_pVBoxFuncs->pfnUtf8Free(uuidUtf8);
111 g_pVBoxFuncs->pfnUtf16Free(uuidUtf16);
112 }
113
114 if (isAccessible)
115 {
116 {
117 PRUnichar *configFile;
118 char *configFile1 = calloc((size_t)64, (size_t)1);
119
120 machine->vtbl->GetSettingsFilePath(machine, &configFile);
121 g_pVBoxFuncs->pfnUtf16ToUtf8(configFile, &configFile1);
122 printf("\tConfig file: %s\n", configFile1);
123
124 free(configFile1);
125 g_pVBoxFuncs->pfnComUnallocMem(configFile);
126 }
127
128 {
129 PRUint32 memorySize;
130
131 machine->vtbl->GetMemorySize(machine, &memorySize);
132 printf("\tMemory size: %uMB\n", memorySize);
133 }
134
135 {
136 PRUnichar *typeId;
137 PRUnichar *osNameUtf16;
138 char *osName;
139 IGuestOSType *osType = NULL;
140
141 machine->vtbl->GetOSTypeId(machine, &typeId);
142 virtualBox->vtbl->GetGuestOSType(virtualBox, typeId, &osType);
143 osType->vtbl->GetDescription(osType, &osNameUtf16);
144 g_pVBoxFuncs->pfnUtf16ToUtf8(osNameUtf16,&osName);
145 printf("\tGuest OS: %s\n\n", osName);
146
147 osType->vtbl->nsisupports.Release((void *)osType);
148 g_pVBoxFuncs->pfnUtf8Free(osName);
149 g_pVBoxFuncs->pfnComUnallocMem(osNameUtf16);
150 g_pVBoxFuncs->pfnComUnallocMem(typeId);
151 }
152 }
153 }
154
155 /*
156 * Let the user chose a machine to start.
157 */
158
159 printf("Type Machine# to start (0 - %u) or 'quit' to do nothing: ",
160 (unsigned)(machineCnt - 1));
161 fflush(stdout);
162
163 if (scanf("%u", &start_id) == 1 && start_id < machineCnt)
164 {
165 IMachine *machine = machines[start_id];
166
167 if (machine)
168 {
169 PRUnichar *uuidUtf16 = NULL;
170
171 machine->vtbl->GetId(machine, &uuidUtf16);
172 startVM(virtualBox, session, uuidUtf16);
173
174 g_pVBoxFuncs->pfnUtf16Free(uuidUtf16);
175 }
176 }
177
178 /*
179 * Don't forget to release the objects in the array.
180 */
181
182 for (i = 0; i < machineCnt; ++i)
183 {
184 IMachine *machine = machines[i];
185
186 if (machine)
187 {
188 machine->vtbl->nsisupports.Release((void *)machine);
189 }
190 }
191}
192
193/**
194 * Start a VM.
195 *
196 * @param virtualBox ptr to IVirtualBox object
197 * @param session ptr to ISession object
198 * @param id identifies the machine to start
199 */
200static void startVM(IVirtualBox *virtualBox, ISession *session, PRUnichar *id)
201{
202 nsresult rc;
203 IMachine *machine = NULL;
204 IProgress *progress = NULL;
205 PRUnichar *env = NULL;
206 PRUnichar *sessionType;
207
208 rc = virtualBox->vtbl->GetMachine(virtualBox, id, &machine);
209
210 if (NS_FAILED(rc) || !machine)
211 {
212 fprintf(stderr, "Error: Couldn't get the machine handle.\n");
213 return;
214 }
215
216 g_pVBoxFuncs->pfnUtf8ToUtf16("gui", &sessionType);
217
218 rc = virtualBox->vtbl->OpenRemoteSession(
219 virtualBox,
220 session,
221 id,
222 sessionType,
223 env,
224 &progress
225 );
226
227 g_pVBoxFuncs->pfnUtf16Free(sessionType);
228
229 if (NS_FAILED(rc))
230 {
231 fprintf(stderr, "Error: OpenRemoteSession failed.\n");
232 }
233 else
234 {
235 PRBool completed;
236 PRInt32 resultCode;
237
238 printf("Waiting for the remote session to open...\n");
239 progress->vtbl->WaitForCompletion(progress, -1);
240
241 rc = progress->vtbl->GetCompleted(progress, &completed);
242 if (NS_FAILED(rc))
243 {
244 fprintf (stderr, "Error: GetCompleted status failed.\n");
245 }
246
247 progress->vtbl->GetResultCode(progress, &resultCode);
248 if (NS_FAILED(resultCode))
249 {
250 IVirtualBoxErrorInfo *errorInfo;
251 PRUnichar *textUtf16;
252 char *text;
253
254 progress->vtbl->GetErrorInfo(progress, &errorInfo);
255 errorInfo->vtbl->GetText(errorInfo, &textUtf16);
256 g_pVBoxFuncs->pfnUtf16ToUtf8(textUtf16, &text);
257 printf("Error: %s\n", text);
258
259 g_pVBoxFuncs->pfnComUnallocMem(textUtf16);
260 g_pVBoxFuncs->pfnUtf8Free(text);
261 }
262 else
263 {
264 fprintf(stderr, "Remote session has been successfully opened.\n");
265 }
266 progress->vtbl->nsisupports.Release((void *)progress);
267 }
268
269 /* It's important to always release resources. */
270 machine->vtbl->nsisupports.Release((void *)machine);
271}
272
273/* Main - Start the ball rolling. */
274
275int main(int argc, char **argv)
276{
277 IVirtualBox *vbox = NULL;
278 ISession *session = NULL;
279 PRUint32 revision = 0;
280 PRUnichar *versionUtf16 = NULL;
281 PRUnichar *homefolderUtf16 = NULL;
282 nsresult rc; /* Result code of various function (method) calls. */
283
284 printf("Starting Main\n");
285
286 /*
287 * VBoxComInitialize does all the necessary startup action and
288 * provides us with pointers to vbox and session handles.
289 * It should be matched by a call to VBoxComUninitialize(vbox)
290 * when done.
291 */
292
293 if (VBoxCGlueInit() != 0)
294 {
295 fprintf(stderr, "%s: FATAL: VBoxCGlueInit failed: %s\n",
296 argv[0], g_szVBoxErrMsg);
297 return EXIT_FAILURE;
298 }
299
300 g_pVBoxFuncs->pfnComInitialize(IVIRTUALBOX_IID_STR, &vbox,
301 ISESSION_IID_STR, &session);
302 if (vbox == NULL)
303 {
304 fprintf(stderr, "%s: FATAL: could not get vbox handle\n", argv[0]);
305 return EXIT_FAILURE;
306 }
307 if (session == NULL)
308 {
309 fprintf(stderr, "%s: FATAL: could not get session handle\n", argv[0]);
310 return EXIT_FAILURE;
311 }
312
313 /*
314 * Now ask for revision, version and home folder information of
315 * this vbox. Were not using fancy macros here so it
316 * remains easy to see how we access C++'s vtable.
317 */
318
319 printf("----------------------------------------------------\n");
320
321 /* 1. Revision */
322
323 rc = vbox->vtbl->GetRevision(vbox, &revision);
324 if (NS_SUCCEEDED(rc))
325 {
326 printf("\tRevision: %u\n", revision);
327 }
328 else
329 {
330 fprintf(stderr, "%s: GetRevision() returned %08x\n",
331 argv[0], (unsigned)rc);
332 }
333
334 /* 2. Version */
335
336 rc = vbox->vtbl->GetVersion(vbox, &versionUtf16);
337 if (NS_SUCCEEDED(rc))
338 {
339 char *version = NULL;
340 g_pVBoxFuncs->pfnUtf16ToUtf8(versionUtf16, &version);
341 printf("\tVersion: %s\n", version);
342 g_pVBoxFuncs->pfnUtf8Free(version);
343 g_pVBoxFuncs->pfnComUnallocMem(versionUtf16);
344 }
345 else
346 {
347 fprintf(stderr, "%s: GetVersion() returned %08x\n",
348 argv[0], (unsigned)rc);
349 }
350
351 /* 3. Home Folder */
352
353 rc = vbox->vtbl->GetHomeFolder(vbox, &homefolderUtf16);
354 if (NS_SUCCEEDED(rc))
355 {
356 char *homefolder = NULL;
357 g_pVBoxFuncs->pfnUtf16ToUtf8(homefolderUtf16, &homefolder);
358 printf("\tHomeFolder: %s\n", homefolder);
359 g_pVBoxFuncs->pfnUtf8Free(homefolder);
360 g_pVBoxFuncs->pfnComUnallocMem(homefolderUtf16);
361 }
362 else
363 {
364 fprintf(stderr, "%s: GetHomeFolder() returned %08x\n",
365 argv[0], (unsigned)rc);
366 }
367
368 listVMs(vbox, session);
369 session->vtbl->Close(session);
370
371 printf("----------------------------------------------------\n");
372
373 /*
374 * Do as mom told us: always clean up after yourself.
375 */
376
377 g_pVBoxFuncs->pfnComUninitialize();
378 VBoxCGlueTerm();
379 printf("Finished Main\n");
380
381 return 0;
382}
383/* vim: set ts=4 sw=4 et: */
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