1 | /* $Revision: 33294 $ */
|
---|
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 |
|
---|
28 | static void listVMs(IVirtualBox *virtualBox, ISession *session);
|
---|
29 | static 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 | */
|
---|
37 | static 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 | */
|
---|
200 | static 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->FindMachine(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 = machine->vtbl->LaunchVMProcess(machine,
|
---|
219 | session,
|
---|
220 | sessionType,
|
---|
221 | env,
|
---|
222 | &progress
|
---|
223 | );
|
---|
224 |
|
---|
225 | g_pVBoxFuncs->pfnUtf16Free(sessionType);
|
---|
226 |
|
---|
227 | if (NS_FAILED(rc))
|
---|
228 | {
|
---|
229 | fprintf(stderr, "Error: OpenRemoteSession failed.\n");
|
---|
230 | }
|
---|
231 | else
|
---|
232 | {
|
---|
233 | PRBool completed;
|
---|
234 | PRInt32 resultCode;
|
---|
235 |
|
---|
236 | printf("Waiting for the remote session to open...\n");
|
---|
237 | progress->vtbl->WaitForCompletion(progress, -1);
|
---|
238 |
|
---|
239 | rc = progress->vtbl->GetCompleted(progress, &completed);
|
---|
240 | if (NS_FAILED(rc))
|
---|
241 | {
|
---|
242 | fprintf (stderr, "Error: GetCompleted status failed.\n");
|
---|
243 | }
|
---|
244 |
|
---|
245 | progress->vtbl->GetResultCode(progress, &resultCode);
|
---|
246 | if (NS_FAILED(resultCode))
|
---|
247 | {
|
---|
248 | IVirtualBoxErrorInfo *errorInfo;
|
---|
249 | PRUnichar *textUtf16;
|
---|
250 | char *text;
|
---|
251 |
|
---|
252 | progress->vtbl->GetErrorInfo(progress, &errorInfo);
|
---|
253 | errorInfo->vtbl->GetText(errorInfo, &textUtf16);
|
---|
254 | g_pVBoxFuncs->pfnUtf16ToUtf8(textUtf16, &text);
|
---|
255 | printf("Error: %s\n", text);
|
---|
256 |
|
---|
257 | g_pVBoxFuncs->pfnComUnallocMem(textUtf16);
|
---|
258 | g_pVBoxFuncs->pfnUtf8Free(text);
|
---|
259 | }
|
---|
260 | else
|
---|
261 | {
|
---|
262 | fprintf(stderr, "Remote session has been successfully opened.\n");
|
---|
263 | }
|
---|
264 | progress->vtbl->nsisupports.Release((void *)progress);
|
---|
265 | }
|
---|
266 |
|
---|
267 | /* It's important to always release resources. */
|
---|
268 | machine->vtbl->nsisupports.Release((void *)machine);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* Main - Start the ball rolling. */
|
---|
272 |
|
---|
273 | int main(int argc, char **argv)
|
---|
274 | {
|
---|
275 | IVirtualBox *vbox = NULL;
|
---|
276 | ISession *session = NULL;
|
---|
277 | PRUint32 revision = 0;
|
---|
278 | PRUnichar *versionUtf16 = NULL;
|
---|
279 | PRUnichar *homefolderUtf16 = NULL;
|
---|
280 | nsresult rc; /* Result code of various function (method) calls. */
|
---|
281 |
|
---|
282 | printf("Starting Main\n");
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * VBoxComInitialize does all the necessary startup action and
|
---|
286 | * provides us with pointers to vbox and session handles.
|
---|
287 | * It should be matched by a call to VBoxComUninitialize(vbox)
|
---|
288 | * when done.
|
---|
289 | */
|
---|
290 |
|
---|
291 | if (VBoxCGlueInit() != 0)
|
---|
292 | {
|
---|
293 | fprintf(stderr, "%s: FATAL: VBoxCGlueInit failed: %s\n",
|
---|
294 | argv[0], g_szVBoxErrMsg);
|
---|
295 | return EXIT_FAILURE;
|
---|
296 | }
|
---|
297 |
|
---|
298 | g_pVBoxFuncs->pfnComInitialize(IVIRTUALBOX_IID_STR, &vbox,
|
---|
299 | ISESSION_IID_STR, &session);
|
---|
300 | if (vbox == NULL)
|
---|
301 | {
|
---|
302 | fprintf(stderr, "%s: FATAL: could not get vbox handle\n", argv[0]);
|
---|
303 | return EXIT_FAILURE;
|
---|
304 | }
|
---|
305 | if (session == NULL)
|
---|
306 | {
|
---|
307 | fprintf(stderr, "%s: FATAL: could not get session handle\n", argv[0]);
|
---|
308 | return EXIT_FAILURE;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /*
|
---|
312 | * Now ask for revision, version and home folder information of
|
---|
313 | * this vbox. Were not using fancy macros here so it
|
---|
314 | * remains easy to see how we access C++'s vtable.
|
---|
315 | */
|
---|
316 |
|
---|
317 | printf("----------------------------------------------------\n");
|
---|
318 |
|
---|
319 | /* 1. Revision */
|
---|
320 |
|
---|
321 | rc = vbox->vtbl->GetRevision(vbox, &revision);
|
---|
322 | if (NS_SUCCEEDED(rc))
|
---|
323 | {
|
---|
324 | printf("\tRevision: %u\n", revision);
|
---|
325 | }
|
---|
326 | else
|
---|
327 | {
|
---|
328 | fprintf(stderr, "%s: GetRevision() returned %08x\n",
|
---|
329 | argv[0], (unsigned)rc);
|
---|
330 | }
|
---|
331 |
|
---|
332 | /* 2. Version */
|
---|
333 |
|
---|
334 | rc = vbox->vtbl->GetVersion(vbox, &versionUtf16);
|
---|
335 | if (NS_SUCCEEDED(rc))
|
---|
336 | {
|
---|
337 | char *version = NULL;
|
---|
338 | g_pVBoxFuncs->pfnUtf16ToUtf8(versionUtf16, &version);
|
---|
339 | printf("\tVersion: %s\n", version);
|
---|
340 | g_pVBoxFuncs->pfnUtf8Free(version);
|
---|
341 | g_pVBoxFuncs->pfnComUnallocMem(versionUtf16);
|
---|
342 | }
|
---|
343 | else
|
---|
344 | {
|
---|
345 | fprintf(stderr, "%s: GetVersion() returned %08x\n",
|
---|
346 | argv[0], (unsigned)rc);
|
---|
347 | }
|
---|
348 |
|
---|
349 | /* 3. Home Folder */
|
---|
350 |
|
---|
351 | rc = vbox->vtbl->GetHomeFolder(vbox, &homefolderUtf16);
|
---|
352 | if (NS_SUCCEEDED(rc))
|
---|
353 | {
|
---|
354 | char *homefolder = NULL;
|
---|
355 | g_pVBoxFuncs->pfnUtf16ToUtf8(homefolderUtf16, &homefolder);
|
---|
356 | printf("\tHomeFolder: %s\n", homefolder);
|
---|
357 | g_pVBoxFuncs->pfnUtf8Free(homefolder);
|
---|
358 | g_pVBoxFuncs->pfnComUnallocMem(homefolderUtf16);
|
---|
359 | }
|
---|
360 | else
|
---|
361 | {
|
---|
362 | fprintf(stderr, "%s: GetHomeFolder() returned %08x\n",
|
---|
363 | argv[0], (unsigned)rc);
|
---|
364 | }
|
---|
365 |
|
---|
366 | listVMs(vbox, session);
|
---|
367 | session->vtbl->UnlockMachine(session);
|
---|
368 |
|
---|
369 | printf("----------------------------------------------------\n");
|
---|
370 |
|
---|
371 | /*
|
---|
372 | * Do as mom told us: always clean up after yourself.
|
---|
373 | */
|
---|
374 |
|
---|
375 | g_pVBoxFuncs->pfnComUninitialize();
|
---|
376 | VBoxCGlueTerm();
|
---|
377 | printf("Finished Main\n");
|
---|
378 |
|
---|
379 | return 0;
|
---|
380 | }
|
---|
381 | /* vim: set ts=4 sw=4 et: */
|
---|