VirtualBox

source: vbox/trunk/src/VBox/Main/cbinding/tstLinuxC.c@ 16524

Last change on this file since 16524 was 16500, checked in by vboxsync, 16 years ago

Cosmetic changes (tabs to spaces).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1/* $Revsion: $ */
2/** @file tstLinuxC.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 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include "cbinding.h"
28
29static char *nsIDToString(nsID *guid);
30static void listVMs(IVirtualBox *virtualBox, ISession *session);
31static void startVM(IVirtualBox *virtualBox, ISession *session, nsID *id);
32
33/**
34 * Helper function to convert an nsID into a human readable string.
35 *
36 * @returns result string, allocated. Has to be freed using free()
37 * @param guid Pointer to nsID that will be converted.
38 */
39static char *nsIDToString(nsID *guid)
40{
41 /* Avoid magic number 39. Yes, sizeof "literal" includes the NUL byte. */
42 char *res = malloc(sizeof "{12345678-1234-1234-1234-123456789012}");
43
44 if (res != NULL)
45 {
46 sprintf(res, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
47 (unsigned)guid->m0, (unsigned)guid->m1, (unsigned)guid->m2,
48 (unsigned)guid->m3[0], (unsigned)guid->m3[1],
49 (unsigned)guid->m3[2], (unsigned)guid->m3[3],
50 (unsigned)guid->m3[4], (unsigned)guid->m3[5],
51 (unsigned)guid->m3[6], (unsigned)guid->m3[7]);
52 }
53 return res;
54}
55
56/**
57 * List the registered VMs.
58 *
59 * @param virtualBox ptr to IVirtualBox object
60 * @param session ptr to ISession object
61 */
62static void listVMs(IVirtualBox *virtualBox, ISession *session)
63{
64 nsresult rc;
65 IMachine **machines = NULL;
66 PRUint32 machineCnt = 0;
67 PRUint32 i;
68 unsigned start_id;
69
70 /*
71 * Get the list of all registered VMs.
72 */
73
74 rc = virtualBox->vtbl->GetMachines2(virtualBox, &machineCnt, &machines);
75 if (NS_FAILED(rc))
76 {
77 fprintf(stderr, "could not get list of machines, rc=%08x\n",
78 (unsigned)rc);
79 return;
80 }
81
82 if (machineCnt)
83 {
84 printf("VM List:\n\n");
85
86 /*
87 * Iterate through the collection.
88 */
89
90 for (i = 0; i < machineCnt; ++i)
91 {
92 IMachine *machine = machines[i];
93 PRBool isAccessible = PR_FALSE;
94
95 printf("\tMachine #%u\n", (unsigned)i);
96
97 if (!machine)
98 {
99 printf("\t(skipped, NULL)\n");
100 continue;
101 }
102
103 machine->vtbl->GetAccessible(machine, &isAccessible);
104
105 if (isAccessible)
106 {
107 PRUnichar *machineName_;
108 char *machineName;
109
110 machine->vtbl->GetName(machine, &machineName_);
111 VBoxUtf16ToUtf8(machineName_,&machineName);
112 printf("\tName: %s\n", machineName);
113
114 VBoxUtf8Free(machineName);
115 VBoxComUnallocMem(machineName_);
116 }
117 else
118 {
119 printf("\tName: <inaccessible>\n");
120 }
121
122
123 {
124 nsID *iid = NULL;
125 char *uuidString;
126
127 machine->vtbl->GetId(machine, &iid);
128 uuidString = nsIDToString(iid);
129 printf("\tUUID: %s\n", uuidString);
130
131 free(uuidString);
132 VBoxComUnallocMem(iid);
133 }
134
135 if (isAccessible)
136 {
137 {
138 PRUnichar *configFile;
139 char *configFile1 = calloc((size_t)64, (size_t)1);
140
141 machine->vtbl->GetSettingsFilePath(machine, &configFile);
142 VBoxUtf16ToUtf8(configFile, &configFile1);
143 printf("\tConfig file: %s\n", configFile1);
144
145 free(configFile1);
146 VBoxComUnallocMem(configFile);
147 }
148
149 {
150 PRUint32 memorySize;
151
152 machine->vtbl->GetMemorySize(machine, &memorySize);
153 printf("\tMemory size: %uMB\n", memorySize);
154 }
155
156 {
157 PRUnichar *typeId;
158 PRUnichar *osName_;
159 char *osName;
160 IGuestOSType *osType = NULL;
161
162 machine->vtbl->GetOSTypeId(machine, &typeId);
163 virtualBox->vtbl->GetGuestOSType(virtualBox, typeId, &osType);
164 osType->vtbl->GetDescription(osType, &osName_);
165 VBoxUtf16ToUtf8(osName_,&osName);
166 printf("\tGuest OS: %s\n\n", osName);
167
168 osType->vtbl->nsisupports.Release((void *)osType);
169 VBoxUtf8Free(osName);
170 VBoxComUnallocMem(osName_);
171 VBoxComUnallocMem(typeId);
172 }
173 }
174 }
175
176 /*
177 * Let the user chose a machine to start.
178 */
179
180 printf("Type Machine# to start (0 - %u) or 'quit' to do nothing: ",
181 (unsigned)(machineCnt - 1));
182 fflush(stdout);
183
184 if (scanf("%u", &start_id) == 1 && start_id < machineCnt)
185 {
186 IMachine *machine = machines[start_id];
187
188 if (machine)
189 {
190 nsID *iid = NULL;
191
192 machine->vtbl->GetId(machine, &iid);
193 startVM(virtualBox, session, iid);
194
195 VBoxComUnallocMem(iid);
196 }
197 }
198
199 /*
200 * Don't forget to release the objects in the array.
201 */
202
203 for (i = 0; i < machineCnt; ++i)
204 {
205 IMachine *machine = machines[i];
206
207 if (machine)
208 {
209 machine->vtbl->nsisupports.Release((void *)machine);
210 }
211 }
212 }
213 else
214 {
215 printf("\tNo VMs\n");
216 }
217}
218
219/**
220 * Start a VM.
221 *
222 * @param virtualBox ptr to IVirtualBox object
223 * @param session ptr to ISession object
224 * @param id identifies the machine to start
225 */
226static void startVM(IVirtualBox *virtualBox, ISession *session, nsID *id)
227{
228 nsresult rc;
229 IMachine *machine = NULL;
230 IProgress *progress = NULL;
231 PRUnichar *env = NULL;
232 PRUnichar *sessionType;
233
234 rc = virtualBox->vtbl->GetMachine(virtualBox, id, &machine);
235
236 if (NS_FAILED(rc) || !machine)
237 {
238 fprintf(stderr, "Error: Couldn't get the machine handle.\n");
239 return;
240 }
241
242 VBoxUtf8ToUtf16("gui", &sessionType);
243
244 rc = virtualBox->vtbl->OpenRemoteSession(
245 virtualBox,
246 session,
247 id,
248 sessionType,
249 env,
250 &progress
251 );
252
253 VBoxUtf16Free(sessionType);
254
255 if (NS_FAILED(rc))
256 {
257 fprintf(stderr, "Error: OpenRemoteSession failed.\n");
258 }
259 else
260 {
261 PRBool completed;
262 nsresult resultCode;
263
264 printf("Waiting for the remote session to open...\n");
265 progress->vtbl->WaitForCompletion(progress, -1);
266
267 rc = progress->vtbl->GetCompleted(progress, &completed);
268 if (NS_FAILED(rc))
269 {
270 fprintf (stderr, "Error: GetCompleted status failed.\n");
271 }
272
273 progress->vtbl->GetResultCode(progress, &resultCode);
274 if (NS_FAILED(resultCode))
275 {
276 IVirtualBoxErrorInfo *errorInfo;
277 PRUnichar *text_;
278 char *text;
279
280 progress->vtbl->GetErrorInfo(progress, &errorInfo);
281 errorInfo->vtbl->GetText(errorInfo, &text_);
282 VBoxUtf16ToUtf8(text_, &text);
283 printf("Error: %s\n", text);
284
285 VBoxComUnallocMem(text_);
286 VBoxUtf8Free(text);
287 }
288 else
289 {
290 fprintf(stderr, "Remote session has been successfully opened.\n");
291 }
292 progress->vtbl->nsisupports.Release((void *)progress);
293 }
294
295 /* It's important to always release resources. */
296 machine->vtbl->nsisupports.Release((void *)machine);
297}
298
299/* Main - Start the ball rolling. */
300
301int main(int argc, char **argv)
302{
303 IVirtualBox *vbox = NULL;
304 ISession *session = NULL;
305 PRUint32 revision = 0;
306 PRUnichar *version_ = NULL;
307 PRUnichar *homefolder_ = NULL;
308 nsresult rc; /* Result code of various function (method) calls. */
309
310 printf("Starting Main\n");
311
312 /*
313 * VBoxComInitialize does all the necessary startup action and
314 * provides us with pointers to vbox and session handles.
315 * It should be matched by a call to VBoxComUninitialize(vbox)
316 * when done.
317 */
318
319 VBoxComInitialize(&vbox, &session);
320
321 if (vbox == NULL)
322 {
323 fprintf(stderr, "%s: FATAL: could not get vbox handle\n", argv[0]);
324 return EXIT_FAILURE;
325 }
326 if (session == NULL)
327 {
328 fprintf (stderr, "%s: FATAL: could not get session handle\n", argv[0]);
329 return EXIT_FAILURE;
330 }
331
332 /*
333 * Now ask for revision, version and home folder information of
334 * this vbox. Were not using fancy macros here so it
335 * remains easy to see how we access C++'s vtable.
336 */
337
338 printf("----------------------------------------------------\n");
339
340 /* 1. Revision */
341
342 rc = vbox->vtbl->GetRevision(vbox, &revision);
343 if (NS_SUCCEEDED(rc))
344 {
345 printf("\tRevision: %u\n", revision);
346 }
347 else
348 {
349 fprintf(stderr, "%s: GetRevision() returned %08x\n",
350 argv[0], (unsigned)rc);
351 }
352
353 /* 2. Version */
354
355 rc = vbox->vtbl->GetVersion(vbox, &version_);
356 if (NS_SUCCEEDED(rc))
357 {
358 char *version = NULL;
359 VBoxUtf16ToUtf8(version_, &version);
360 printf("\tVersion: %s\n", version);
361 VBoxUtf8Free(version);
362 }
363 else
364 {
365 fprintf(stderr, "%s: GetVersion() returned %08x\n",
366 argv[0], (unsigned)rc);
367 }
368 VBoxComUnallocMem(version_);
369
370 /* 3. Home Folder */
371
372 rc = vbox->vtbl->GetHomeFolder(vbox, &homefolder_);
373 if (NS_SUCCEEDED(rc))
374 {
375 char *homefolder = NULL;
376 VBoxUtf16ToUtf8(homefolder_, &homefolder);
377 printf("\tHomeFolder: %s\n", homefolder);
378 VBoxUtf8Free(homefolder);
379 }
380 else
381 {
382 fprintf(stderr, "%s: GetHomeFolder() returned %08x\n",
383 argv[0], (unsigned)rc);
384 }
385 VBoxComUnallocMem(homefolder_);
386
387 listVMs(vbox, session);
388 session->vtbl->Close(session);
389
390 printf("----------------------------------------------------\n");
391
392 /*
393 * Do as mom told us: always clean up after yourself.
394 */
395
396 VBoxComUninitialize();
397 printf("Finished Main\n");
398
399 return 0;
400}
401/* 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