1 | /* $Revision: 44529 $ */
|
---|
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-2010 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 | #include <unistd.h>
|
---|
28 | #include <signal.h>
|
---|
29 | #include <sys/poll.h>
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Internal Functions *
|
---|
33 | *******************************************************************************/
|
---|
34 | static void listVMs(IVirtualBox *virtualBox, ISession *session, nsIEventQueue *queue);
|
---|
35 | static void registerCallBack(IVirtualBox *virtualBox, ISession *session, PRUnichar *machineId, nsIEventQueue *queue);
|
---|
36 | static void startVM(IVirtualBox *virtualBox, ISession *session, PRUnichar *id, nsIEventQueue *queue);
|
---|
37 |
|
---|
38 | /*******************************************************************************
|
---|
39 | * Global Variables *
|
---|
40 | *******************************************************************************/
|
---|
41 | /** Set by signal handler. */
|
---|
42 | static volatile int g_fStop = 0;
|
---|
43 |
|
---|
44 | int volatile g_refcount = 0;
|
---|
45 |
|
---|
46 | /* #define for printing nsID type UUID's */
|
---|
47 |
|
---|
48 | #define printUUID(iid) \
|
---|
49 | {\
|
---|
50 | printf(#iid ": {%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",\
|
---|
51 | (unsigned)(iid)->m0,\
|
---|
52 | (unsigned)(iid)->m1,\
|
---|
53 | (unsigned)(iid)->m2,\
|
---|
54 | (unsigned)(iid)->m3[0],\
|
---|
55 | (unsigned)(iid)->m3[1],\
|
---|
56 | (unsigned)(iid)->m3[2],\
|
---|
57 | (unsigned)(iid)->m3[3],\
|
---|
58 | (unsigned)(iid)->m3[4],\
|
---|
59 | (unsigned)(iid)->m3[5],\
|
---|
60 | (unsigned)(iid)->m3[6],\
|
---|
61 | (unsigned)(iid)->m3[7]);\
|
---|
62 | }\
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Callback functions
|
---|
66 | */
|
---|
67 | static const char *GetStateName(PRUint32 machineState)
|
---|
68 | {
|
---|
69 | switch (machineState)
|
---|
70 | {
|
---|
71 | case MachineState_Null: return "<null>";
|
---|
72 | case MachineState_PoweredOff: return "PoweredOff";
|
---|
73 | case MachineState_Saved: return "Saved";
|
---|
74 | case MachineState_Teleported: return "Teleported";
|
---|
75 | case MachineState_Aborted: return "Aborted";
|
---|
76 | case MachineState_Running: return "Running";
|
---|
77 | case MachineState_Teleporting: return "Teleporting";
|
---|
78 | case MachineState_LiveSnapshotting: return "LiveSnapshotting";
|
---|
79 | case MachineState_Paused: return "Paused";
|
---|
80 | case MachineState_Stuck: return "Stuck";
|
---|
81 | case MachineState_Starting: return "Starting";
|
---|
82 | case MachineState_Stopping: return "Stopping";
|
---|
83 | case MachineState_Saving: return "Saving";
|
---|
84 | case MachineState_Restoring: return "Restoring";
|
---|
85 | case MachineState_TeleportingPausedVM: return "TeleportingPausedVM";
|
---|
86 | case MachineState_TeleportingIn: return "TeleportingIn";
|
---|
87 | case MachineState_Discarding: return "Discarding";
|
---|
88 | case MachineState_SettingUp: return "SettingUp";
|
---|
89 | default: return "no idea";
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | static nsresult OnMousePointerShapeChange(
|
---|
94 | IConsoleCallback *pThis,
|
---|
95 | PRBool visible,
|
---|
96 | PRBool alpha,
|
---|
97 | PRUint32 xHot,
|
---|
98 | PRUint32 yHot,
|
---|
99 | PRUint32 width,
|
---|
100 | PRUint32 height,
|
---|
101 | PRUint8 * shape
|
---|
102 | ) {
|
---|
103 | printf("OnMousePointerShapeChange\n");
|
---|
104 | return 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 | static nsresult OnMouseCapabilityChange(
|
---|
108 | IConsoleCallback *pThis,
|
---|
109 | PRBool supportsAbsolute,
|
---|
110 | PRBool needsHostCursor
|
---|
111 | ) {
|
---|
112 | printf("OnMouseCapabilityChange\n");
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 |
|
---|
116 | static nsresult OnKeyboardLedsChange(
|
---|
117 | IConsoleCallback *pThis,
|
---|
118 | PRBool numLock,
|
---|
119 | PRBool capsLock,
|
---|
120 | PRBool scrollLock
|
---|
121 | ) {
|
---|
122 | printf("OnMouseCapabilityChange\n");
|
---|
123 | return 0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | static nsresult OnStateChange(
|
---|
127 | IConsoleCallback *pThis,
|
---|
128 | PRUint32 state
|
---|
129 | ) {
|
---|
130 | printf("OnStateChange: %s\n", GetStateName(state));
|
---|
131 | fflush(stdout);
|
---|
132 | if ( state == MachineState_PoweredOff
|
---|
133 | || state == MachineState_Saved
|
---|
134 | || state == MachineState_Teleported
|
---|
135 | || state == MachineState_Aborted
|
---|
136 | )
|
---|
137 | g_fStop = 1;
|
---|
138 | return 0;
|
---|
139 | }
|
---|
140 |
|
---|
141 | static nsresult OnAdditionsStateChange(IConsoleCallback *pThis )
|
---|
142 | {
|
---|
143 | printf("OnAdditionsStateChange\n");
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | static nsresult OnNetworkAdapterChange(
|
---|
148 | IConsoleCallback *pThis,
|
---|
149 | INetworkAdapter * networkAdapter
|
---|
150 | ) {
|
---|
151 | printf("OnNetworkAdapterChange\n");
|
---|
152 | return 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 | static nsresult OnSerialPortChange(
|
---|
156 | IConsoleCallback *pThis,
|
---|
157 | ISerialPort * serialPort
|
---|
158 | ) {
|
---|
159 | printf("OnSerialPortChange\n");
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 |
|
---|
163 | static nsresult OnParallelPortChange(
|
---|
164 | IConsoleCallback *pThis,
|
---|
165 | IParallelPort * parallelPort
|
---|
166 | ) {
|
---|
167 | printf("OnParallelPortChange\n");
|
---|
168 | return 0;
|
---|
169 | }
|
---|
170 |
|
---|
171 | static nsresult OnStorageControllerChange(IConsoleCallback *pThis)
|
---|
172 | {
|
---|
173 | printf("OnStorageControllerChange\n");
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | static nsresult OnMediumChange(IConsoleCallback *pThis,
|
---|
178 | IMediumAttachment *mediumAttachment)
|
---|
179 | {
|
---|
180 | printf("OnMediumChange\n");
|
---|
181 | return 0;
|
---|
182 | }
|
---|
183 |
|
---|
184 | static nsresult OnVRDPServerChange(IConsoleCallback *pThis )
|
---|
185 | {
|
---|
186 | printf("OnVRDPServerChange\n");
|
---|
187 | return 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | static nsresult OnUSBControllerChange(IConsoleCallback *pThis )
|
---|
191 | {
|
---|
192 | printf("OnUSBControllerChange\n");
|
---|
193 | return 0;
|
---|
194 | }
|
---|
195 |
|
---|
196 | static nsresult OnUSBDeviceStateChange(
|
---|
197 | IConsoleCallback *pThis,
|
---|
198 | IUSBDevice * device,
|
---|
199 | PRBool attached,
|
---|
200 | IVirtualBoxErrorInfo * error
|
---|
201 | ) {
|
---|
202 | printf("OnUSBDeviceStateChange\n");
|
---|
203 | return 0;
|
---|
204 | }
|
---|
205 |
|
---|
206 | static nsresult OnSharedFolderChange(
|
---|
207 | IConsoleCallback *pThis,
|
---|
208 | PRUint32 scope
|
---|
209 | ) {
|
---|
210 | printf("OnSharedFolderChange\n");
|
---|
211 | return 0;
|
---|
212 | }
|
---|
213 |
|
---|
214 | static nsresult OnRuntimeError(
|
---|
215 | IConsoleCallback *pThis,
|
---|
216 | PRBool fatal,
|
---|
217 | PRUnichar * id,
|
---|
218 | PRUnichar * message
|
---|
219 | ) {
|
---|
220 | printf("OnRuntimeError\n");
|
---|
221 | return 0;
|
---|
222 | }
|
---|
223 |
|
---|
224 | static nsresult OnCanShowWindow(
|
---|
225 | IConsoleCallback *pThis,
|
---|
226 | PRBool * canShow
|
---|
227 | ) {
|
---|
228 | printf("OnCanShowWindow\n");
|
---|
229 | return 0;
|
---|
230 | }
|
---|
231 |
|
---|
232 | static nsresult OnShowWindow(
|
---|
233 | IConsoleCallback *pThis,
|
---|
234 | PRUint64 * winId
|
---|
235 | ) {
|
---|
236 | printf("OnShowWindow\n");
|
---|
237 | return 0;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | static nsresult AddRef(nsISupports *pThis)
|
---|
242 | {
|
---|
243 | nsresult c;
|
---|
244 |
|
---|
245 | c = ++g_refcount;
|
---|
246 | printf("AddRef: %d\n", c);
|
---|
247 | return c;
|
---|
248 | }
|
---|
249 |
|
---|
250 | static nsresult Release(nsISupports *pThis)
|
---|
251 | {
|
---|
252 | nsresult c;
|
---|
253 |
|
---|
254 | c = --g_refcount;
|
---|
255 | printf("Release: %d\n", c);
|
---|
256 | if (c == 0)
|
---|
257 | {
|
---|
258 | /* delete object */
|
---|
259 | free(pThis->vtbl);
|
---|
260 | free(pThis);
|
---|
261 | }
|
---|
262 | return c;
|
---|
263 | }
|
---|
264 |
|
---|
265 | static nsresult QueryInterface(nsISupports *pThis, const nsID *iid, void **resultp)
|
---|
266 | {
|
---|
267 | static const nsID ivirtualboxCallbackUUID = IVIRTUALBOXCALLBACK_IID;
|
---|
268 | static const nsID isupportIID = NS_ISUPPORTS_IID;
|
---|
269 |
|
---|
270 | /* match iid */
|
---|
271 | if ( memcmp(iid, &ivirtualboxCallbackUUID, sizeof(nsID)) == 0
|
---|
272 | || memcmp(iid, &isupportIID, sizeof(nsID)) == 0)
|
---|
273 | {
|
---|
274 | ++g_refcount;
|
---|
275 | printf("QueryInterface: %d\n", g_refcount);
|
---|
276 | *resultp = pThis;
|
---|
277 | return NS_OK;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /* printf("vboxCallback QueryInterface didn't find a matching interface\n"); */
|
---|
281 | printUUID(iid);
|
---|
282 | printUUID(&ivirtualboxCallbackUUID);
|
---|
283 | return NS_NOINTERFACE;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Signal callback.
|
---|
288 | *
|
---|
289 | * @param iSig The signal number (ignored).
|
---|
290 | */
|
---|
291 | static void sigIntHandler(int iSig)
|
---|
292 | {
|
---|
293 | printf("sigIntHandler\n");
|
---|
294 | (void)iSig;
|
---|
295 | g_fStop = 1;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * Register callback functions for the selected VM.
|
---|
300 | *
|
---|
301 | * @param virtualBox ptr to IVirtualBox object
|
---|
302 | * @param session ptr to ISession object
|
---|
303 | * @param id identifies the machine to start
|
---|
304 | * @param queue handle to the event queue
|
---|
305 | */
|
---|
306 | static void registerCallBack(IVirtualBox *virtualBox, ISession *session, PRUnichar *machineId, nsIEventQueue *queue)
|
---|
307 | {
|
---|
308 | IConsole *console = NULL;
|
---|
309 | nsresult rc;
|
---|
310 |
|
---|
311 | rc = session->vtbl->GetConsole(session, &console);
|
---|
312 | if ((NS_SUCCEEDED(rc)) && console)
|
---|
313 | {
|
---|
314 | IConsoleCallback *consoleCallback = NULL;
|
---|
315 |
|
---|
316 | consoleCallback = calloc(1, sizeof(IConsoleCallback));
|
---|
317 | consoleCallback->vtbl = calloc(1, sizeof(struct IConsoleCallback_vtbl));
|
---|
318 |
|
---|
319 | if (consoleCallback && consoleCallback->vtbl)
|
---|
320 | {
|
---|
321 | consoleCallback->vtbl->nsisupports.AddRef = &AddRef;
|
---|
322 | consoleCallback->vtbl->nsisupports.Release = &Release;
|
---|
323 | consoleCallback->vtbl->nsisupports.QueryInterface = &QueryInterface;
|
---|
324 | consoleCallback->vtbl->OnMousePointerShapeChange = &OnMousePointerShapeChange;
|
---|
325 | consoleCallback->vtbl->OnMouseCapabilityChange = &OnMouseCapabilityChange;
|
---|
326 | consoleCallback->vtbl->OnKeyboardLedsChange =&OnKeyboardLedsChange;
|
---|
327 | consoleCallback->vtbl->OnStateChange = &OnStateChange;
|
---|
328 | consoleCallback->vtbl->OnAdditionsStateChange = &OnAdditionsStateChange;
|
---|
329 | consoleCallback->vtbl->OnNetworkAdapterChange = &OnNetworkAdapterChange;
|
---|
330 | consoleCallback->vtbl->OnSerialPortChange = &OnSerialPortChange;
|
---|
331 | consoleCallback->vtbl->OnParallelPortChange = &OnParallelPortChange;
|
---|
332 | consoleCallback->vtbl->OnStorageControllerChange = &OnStorageControllerChange;
|
---|
333 | consoleCallback->vtbl->OnMediumChange = &OnMediumChange;
|
---|
334 | consoleCallback->vtbl->OnVRDPServerChange = &OnVRDPServerChange;
|
---|
335 | consoleCallback->vtbl->OnUSBControllerChange = &OnUSBControllerChange;
|
---|
336 | consoleCallback->vtbl->OnUSBDeviceStateChange = &OnUSBDeviceStateChange;
|
---|
337 | consoleCallback->vtbl->OnSharedFolderChange = &OnSharedFolderChange;
|
---|
338 | consoleCallback->vtbl->OnRuntimeError = &OnRuntimeError;
|
---|
339 | consoleCallback->vtbl->OnCanShowWindow = &OnCanShowWindow;
|
---|
340 | consoleCallback->vtbl->OnShowWindow = &OnShowWindow;
|
---|
341 | g_refcount = 1;
|
---|
342 |
|
---|
343 | rc = console->vtbl->RegisterCallback(console, consoleCallback);
|
---|
344 | if (NS_SUCCEEDED(rc))
|
---|
345 | {
|
---|
346 | /* crude way to show how it works, but any
|
---|
347 | * great ideas anyone?
|
---|
348 | */
|
---|
349 | PRInt32 fd;
|
---|
350 | int ret;
|
---|
351 |
|
---|
352 | printf("Entering event loop, PowerOff the machine to exit or press Ctrl-C to terminate\n");
|
---|
353 | fflush(stdout);
|
---|
354 | signal(SIGINT, sigIntHandler);
|
---|
355 |
|
---|
356 | fd = queue->vtbl->GetEventQueueSelectFD(queue);
|
---|
357 | if (fd >= 0)
|
---|
358 | {
|
---|
359 | while (!g_fStop)
|
---|
360 | {
|
---|
361 | struct pollfd pfd;
|
---|
362 |
|
---|
363 | pfd.fd = fd;
|
---|
364 | pfd.events = POLLIN | POLLERR | POLLHUP;
|
---|
365 | pfd.revents = 0;
|
---|
366 |
|
---|
367 | ret = poll(&pfd, 1, 250);
|
---|
368 |
|
---|
369 | if (ret <= 0)
|
---|
370 | continue;
|
---|
371 |
|
---|
372 | if (pfd.revents & POLLHUP)
|
---|
373 | g_fStop = 1;
|
---|
374 |
|
---|
375 | queue->vtbl->ProcessPendingEvents(queue);
|
---|
376 | }
|
---|
377 | }
|
---|
378 | else
|
---|
379 | {
|
---|
380 | while (!g_fStop)
|
---|
381 | {
|
---|
382 | PLEvent *pEvent = NULL;
|
---|
383 | rc = queue->vtbl->WaitForEvent(queue, &pEvent);
|
---|
384 | /*printf("event: %p rc=%x\n", (void *)pEvent, rc);*/
|
---|
385 | if (NS_SUCCEEDED(rc))
|
---|
386 | queue->vtbl->HandleEvent(queue, pEvent);
|
---|
387 | }
|
---|
388 | }
|
---|
389 | signal(SIGINT, SIG_DFL);
|
---|
390 | }
|
---|
391 | console->vtbl->UnregisterCallback(console, consoleCallback);
|
---|
392 | consoleCallback->vtbl->nsisupports.Release((nsISupports *)consoleCallback);
|
---|
393 | }
|
---|
394 | else
|
---|
395 | {
|
---|
396 | printf("Failed while allocating memory for console Callback.\n");
|
---|
397 | }
|
---|
398 | }
|
---|
399 | }
|
---|
400 |
|
---|
401 | /**
|
---|
402 | * List the registered VMs.
|
---|
403 | *
|
---|
404 | * @param virtualBox ptr to IVirtualBox object
|
---|
405 | * @param session ptr to ISession object
|
---|
406 | * @param queue handle to the event queue
|
---|
407 | */
|
---|
408 | static void listVMs(IVirtualBox *virtualBox, ISession *session, nsIEventQueue *queue)
|
---|
409 | {
|
---|
410 | nsresult rc;
|
---|
411 | IMachine **machines = NULL;
|
---|
412 | PRUint32 machineCnt = 0;
|
---|
413 | PRUint32 i;
|
---|
414 | unsigned start_id;
|
---|
415 |
|
---|
416 | /*
|
---|
417 | * Get the list of all registered VMs.
|
---|
418 | */
|
---|
419 |
|
---|
420 | rc = virtualBox->vtbl->GetMachines(virtualBox, &machineCnt, &machines);
|
---|
421 | if (NS_FAILED(rc))
|
---|
422 | {
|
---|
423 | fprintf(stderr, "could not get list of machines, rc=%08x\n",
|
---|
424 | (unsigned)rc);
|
---|
425 | return;
|
---|
426 | }
|
---|
427 |
|
---|
428 | if (machineCnt == 0)
|
---|
429 | {
|
---|
430 | printf("\tNo VMs\n");
|
---|
431 | return;
|
---|
432 | }
|
---|
433 |
|
---|
434 | printf("VM List:\n\n");
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * Iterate through the collection.
|
---|
438 | */
|
---|
439 |
|
---|
440 | for (i = 0; i < machineCnt; ++i)
|
---|
441 | {
|
---|
442 | IMachine *machine = machines[i];
|
---|
443 | PRBool isAccessible = PR_FALSE;
|
---|
444 |
|
---|
445 | printf("\tMachine #%u\n", (unsigned)i);
|
---|
446 |
|
---|
447 | if (!machine)
|
---|
448 | {
|
---|
449 | printf("\t(skipped, NULL)\n");
|
---|
450 | continue;
|
---|
451 | }
|
---|
452 |
|
---|
453 | machine->vtbl->GetAccessible(machine, &isAccessible);
|
---|
454 |
|
---|
455 | if (isAccessible)
|
---|
456 | {
|
---|
457 | PRUnichar *machineNameUtf16;
|
---|
458 | char *machineName;
|
---|
459 |
|
---|
460 | machine->vtbl->GetName(machine, &machineNameUtf16);
|
---|
461 | g_pVBoxFuncs->pfnUtf16ToUtf8(machineNameUtf16,&machineName);
|
---|
462 | printf("\tName: %s\n", machineName);
|
---|
463 |
|
---|
464 | g_pVBoxFuncs->pfnUtf8Free(machineName);
|
---|
465 | g_pVBoxFuncs->pfnComUnallocMem(machineNameUtf16);
|
---|
466 | }
|
---|
467 | else
|
---|
468 | {
|
---|
469 | printf("\tName: <inaccessible>\n");
|
---|
470 | }
|
---|
471 |
|
---|
472 | {
|
---|
473 | PRUnichar *uuidUtf16 = NULL;
|
---|
474 | char *uuidUtf8 = NULL;
|
---|
475 |
|
---|
476 | machine->vtbl->GetId(machine, &uuidUtf16);
|
---|
477 | g_pVBoxFuncs->pfnUtf16ToUtf8(uuidUtf16, &uuidUtf8);
|
---|
478 | printf("\tUUID: %s\n", uuidUtf8);
|
---|
479 |
|
---|
480 | g_pVBoxFuncs->pfnUtf8Free(uuidUtf8);
|
---|
481 | g_pVBoxFuncs->pfnUtf16Free(uuidUtf16);
|
---|
482 | }
|
---|
483 |
|
---|
484 | if (isAccessible)
|
---|
485 | {
|
---|
486 | {
|
---|
487 | PRUnichar *configFile;
|
---|
488 | char *configFile1 = calloc((size_t)64, (size_t)1);
|
---|
489 |
|
---|
490 | machine->vtbl->GetSettingsFilePath(machine, &configFile);
|
---|
491 | g_pVBoxFuncs->pfnUtf16ToUtf8(configFile, &configFile1);
|
---|
492 | printf("\tConfig file: %s\n", configFile1);
|
---|
493 |
|
---|
494 | free(configFile1);
|
---|
495 | g_pVBoxFuncs->pfnComUnallocMem(configFile);
|
---|
496 | }
|
---|
497 |
|
---|
498 | {
|
---|
499 | PRUint32 memorySize;
|
---|
500 |
|
---|
501 | machine->vtbl->GetMemorySize(machine, &memorySize);
|
---|
502 | printf("\tMemory size: %uMB\n", memorySize);
|
---|
503 | }
|
---|
504 |
|
---|
505 | {
|
---|
506 | PRUnichar *typeId;
|
---|
507 | PRUnichar *osNameUtf16;
|
---|
508 | char *osName;
|
---|
509 | IGuestOSType *osType = NULL;
|
---|
510 |
|
---|
511 | machine->vtbl->GetOSTypeId(machine, &typeId);
|
---|
512 | virtualBox->vtbl->GetGuestOSType(virtualBox, typeId, &osType);
|
---|
513 | osType->vtbl->GetDescription(osType, &osNameUtf16);
|
---|
514 | g_pVBoxFuncs->pfnUtf16ToUtf8(osNameUtf16,&osName);
|
---|
515 | printf("\tGuest OS: %s\n\n", osName);
|
---|
516 |
|
---|
517 | osType->vtbl->nsisupports.Release((void *)osType);
|
---|
518 | g_pVBoxFuncs->pfnUtf8Free(osName);
|
---|
519 | g_pVBoxFuncs->pfnComUnallocMem(osNameUtf16);
|
---|
520 | g_pVBoxFuncs->pfnComUnallocMem(typeId);
|
---|
521 | }
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | /*
|
---|
526 | * Let the user chose a machine to start.
|
---|
527 | */
|
---|
528 |
|
---|
529 | printf("Type Machine# to start (0 - %u) or 'quit' to do nothing: ",
|
---|
530 | (unsigned)(machineCnt - 1));
|
---|
531 | fflush(stdout);
|
---|
532 |
|
---|
533 | if (scanf("%u", &start_id) == 1 && start_id < machineCnt)
|
---|
534 | {
|
---|
535 | IMachine *machine = machines[start_id];
|
---|
536 |
|
---|
537 | if (machine)
|
---|
538 | {
|
---|
539 | PRUnichar *uuidUtf16 = NULL;
|
---|
540 |
|
---|
541 | machine->vtbl->GetId(machine, &uuidUtf16);
|
---|
542 | startVM(virtualBox, session, uuidUtf16, queue);
|
---|
543 |
|
---|
544 | g_pVBoxFuncs->pfnUtf16Free(uuidUtf16);
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | /*
|
---|
549 | * Don't forget to release the objects in the array.
|
---|
550 | */
|
---|
551 |
|
---|
552 | for (i = 0; i < machineCnt; ++i)
|
---|
553 | {
|
---|
554 | IMachine *machine = machines[i];
|
---|
555 |
|
---|
556 | if (machine)
|
---|
557 | {
|
---|
558 | machine->vtbl->nsisupports.Release((nsISupports *)machine);
|
---|
559 | }
|
---|
560 | }
|
---|
561 | }
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * Start a VM.
|
---|
565 | *
|
---|
566 | * @param virtualBox ptr to IVirtualBox object
|
---|
567 | * @param session ptr to ISession object
|
---|
568 | * @param id identifies the machine to start
|
---|
569 | * @param queue handle to the event queue
|
---|
570 | */
|
---|
571 |
|
---|
572 | static void startVM(IVirtualBox *virtualBox, ISession *session, PRUnichar *id, nsIEventQueue *queue)
|
---|
573 | {
|
---|
574 | nsresult rc;
|
---|
575 | IMachine *machine = NULL;
|
---|
576 | IProgress *progress = NULL;
|
---|
577 | PRUnichar *env = NULL;
|
---|
578 | PRUnichar *sessionType;
|
---|
579 |
|
---|
580 | rc = virtualBox->vtbl->GetMachine(virtualBox, id, &machine);
|
---|
581 |
|
---|
582 | if (NS_FAILED(rc) || !machine)
|
---|
583 | {
|
---|
584 | fprintf(stderr, "Error: Couldn't get the machine handle.\n");
|
---|
585 | return;
|
---|
586 | }
|
---|
587 |
|
---|
588 | g_pVBoxFuncs->pfnUtf8ToUtf16("gui", &sessionType);
|
---|
589 |
|
---|
590 | rc = virtualBox->vtbl->OpenRemoteSession(
|
---|
591 | virtualBox,
|
---|
592 | session,
|
---|
593 | id,
|
---|
594 | sessionType,
|
---|
595 | env,
|
---|
596 | &progress
|
---|
597 | );
|
---|
598 |
|
---|
599 | g_pVBoxFuncs->pfnUtf16Free(sessionType);
|
---|
600 |
|
---|
601 | if (NS_FAILED(rc))
|
---|
602 | {
|
---|
603 | fprintf(stderr, "Error: OpenRemoteSession failed.\n");
|
---|
604 | }
|
---|
605 | else
|
---|
606 | {
|
---|
607 | PRBool completed;
|
---|
608 | PRInt32 resultCode;
|
---|
609 |
|
---|
610 | printf("Waiting for the remote session to open...\n");
|
---|
611 | progress->vtbl->WaitForCompletion(progress, -1);
|
---|
612 |
|
---|
613 | rc = progress->vtbl->GetCompleted(progress, &completed);
|
---|
614 | if (NS_FAILED(rc))
|
---|
615 | {
|
---|
616 | fprintf (stderr, "Error: GetCompleted status failed.\n");
|
---|
617 | }
|
---|
618 |
|
---|
619 | progress->vtbl->GetResultCode(progress, &resultCode);
|
---|
620 | if (NS_FAILED(resultCode))
|
---|
621 | {
|
---|
622 | IVirtualBoxErrorInfo *errorInfo;
|
---|
623 | PRUnichar *textUtf16;
|
---|
624 | char *text;
|
---|
625 |
|
---|
626 | progress->vtbl->GetErrorInfo(progress, &errorInfo);
|
---|
627 | errorInfo->vtbl->GetText(errorInfo, &textUtf16);
|
---|
628 | g_pVBoxFuncs->pfnUtf16ToUtf8(textUtf16, &text);
|
---|
629 | printf("Error: %s\n", text);
|
---|
630 |
|
---|
631 | g_pVBoxFuncs->pfnComUnallocMem(textUtf16);
|
---|
632 | g_pVBoxFuncs->pfnUtf8Free(text);
|
---|
633 | }
|
---|
634 | else
|
---|
635 | {
|
---|
636 | fprintf(stderr, "Remote session has been successfully opened.\n");
|
---|
637 | registerCallBack(virtualBox, session, id, queue);
|
---|
638 | }
|
---|
639 | progress->vtbl->nsisupports.Release((void *)progress);
|
---|
640 | }
|
---|
641 |
|
---|
642 | /* It's important to always release resources. */
|
---|
643 | machine->vtbl->nsisupports.Release((void *)machine);
|
---|
644 | }
|
---|
645 |
|
---|
646 | /* Main - Start the ball rolling. */
|
---|
647 |
|
---|
648 | int main(int argc, char **argv)
|
---|
649 | {
|
---|
650 | IVirtualBox *vbox = NULL;
|
---|
651 | ISession *session = NULL;
|
---|
652 | nsIEventQueue *queue = NULL;
|
---|
653 | PRUint32 revision = 0;
|
---|
654 | PRUnichar *versionUtf16 = NULL;
|
---|
655 | PRUnichar *homefolderUtf16 = NULL;
|
---|
656 | nsresult rc; /* Result code of various function (method) calls. */
|
---|
657 |
|
---|
658 | printf("Starting Main\n");
|
---|
659 |
|
---|
660 | /*
|
---|
661 | * VBoxComInitialize does all the necessary startup action and
|
---|
662 | * provides us with pointers to vbox and session handles.
|
---|
663 | * It should be matched by a call to VBoxComUninitialize(vbox)
|
---|
664 | * when done.
|
---|
665 | */
|
---|
666 |
|
---|
667 | if (VBoxCGlueInit() != 0)
|
---|
668 | {
|
---|
669 | fprintf(stderr, "%s: FATAL: VBoxCGlueInit failed: %s\n",
|
---|
670 | argv[0], g_szVBoxErrMsg);
|
---|
671 | return EXIT_FAILURE;
|
---|
672 | }
|
---|
673 |
|
---|
674 | g_pVBoxFuncs->pfnComInitialize(IVIRTUALBOX_IID_STR, &vbox,
|
---|
675 | ISESSION_IID_STR, &session);
|
---|
676 | if (vbox == NULL)
|
---|
677 | {
|
---|
678 | fprintf(stderr, "%s: FATAL: could not get vbox handle\n", argv[0]);
|
---|
679 | return EXIT_FAILURE;
|
---|
680 | }
|
---|
681 | if (session == NULL)
|
---|
682 | {
|
---|
683 | fprintf(stderr, "%s: FATAL: could not get session handle\n", argv[0]);
|
---|
684 | return EXIT_FAILURE;
|
---|
685 | }
|
---|
686 | g_pVBoxFuncs->pfnGetEventQueue(&queue);
|
---|
687 | printf("Got the event queue: %p\n", (void *)queue);
|
---|
688 |
|
---|
689 | /*
|
---|
690 | * Now ask for revision, version and home folder information of
|
---|
691 | * this vbox. Were not using fancy macros here so it
|
---|
692 | * remains easy to see how we access C++'s vtable.
|
---|
693 | */
|
---|
694 |
|
---|
695 | printf("----------------------------------------------------\n");
|
---|
696 |
|
---|
697 | /* 1. Revision */
|
---|
698 |
|
---|
699 | rc = vbox->vtbl->GetRevision(vbox, &revision);
|
---|
700 | if (NS_SUCCEEDED(rc))
|
---|
701 | {
|
---|
702 | printf("\tRevision: %u\n", revision);
|
---|
703 | }
|
---|
704 | else
|
---|
705 | {
|
---|
706 | fprintf(stderr, "%s: GetRevision() returned %08x\n",
|
---|
707 | argv[0], (unsigned)rc);
|
---|
708 | }
|
---|
709 |
|
---|
710 | /* 2. Version */
|
---|
711 |
|
---|
712 | rc = vbox->vtbl->GetVersion(vbox, &versionUtf16);
|
---|
713 | if (NS_SUCCEEDED(rc))
|
---|
714 | {
|
---|
715 | char *version = NULL;
|
---|
716 | g_pVBoxFuncs->pfnUtf16ToUtf8(versionUtf16, &version);
|
---|
717 | printf("\tVersion: %s\n", version);
|
---|
718 | g_pVBoxFuncs->pfnUtf8Free(version);
|
---|
719 | g_pVBoxFuncs->pfnComUnallocMem(versionUtf16);
|
---|
720 | }
|
---|
721 | else
|
---|
722 | {
|
---|
723 | fprintf(stderr, "%s: GetVersion() returned %08x\n",
|
---|
724 | argv[0], (unsigned)rc);
|
---|
725 | }
|
---|
726 |
|
---|
727 | /* 3. Home Folder */
|
---|
728 |
|
---|
729 | rc = vbox->vtbl->GetHomeFolder(vbox, &homefolderUtf16);
|
---|
730 | if (NS_SUCCEEDED(rc))
|
---|
731 | {
|
---|
732 | char *homefolder = NULL;
|
---|
733 | g_pVBoxFuncs->pfnUtf16ToUtf8(homefolderUtf16, &homefolder);
|
---|
734 | printf("\tHomeFolder: %s\n", homefolder);
|
---|
735 | g_pVBoxFuncs->pfnUtf8Free(homefolder);
|
---|
736 | g_pVBoxFuncs->pfnComUnallocMem(homefolderUtf16);
|
---|
737 | }
|
---|
738 | else
|
---|
739 | {
|
---|
740 | fprintf(stderr, "%s: GetHomeFolder() returned %08x\n",
|
---|
741 | argv[0], (unsigned)rc);
|
---|
742 | }
|
---|
743 |
|
---|
744 | listVMs(vbox, session, queue);
|
---|
745 | session->vtbl->Close(session);
|
---|
746 |
|
---|
747 | printf("----------------------------------------------------\n");
|
---|
748 |
|
---|
749 | /*
|
---|
750 | * Do as mom told us: always clean up after yourself.
|
---|
751 | */
|
---|
752 |
|
---|
753 | g_pVBoxFuncs->pfnComUninitialize();
|
---|
754 | VBoxCGlueTerm();
|
---|
755 | printf("Finished Main\n");
|
---|
756 |
|
---|
757 | return 0;
|
---|
758 | }
|
---|
759 | /* vim: set ts=4 sw=4 et: */
|
---|