1 | /** @file
|
---|
2 | *
|
---|
3 | * tstVBoxAPILinux - sample program to illustrate the VirtualBox
|
---|
4 | * XPCOM API for machine management on Linux.
|
---|
5 | * It only uses standard C/C++ and XPCOM semantics,
|
---|
6 | * no additional VBox classes/macros/helpers.
|
---|
7 | */
|
---|
8 |
|
---|
9 | /*
|
---|
10 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
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 |
|
---|
21 | /*
|
---|
22 | * PURPOSE OF THIS SAMPLE PROGRAM
|
---|
23 | * ------------------------------
|
---|
24 | *
|
---|
25 | * This sample program is intended to demonstrate the minimal code necessary
|
---|
26 | * to use VirtualBox XPCOM API for learning puroses only. The program uses
|
---|
27 | * pure XPCOM and doesn't have any extra dependencies to let you better
|
---|
28 | * understand what is going on when a client talks to the VirtualBox core
|
---|
29 | * using the XPCOM framework.
|
---|
30 | *
|
---|
31 | * However, if you want to write a real application, it is highly recommended
|
---|
32 | * to use our MS COM XPCOM Glue library and helper C++ classes. This way, you
|
---|
33 | * will get at least the following benefits:
|
---|
34 | *
|
---|
35 | * a) better portability: both the MS COM (used on Windows) and XPCOM (used
|
---|
36 | * everywhere else) VirtualBox client application from the same source code
|
---|
37 | * (including common smart C++ templates for automatic interface pointer
|
---|
38 | * reference counter and string data management);
|
---|
39 | * b) simpler XPCOM initialization and shutdown (only a signle method call
|
---|
40 | * that does everything right).
|
---|
41 | *
|
---|
42 | * Currently, there is no separate sample program that uses the VirtualBox MS
|
---|
43 | * COM XPCOM Glue library. Please refer to the sources of stock VirtualBox
|
---|
44 | * applications such as the VirtualBox GUI frontend or the VBoxManage command
|
---|
45 | * line frontend.
|
---|
46 | *
|
---|
47 | *
|
---|
48 | * RUNNING THIS SAMPLE PROGRAM
|
---|
49 | * ---------------------------
|
---|
50 | *
|
---|
51 | * This sample program needs to know where the VirtualBox core files reside
|
---|
52 | * and where to search for VirtualBox shared libraries. Therefore, you need to
|
---|
53 | * use the following (or similar) command to execute it:
|
---|
54 | *
|
---|
55 | * $ env VBOX_XPCOM_HOME=../../.. LD_LIBRARY_PATH=../../.. ./tstVBoxAPILinux
|
---|
56 | *
|
---|
57 | * The above command assumes that VBoxRT.so, VBoxXPCOM.so and others reside in
|
---|
58 | * the directory ../../..
|
---|
59 | */
|
---|
60 |
|
---|
61 |
|
---|
62 | #include <stdio.h>
|
---|
63 | #include <stdlib.h>
|
---|
64 | #include <iconv.h>
|
---|
65 | #include <errno.h>
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Include the XPCOM headers
|
---|
69 | */
|
---|
70 |
|
---|
71 | #if defined(XPCOM_GLUE)
|
---|
72 | #include <nsXPCOMGlue.h>
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | #include <nsMemory.h>
|
---|
76 | #include <nsString.h>
|
---|
77 | #include <nsIServiceManager.h>
|
---|
78 | #include <nsEventQueueUtils.h>
|
---|
79 |
|
---|
80 | #include <nsIExceptionService.h>
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * VirtualBox XPCOM interface. This header is generated
|
---|
84 | * from IDL which in turn is generated from a custom XML format.
|
---|
85 | */
|
---|
86 | #include "VirtualBox_XPCOM.h"
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Prototypes
|
---|
90 | */
|
---|
91 |
|
---|
92 | char *nsIDToString(nsID *guid);
|
---|
93 | void printErrorInfo();
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Display all registered VMs on the screen with some information about each
|
---|
98 | *
|
---|
99 | * @param virtualBox VirtualBox instance object.
|
---|
100 | */
|
---|
101 | void listVMs(IVirtualBox *virtualBox)
|
---|
102 | {
|
---|
103 | nsresult rc;
|
---|
104 |
|
---|
105 | printf("----------------------------------------------------\n");
|
---|
106 | printf("VM List:\n\n");
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Get the list of all registered VMs
|
---|
110 | */
|
---|
111 | IMachine **machines = NULL;
|
---|
112 | PRUint32 machineCnt = 0;
|
---|
113 |
|
---|
114 | rc = virtualBox->GetMachines(&machineCnt, &machines);
|
---|
115 | if (NS_SUCCEEDED(rc))
|
---|
116 | {
|
---|
117 | /*
|
---|
118 | * Iterate through the collection
|
---|
119 | */
|
---|
120 | for (PRUint32 i = 0; i < machineCnt; ++ i)
|
---|
121 | {
|
---|
122 | IMachine *machine = machines[i];
|
---|
123 | if (machine)
|
---|
124 | {
|
---|
125 | PRBool isAccessible = PR_FALSE;
|
---|
126 | machine->GetAccessible(&isAccessible);
|
---|
127 |
|
---|
128 | if (isAccessible)
|
---|
129 | {
|
---|
130 | nsXPIDLString machineName;
|
---|
131 | machine->GetName(getter_Copies(machineName));
|
---|
132 | char *machineNameAscii = ToNewCString(machineName);
|
---|
133 | printf("\tName: %s\n", machineNameAscii);
|
---|
134 | free(machineNameAscii);
|
---|
135 | }
|
---|
136 | else
|
---|
137 | {
|
---|
138 | printf("\tName: <inaccessible>\n");
|
---|
139 | }
|
---|
140 |
|
---|
141 | nsXPIDLString iid;
|
---|
142 | machine->GetId(getter_Copies(iid));
|
---|
143 | const char *uuidString = ToNewCString(iid);
|
---|
144 | printf("\tUUID: %s\n", uuidString);
|
---|
145 | free((void*)uuidString);
|
---|
146 |
|
---|
147 | if (isAccessible)
|
---|
148 | {
|
---|
149 | nsXPIDLString configFile;
|
---|
150 | machine->GetSettingsFilePath(getter_Copies(configFile));
|
---|
151 | char *configFileAscii = ToNewCString(configFile);
|
---|
152 | printf("\tConfig file: %s\n", configFileAscii);
|
---|
153 | free(configFileAscii);
|
---|
154 |
|
---|
155 | PRUint32 memorySize;
|
---|
156 | machine->GetMemorySize(&memorySize);
|
---|
157 | printf("\tMemory size: %uMB\n", memorySize);
|
---|
158 |
|
---|
159 | nsXPIDLString typeId;
|
---|
160 | machine->GetOSTypeId(getter_Copies(typeId));
|
---|
161 | IGuestOSType *osType = nsnull;
|
---|
162 | virtualBox->GetGuestOSType (typeId.get(), &osType);
|
---|
163 | nsXPIDLString osName;
|
---|
164 | osType->GetDescription(getter_Copies(osName));
|
---|
165 | char *osNameAscii = ToNewCString(osName);
|
---|
166 | printf("\tGuest OS: %s\n\n", osNameAscii);
|
---|
167 | free(osNameAscii);
|
---|
168 | osType->Release();
|
---|
169 | }
|
---|
170 |
|
---|
171 | /* don't forget to release the objects in the array... */
|
---|
172 | machine->Release();
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 | printf("----------------------------------------------------\n\n");
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Create a sample VM
|
---|
181 | *
|
---|
182 | * @param virtualBox VirtualBox instance object.
|
---|
183 | */
|
---|
184 | void createVM(IVirtualBox *virtualBox)
|
---|
185 | {
|
---|
186 | nsresult rc;
|
---|
187 | /*
|
---|
188 | * First create a unnamed new VM. It will be unconfigured and not be saved
|
---|
189 | * in the configuration until we explicitely choose to do so.
|
---|
190 | */
|
---|
191 | nsCOMPtr <IMachine> machine;
|
---|
192 | rc = virtualBox->CreateMachine(NS_LITERAL_STRING("A brand new name").get(),
|
---|
193 | nsnull, nsnull, nsnull, false, getter_AddRefs(machine));
|
---|
194 | if (NS_FAILED(rc))
|
---|
195 | {
|
---|
196 | printf("Error: could not create machine! rc=%08X\n", rc);
|
---|
197 | return;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Set some properties
|
---|
202 | */
|
---|
203 | /* alternative to illustrate the use of string classes */
|
---|
204 | rc = machine->SetName(NS_ConvertUTF8toUTF16("A new name").get());
|
---|
205 | rc = machine->SetMemorySize(128);
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Now a more advanced property -- the guest OS type. This is
|
---|
209 | * an object by itself which has to be found first. Note that we
|
---|
210 | * use the ID of the guest OS type here which is an internal
|
---|
211 | * representation (you can find that by configuring the OS type of
|
---|
212 | * a machine in the GUI and then looking at the <Guest ostype=""/>
|
---|
213 | * setting in the XML file. It is also possible to get the OS type from
|
---|
214 | * its description (win2k would be "Windows 2000") by getting the
|
---|
215 | * guest OS type collection and enumerating it.
|
---|
216 | */
|
---|
217 | nsCOMPtr <IGuestOSType> osType;
|
---|
218 | rc = virtualBox->GetGuestOSType(NS_LITERAL_STRING("win2k").get(),
|
---|
219 | getter_AddRefs(osType));
|
---|
220 | if (NS_FAILED(rc))
|
---|
221 | {
|
---|
222 | printf("Error: could not find guest OS type! rc=%08X\n", rc);
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | machine->SetOSTypeId (NS_LITERAL_STRING("win2k").get());
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Register the VM. Note that this call also saves the VM config
|
---|
231 | * to disk. It is also possible to save the VM settings but not
|
---|
232 | * register the VM.
|
---|
233 | *
|
---|
234 | * Also note that due to current VirtualBox limitations, the machine
|
---|
235 | * must be registered *before* we can attach hard disks to it.
|
---|
236 | */
|
---|
237 | rc = virtualBox->RegisterMachine(machine);
|
---|
238 | if (NS_FAILED(rc))
|
---|
239 | {
|
---|
240 | printf("Error: could not register machine! rc=%08X\n", rc);
|
---|
241 | printErrorInfo();
|
---|
242 | return;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * In order to manipulate the registered machine, we must open a session
|
---|
247 | * for that machine. Do it now.
|
---|
248 | */
|
---|
249 | nsCOMPtr<ISession> session;
|
---|
250 | {
|
---|
251 | nsCOMPtr<nsIComponentManager> manager;
|
---|
252 | rc = NS_GetComponentManager (getter_AddRefs (manager));
|
---|
253 | if (NS_FAILED(rc))
|
---|
254 | {
|
---|
255 | printf("Error: could not get component manager! rc=%08X\n", rc);
|
---|
256 | return;
|
---|
257 | }
|
---|
258 | rc = manager->CreateInstanceByContractID (NS_SESSION_CONTRACTID,
|
---|
259 | nsnull,
|
---|
260 | NS_GET_IID(ISession),
|
---|
261 | getter_AddRefs(session));
|
---|
262 | if (NS_FAILED(rc))
|
---|
263 | {
|
---|
264 | printf("Error, could not instantiate Session object! rc=0x%x\n", rc);
|
---|
265 | return;
|
---|
266 | }
|
---|
267 |
|
---|
268 | machine->LockMachine(session, LockType_Write);
|
---|
269 | if (NS_FAILED(rc))
|
---|
270 | {
|
---|
271 | printf("Error, could not open session! rc=0x%x\n", rc);
|
---|
272 | return;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * After the machine is registered, the initial machine object becomes
|
---|
277 | * immutable. In order to get a mutable machine object, we must query
|
---|
278 | * it from the opened session object.
|
---|
279 | */
|
---|
280 | rc = session->GetMachine(getter_AddRefs(machine));
|
---|
281 | if (NS_FAILED(rc))
|
---|
282 | {
|
---|
283 | printf("Error, could not get sessioned machine! rc=0x%x\n", rc);
|
---|
284 | return;
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * Create a virtual harddisk
|
---|
290 | */
|
---|
291 | nsCOMPtr<IMedium> hardDisk = 0;
|
---|
292 | rc = virtualBox->CreateHardDisk(NS_LITERAL_STRING("VDI").get(),
|
---|
293 | NS_LITERAL_STRING("TestHardDisk.vdi").get(),
|
---|
294 | getter_AddRefs(hardDisk));
|
---|
295 | if (NS_FAILED(rc))
|
---|
296 | {
|
---|
297 | printf("Failed creating a hard disk object! rc=%08X\n", rc);
|
---|
298 | }
|
---|
299 | else
|
---|
300 | {
|
---|
301 | /*
|
---|
302 | * We have only created an object so far. No on disk representation exists
|
---|
303 | * because none of its properties has been set so far. Let's continue creating
|
---|
304 | * a dynamically expanding image.
|
---|
305 | */
|
---|
306 | nsCOMPtr <IProgress> progress;
|
---|
307 | rc = hardDisk->CreateBaseStorage(100, // size in megabytes
|
---|
308 | MediumVariant_Standard,
|
---|
309 | getter_AddRefs(progress)); // optional progress object
|
---|
310 | if (NS_FAILED(rc))
|
---|
311 | {
|
---|
312 | printf("Failed creating hard disk image! rc=%08X\n", rc);
|
---|
313 | }
|
---|
314 | else
|
---|
315 | {
|
---|
316 | /*
|
---|
317 | * Creating the image is done in the background because it can take quite
|
---|
318 | * some time (at least fixed size images). We have to wait for its completion.
|
---|
319 | * Here we wait forever (timeout -1) which is potentially dangerous.
|
---|
320 | */
|
---|
321 | rc = progress->WaitForCompletion(-1);
|
---|
322 | PRInt32 resultCode;
|
---|
323 | progress->GetResultCode(&resultCode);
|
---|
324 | if (NS_FAILED(rc) || NS_FAILED(resultCode))
|
---|
325 | {
|
---|
326 | printf("Error: could not create hard disk! rc=%08X\n",
|
---|
327 | NS_FAILED(rc) ? rc : resultCode);
|
---|
328 | }
|
---|
329 | else
|
---|
330 | {
|
---|
331 | /*
|
---|
332 | * Now that it's created, we can assign it to the VM.
|
---|
333 | */
|
---|
334 | rc = machine->AttachDevice(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
|
---|
335 | 0, // channel number on the controller
|
---|
336 | 0, // device number on the controller
|
---|
337 | DeviceType_HardDisk,
|
---|
338 | hardDisk);
|
---|
339 | if (NS_FAILED(rc))
|
---|
340 | {
|
---|
341 | printf("Error: could not attach hard disk! rc=%08X\n", rc);
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | /*
|
---|
348 | * It's got a hard disk but that one is new and thus not bootable. Make it
|
---|
349 | * boot from an ISO file. This requires some processing. First the ISO file
|
---|
350 | * has to be registered and then mounted to the VM's DVD drive and selected
|
---|
351 | * as the boot device.
|
---|
352 | */
|
---|
353 | nsCOMPtr<IMedium> dvdImage;
|
---|
354 | rc = virtualBox->OpenMedium(NS_LITERAL_STRING("/home/vbox/isos/winnt4ger.iso").get(),
|
---|
355 | DeviceType_DVD,
|
---|
356 | AccessMode_ReadOnly,
|
---|
357 | getter_AddRefs(dvdImage));
|
---|
358 | if (NS_FAILED(rc))
|
---|
359 | printf("Error: could not open CD image! rc=%08X\n", rc);
|
---|
360 | else
|
---|
361 | {
|
---|
362 | /*
|
---|
363 | * Now assign it to our VM
|
---|
364 | */
|
---|
365 | rc = machine->MountMedium(NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
|
---|
366 | 2, // channel number on the controller
|
---|
367 | 0, // device number on the controller
|
---|
368 | dvdImage,
|
---|
369 | PR_FALSE); // aForce
|
---|
370 | if (NS_FAILED(rc))
|
---|
371 | {
|
---|
372 | printf("Error: could not mount ISO image! rc=%08X\n", rc);
|
---|
373 | }
|
---|
374 | else
|
---|
375 | {
|
---|
376 | /*
|
---|
377 | * Last step: tell the VM to boot from the CD.
|
---|
378 | */
|
---|
379 | rc = machine->SetBootOrder (1, DeviceType::DVD);
|
---|
380 | if (NS_FAILED(rc))
|
---|
381 | {
|
---|
382 | printf("Could not set boot device! rc=%08X\n", rc);
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | /*
|
---|
388 | * Save all changes we've just made.
|
---|
389 | */
|
---|
390 | rc = machine->SaveSettings();
|
---|
391 | if (NS_FAILED(rc))
|
---|
392 | {
|
---|
393 | printf("Could not save machine settings! rc=%08X\n", rc);
|
---|
394 | }
|
---|
395 |
|
---|
396 | /*
|
---|
397 | * It is always important to close the open session when it becomes not
|
---|
398 | * necessary any more.
|
---|
399 | */
|
---|
400 | session->UnlockMachine();
|
---|
401 | }
|
---|
402 |
|
---|
403 | // main
|
---|
404 | ///////////////////////////////////////////////////////////////////////////////
|
---|
405 |
|
---|
406 | int main(int argc, char *argv[])
|
---|
407 | {
|
---|
408 | /*
|
---|
409 | * Check that PRUnichar is equal in size to what compiler composes L""
|
---|
410 | * strings from; otherwise NS_LITERAL_STRING macros won't work correctly
|
---|
411 | * and we will get a meaningless SIGSEGV. This, of course, must be checked
|
---|
412 | * at compile time in xpcom/string/nsTDependentString.h, but XPCOM lacks
|
---|
413 | * compile-time assert macros and I'm not going to add them now.
|
---|
414 | */
|
---|
415 | if (sizeof(PRUnichar) != sizeof(wchar_t))
|
---|
416 | {
|
---|
417 | printf("Error: sizeof(PRUnichar) {%lu} != sizeof(wchar_t) {%lu}!\n"
|
---|
418 | "Probably, you forgot the -fshort-wchar compiler option.\n",
|
---|
419 | (unsigned long) sizeof(PRUnichar),
|
---|
420 | (unsigned long) sizeof(wchar_t));
|
---|
421 | return -1;
|
---|
422 | }
|
---|
423 |
|
---|
424 | nsresult rc;
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * This is the standard XPCOM init procedure.
|
---|
428 | * What we do is just follow the required steps to get an instance
|
---|
429 | * of our main interface, which is IVirtualBox.
|
---|
430 | */
|
---|
431 | #if defined(XPCOM_GLUE)
|
---|
432 | XPCOMGlueStartup(nsnull);
|
---|
433 | #endif
|
---|
434 |
|
---|
435 | /*
|
---|
436 | * Note that we scope all nsCOMPtr variables in order to have all XPCOM
|
---|
437 | * objects automatically released before we call NS_ShutdownXPCOM at the
|
---|
438 | * end. This is an XPCOM requirement.
|
---|
439 | */
|
---|
440 | {
|
---|
441 | nsCOMPtr<nsIServiceManager> serviceManager;
|
---|
442 | rc = NS_InitXPCOM2(getter_AddRefs(serviceManager), nsnull, nsnull);
|
---|
443 | if (NS_FAILED(rc))
|
---|
444 | {
|
---|
445 | printf("Error: XPCOM could not be initialized! rc=0x%x\n", rc);
|
---|
446 | return -1;
|
---|
447 | }
|
---|
448 |
|
---|
449 | #if 0
|
---|
450 | /*
|
---|
451 | * Register our components. This step is only necessary if this executable
|
---|
452 | * implements XPCOM components itself which is not the case for this
|
---|
453 | * simple example.
|
---|
454 | */
|
---|
455 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(serviceManager);
|
---|
456 | if (!registrar)
|
---|
457 | {
|
---|
458 | printf("Error: could not query nsIComponentRegistrar interface!\n");
|
---|
459 | return -1;
|
---|
460 | }
|
---|
461 | registrar->AutoRegister(nsnull);
|
---|
462 | #endif
|
---|
463 |
|
---|
464 | /*
|
---|
465 | * Make sure the main event queue is created. This event queue is
|
---|
466 | * responsible for dispatching incoming XPCOM IPC messages. The main
|
---|
467 | * thread should run this event queue's loop during lengthy non-XPCOM
|
---|
468 | * operations to ensure messages from the VirtualBox server and other
|
---|
469 | * XPCOM IPC clients are processed. This use case doesn't perform such
|
---|
470 | * operations so it doesn't run the event loop.
|
---|
471 | */
|
---|
472 | nsCOMPtr<nsIEventQueue> eventQ;
|
---|
473 | rc = NS_GetMainEventQ(getter_AddRefs (eventQ));
|
---|
474 | if (NS_FAILED(rc))
|
---|
475 | {
|
---|
476 | printf("Error: could not get main event queue! rc=%08X\n", rc);
|
---|
477 | return -1;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /*
|
---|
481 | * Now XPCOM is ready and we can start to do real work.
|
---|
482 | * IVirtualBox is the root interface of VirtualBox and will be
|
---|
483 | * retrieved from the XPCOM component manager. We use the
|
---|
484 | * XPCOM provided smart pointer nsCOMPtr for all objects because
|
---|
485 | * that's very convenient and removes the need deal with reference
|
---|
486 | * counting and freeing.
|
---|
487 | */
|
---|
488 | nsCOMPtr<nsIComponentManager> manager;
|
---|
489 | rc = NS_GetComponentManager (getter_AddRefs (manager));
|
---|
490 | if (NS_FAILED(rc))
|
---|
491 | {
|
---|
492 | printf("Error: could not get component manager! rc=%08X\n", rc);
|
---|
493 | return -1;
|
---|
494 | }
|
---|
495 |
|
---|
496 | nsCOMPtr<IVirtualBox> virtualBox;
|
---|
497 | rc = manager->CreateInstanceByContractID (NS_VIRTUALBOX_CONTRACTID,
|
---|
498 | nsnull,
|
---|
499 | NS_GET_IID(IVirtualBox),
|
---|
500 | getter_AddRefs(virtualBox));
|
---|
501 | if (NS_FAILED(rc))
|
---|
502 | {
|
---|
503 | printf("Error, could not instantiate VirtualBox object! rc=0x%x\n", rc);
|
---|
504 | return -1;
|
---|
505 | }
|
---|
506 | printf("VirtualBox object created\n");
|
---|
507 |
|
---|
508 | ////////////////////////////////////////////////////////////////////////////////
|
---|
509 | ////////////////////////////////////////////////////////////////////////////////
|
---|
510 | ////////////////////////////////////////////////////////////////////////////////
|
---|
511 |
|
---|
512 |
|
---|
513 | listVMs(virtualBox);
|
---|
514 |
|
---|
515 | createVM(virtualBox);
|
---|
516 |
|
---|
517 |
|
---|
518 | ////////////////////////////////////////////////////////////////////////////////
|
---|
519 | ////////////////////////////////////////////////////////////////////////////////
|
---|
520 | ////////////////////////////////////////////////////////////////////////////////
|
---|
521 |
|
---|
522 | /* this is enough to free the IVirtualBox instance -- smart pointers rule! */
|
---|
523 | virtualBox = nsnull;
|
---|
524 |
|
---|
525 | /*
|
---|
526 | * Process events that might have queued up in the XPCOM event
|
---|
527 | * queue. If we don't process them, the server might hang.
|
---|
528 | */
|
---|
529 | eventQ->ProcessPendingEvents();
|
---|
530 | }
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * Perform the standard XPCOM shutdown procedure.
|
---|
534 | */
|
---|
535 | NS_ShutdownXPCOM(nsnull);
|
---|
536 | #if defined(XPCOM_GLUE)
|
---|
537 | XPCOMGlueShutdown();
|
---|
538 | #endif
|
---|
539 | printf("Done!\n");
|
---|
540 | return 0;
|
---|
541 | }
|
---|
542 |
|
---|
543 |
|
---|
544 | //////////////////////////////////////////////////////////////////////////////////////////////////////
|
---|
545 | //// Helpers
|
---|
546 | //////////////////////////////////////////////////////////////////////////////////////////////////////
|
---|
547 |
|
---|
548 | /**
|
---|
549 | * Helper function to convert an nsID into a human readable string
|
---|
550 | *
|
---|
551 | * @returns result string, allocated. Has to be freed using free()
|
---|
552 | * @param guid Pointer to nsID that will be converted.
|
---|
553 | */
|
---|
554 | char *nsIDToString(nsID *guid)
|
---|
555 | {
|
---|
556 | char *res = (char*)malloc(39);
|
---|
557 |
|
---|
558 | if (res != NULL)
|
---|
559 | {
|
---|
560 | snprintf(res, 39, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
|
---|
561 | guid->m0, (PRUint32)guid->m1, (PRUint32)guid->m2,
|
---|
562 | (PRUint32)guid->m3[0], (PRUint32)guid->m3[1], (PRUint32)guid->m3[2],
|
---|
563 | (PRUint32)guid->m3[3], (PRUint32)guid->m3[4], (PRUint32)guid->m3[5],
|
---|
564 | (PRUint32)guid->m3[6], (PRUint32)guid->m3[7]);
|
---|
565 | }
|
---|
566 | return res;
|
---|
567 | }
|
---|
568 |
|
---|
569 | /**
|
---|
570 | * Helper function to print XPCOM exception information set on the current
|
---|
571 | * thread after a failed XPCOM method call. This function will also print
|
---|
572 | * extended VirtualBox error info if it is available.
|
---|
573 | */
|
---|
574 | void printErrorInfo()
|
---|
575 | {
|
---|
576 | nsresult rc;
|
---|
577 |
|
---|
578 | nsCOMPtr <nsIExceptionService> es;
|
---|
579 | es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
580 | if (NS_SUCCEEDED(rc))
|
---|
581 | {
|
---|
582 | nsCOMPtr <nsIExceptionManager> em;
|
---|
583 | rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
|
---|
584 | if (NS_SUCCEEDED(rc))
|
---|
585 | {
|
---|
586 | nsCOMPtr<nsIException> ex;
|
---|
587 | rc = em->GetCurrentException (getter_AddRefs (ex));
|
---|
588 | if (NS_SUCCEEDED(rc) && ex)
|
---|
589 | {
|
---|
590 | nsCOMPtr <IVirtualBoxErrorInfo> info;
|
---|
591 | info = do_QueryInterface(ex, &rc);
|
---|
592 | if (NS_SUCCEEDED(rc) && info)
|
---|
593 | {
|
---|
594 | /* got extended error info */
|
---|
595 | printf ("Extended error info (IVirtualBoxErrorInfo):\n");
|
---|
596 | PRInt32 resultCode = NS_OK;
|
---|
597 | info->GetResultCode (&resultCode);
|
---|
598 | printf (" resultCode=%08X\n", resultCode);
|
---|
599 | nsXPIDLString component;
|
---|
600 | info->GetComponent (getter_Copies (component));
|
---|
601 | printf (" component=%s\n", NS_ConvertUTF16toUTF8(component).get());
|
---|
602 | nsXPIDLString text;
|
---|
603 | info->GetText (getter_Copies (text));
|
---|
604 | printf (" text=%s\n", NS_ConvertUTF16toUTF8(text).get());
|
---|
605 | }
|
---|
606 | else
|
---|
607 | {
|
---|
608 | /* got basic error info */
|
---|
609 | printf ("Basic error info (nsIException):\n");
|
---|
610 | nsresult resultCode = NS_OK;
|
---|
611 | ex->GetResult (&resultCode);
|
---|
612 | printf (" resultCode=%08X\n", resultCode);
|
---|
613 | nsXPIDLCString message;
|
---|
614 | ex->GetMessage (getter_Copies (message));
|
---|
615 | printf (" message=%s\n", message.get());
|
---|
616 | }
|
---|
617 |
|
---|
618 | /* reset the exception to NULL to indicate we've processed it */
|
---|
619 | em->SetCurrentException (NULL);
|
---|
620 |
|
---|
621 | rc = NS_OK;
|
---|
622 | }
|
---|
623 | }
|
---|
624 | }
|
---|
625 | }
|
---|