VirtualBox

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

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