VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstAPI.cpp@ 3411

Last change on this file since 3411 was 3387, checked in by vboxsync, 17 years ago

Main: com::GetVBoxUserHomeDirectory() now takes char * instead of Utf8Str because on XPCOM platforms, this function may be called before XPCOM has been initialized, when Utf8Str functionality is not yet available (because it uses XPCOM memory management).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.4 KB
Line 
1/** @file
2 *
3 * tstAPI - test program for our COM/XPCOM interface
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <VBox/com/com.h>
26#include <VBox/com/string.h>
27#include <VBox/com/Guid.h>
28#include <VBox/com/ErrorInfo.h>
29#include <VBox/com/EventQueue.h>
30
31#include <VBox/com/VirtualBox.h>
32
33using namespace com;
34
35#define LOG_ENABLED
36#define LOG_GROUP LOG_GROUP_MAIN
37#define LOG_INSTANCE NULL
38#include <VBox/log.h>
39
40#include <iprt/runtime.h>
41#include <iprt/stream.h>
42
43#define printf RTPrintf
44
45// funcs
46///////////////////////////////////////////////////////////////////////////////
47
48HRESULT readAndChangeMachineSettings (IMachine *machine, IMachine *readonlyMachine = 0)
49{
50 HRESULT rc = S_OK;
51
52 Bstr name;
53 printf ("Getting machine name...\n");
54 CHECK_RC_RET (machine->COMGETTER(Name) (name.asOutParam()));
55 printf ("Name: {%ls}\n", name.raw());
56
57 printf("Getting machine GUID...\n");
58 Guid guid;
59 CHECK_RC (machine->COMGETTER(Id) (guid.asOutParam()));
60 if (SUCCEEDED (rc) && !guid.isEmpty()) {
61 printf ("Guid::toString(): {%s}\n", (const char *) guid.toString());
62 } else {
63 printf ("WARNING: there's no GUID!");
64 }
65
66 ULONG memorySize;
67 printf ("Getting memory size...\n");
68 CHECK_RC_RET (machine->COMGETTER(MemorySize) (&memorySize));
69 printf ("Memory size: %d\n", memorySize);
70
71 MachineState_T machineState;
72 printf ("Getting machine state...\n");
73 CHECK_RC_RET (machine->COMGETTER(State) (&machineState));
74 printf ("Machine state: %d\n", machineState);
75
76 BOOL modified;
77 printf ("Are any settings modified?...\n");
78 CHECK_RC (machine->COMGETTER(SettingsModified) (&modified));
79 if (SUCCEEDED (rc))
80 printf ("%s\n", modified ? "yes" : "no");
81
82 ULONG memorySizeBig = memorySize * 10;
83 printf("Changing memory size to %d...\n", memorySizeBig);
84 CHECK_RC (machine->COMSETTER(MemorySize) (memorySizeBig));
85
86 if (SUCCEEDED (rc))
87 {
88 printf ("Are any settings modified now?...\n");
89 CHECK_RC_RET (machine->COMGETTER(SettingsModified) (&modified));
90 printf ("%s\n", modified ? "yes" : "no");
91 ASSERT_RET (modified, 0);
92
93 ULONG memorySizeGot;
94 printf ("Getting memory size again...\n");
95 CHECK_RC_RET (machine->COMGETTER(MemorySize) (&memorySizeGot));
96 printf ("Memory size: %d\n", memorySizeGot);
97 ASSERT_RET (memorySizeGot == memorySizeBig, 0);
98
99 if (readonlyMachine)
100 {
101 printf ("Getting memory size of the counterpart readonly machine...\n");
102 ULONG memorySizeRO;
103 readonlyMachine->COMGETTER(MemorySize) (&memorySizeRO);
104 printf ("Memory size: %d\n", memorySizeRO);
105 ASSERT_RET (memorySizeRO != memorySizeGot, 0);
106 }
107
108 printf ("Discarding recent changes...\n");
109 CHECK_RC_RET (machine->DiscardSettings());
110 printf ("Are any settings modified after discarding?...\n");
111 CHECK_RC_RET (machine->COMGETTER(SettingsModified) (&modified));
112 printf ("%s\n", modified ? "yes" : "no");
113 ASSERT_RET (!modified, 0);
114
115 printf ("Getting memory size once more...\n");
116 CHECK_RC_RET (machine->COMGETTER(MemorySize) (&memorySizeGot));
117 printf ("Memory size: %d\n", memorySizeGot);
118 ASSERT_RET (memorySizeGot == memorySize, 0);
119
120 memorySize = memorySize > 128 ? memorySize / 2 : memorySize * 2;
121 printf("Changing memory size to %d...\n", memorySize);
122 CHECK_RC_RET (machine->COMSETTER(MemorySize) (memorySize));
123 }
124
125 Bstr desc;
126 printf ("Getting description...\n");
127 CHECK_ERROR_RET (machine, COMGETTER(Description) (desc.asOutParam()), rc);
128 printf ("Description is: \"%ls\"\n", desc.raw());
129
130 desc = L"This is an exemplary description (changed).";
131 printf ("Setting description to \"%ls\"...\n", desc.raw());
132 CHECK_ERROR_RET (machine, COMSETTER(Description) (desc), rc);
133
134 printf ("Saving machine settings...\n");
135 CHECK_RC (machine->SaveSettings());
136 if (SUCCEEDED (rc))
137 {
138 printf ("Are any settings modified after saving?...\n");
139 CHECK_RC_RET (machine->COMGETTER(SettingsModified) (&modified));
140 printf ("%s\n", modified ? "yes" : "no");
141 ASSERT_RET (!modified, 0);
142
143 if (readonlyMachine) {
144 printf ("Getting memory size of the counterpart readonly machine...\n");
145 ULONG memorySizeRO;
146 readonlyMachine->COMGETTER(MemorySize) (&memorySizeRO);
147 printf ("Memory size: %d\n", memorySizeRO);
148 ASSERT_RET (memorySizeRO == memorySize, 0);
149 }
150 }
151
152 Bstr extraDataKey = L"Blafasel";
153 Bstr extraData;
154 printf ("Getting extra data key {%ls}...\n", extraDataKey.raw());
155 CHECK_RC_RET (machine->GetExtraData (extraDataKey, extraData.asOutParam()));
156 if (!extraData.isEmpty()) {
157 printf ("Extra data value: {%ls}\n", extraData.raw());
158 } else {
159 if (extraData.isNull())
160 printf ("No extra data exists\n");
161 else
162 printf ("Extra data is empty\n");
163 }
164
165 if (extraData.isEmpty())
166 extraData = L"Das ist die Berliner Luft, Luft, Luft...";
167 else
168 extraData.setNull();
169 printf (
170 "Setting extra data key {%ls} to {%ls}...\n",
171 extraDataKey.raw(), extraData.raw()
172 );
173 CHECK_RC (machine->SetExtraData (extraDataKey, extraData));
174
175 if (SUCCEEDED (rc)) {
176 printf ("Getting extra data key {%ls} again...\n", extraDataKey.raw());
177 CHECK_RC_RET (machine->GetExtraData (extraDataKey, extraData.asOutParam()));
178 if (!extraData.isEmpty()) {
179 printf ("Extra data value: {%ls}\n", extraData.raw());
180 } else {
181 if (extraData.isNull())
182 printf ("No extra data exists\n");
183 else
184 printf ("Extra data is empty\n");
185 }
186 }
187
188 return rc;
189}
190
191// main
192///////////////////////////////////////////////////////////////////////////////
193
194int main(int argc, char *argv[])
195{
196 /*
197 * Initialize the VBox runtime without loading
198 * the support driver.
199 */
200 RTR3Init(false);
201
202 HRESULT rc;
203
204 {
205 char homeDir [RTPATH_MAX];
206 GetVBoxUserHomeDirectory (homeDir, sizeof (homeDir));
207 printf ("VirtualBox Home Directory = '%s'\n", homeDir);
208 }
209
210 printf ("Initializing COM...\n");
211
212 CHECK_RC_RET (com::Initialize());
213
214 do
215 {
216 // scopes all the stuff till shutdown
217 ////////////////////////////////////////////////////////////////////////////
218
219 ComPtr <IVirtualBox> virtualBox;
220 ComPtr <ISession> session;
221
222#if 0
223 // Utf8Str test
224 ////////////////////////////////////////////////////////////////////////////
225
226 Utf8Str nullUtf8Str;
227 printf ("nullUtf8Str='%s'\n", nullUtf8Str.raw());
228
229 Utf8Str simpleUtf8Str = "simpleUtf8Str";
230 printf ("simpleUtf8Str='%s'\n", simpleUtf8Str.raw());
231
232 Utf8Str utf8StrFmt = Utf8StrFmt ("[0=%d]%s[1=%d]",
233 0, "utf8StrFmt", 1);
234 printf ("utf8StrFmt='%s'\n", utf8StrFmt.raw());
235
236#endif
237
238 printf ("Creating VirtualBox object...\n");
239 CHECK_RC (virtualBox.createLocalObject (CLSID_VirtualBox));
240 if (FAILED (rc))
241 {
242 CHECK_ERROR_NOCALL();
243 break;
244 }
245
246 printf ("Creating Session object...\n");
247 CHECK_RC (session.createInprocObject (CLSID_Session));
248 if (FAILED (rc))
249 {
250 CHECK_ERROR_NOCALL();
251 break;
252 }
253
254#if 0
255 // IUnknown identity test
256 ////////////////////////////////////////////////////////////////////////////
257 {
258 ComPtr <IVirtualBox> virtualBox2;
259
260 printf ("Creating one more VirtualBox object...\n");
261 CHECK_RC (virtualBox2.createLocalObject (CLSID_VirtualBox));
262 if (FAILED (rc))
263 {
264 CHECK_ERROR_NOCALL();
265 break;
266 }
267
268 printf ("IVirtualBox(virualBox)=%p IVirtualBox(virualBox2)=%p\n",
269 (IVirtualBox *) virtualBox, (IVirtualBox *) virtualBox2);
270
271 ComPtr <IUnknown> unk (virtualBox);
272 ComPtr <IUnknown> unk2;
273 unk2 = virtualBox2;
274
275 printf ("IUnknown(virualBox)=%p IUnknown(virualBox2)=%p\n",
276 (IUnknown *) unk, (IUnknown *) unk2);
277
278 ComPtr <IVirtualBox> vb = unk;
279 ComPtr <IVirtualBox> vb2 = unk;
280
281 printf ("IVirtualBox(IUnknown(virualBox))=%p IVirtualBox(IUnknown(virualBox2))=%p\n",
282 (IVirtualBox *) vb, (IVirtualBox *) vb2);
283
284 printf ("Will be now released (press Enter)...");
285 getchar();
286 }
287#endif
288
289 // create the event queue
290 // (here it is necessary only to process remaining XPCOM/IPC events
291 // after the session is closed)
292 EventQueue eventQ;
293
294 // some outdated stuff
295 ////////////////////////////////////////////////////////////////////////////
296
297#if 0
298 printf("Getting IHost interface...\n");
299 IHost *host;
300 rc = virtualBox->GetHost(&host);
301 if (SUCCEEDED(rc))
302 {
303 IHostDVDDriveCollection *dvdColl;
304 rc = host->GetHostDVDDrives(&dvdColl);
305 if (SUCCEEDED(rc))
306 {
307 IHostDVDDrive *dvdDrive = NULL;
308 dvdColl->GetNextHostDVDDrive(dvdDrive, &dvdDrive);
309 while (dvdDrive)
310 {
311 BSTR driveName;
312 char *driveNameUtf8;
313 dvdDrive->GetDriveName(&driveName);
314 RTStrUcs2ToUtf8(&driveNameUtf8, (PCRTUCS2)driveName);
315 printf("Host DVD drive name: %s\n", driveNameUtf8);
316 RTStrFree(driveNameUtf8);
317 SysFreeString(driveName);
318 IHostDVDDrive *dvdDriveTemp = dvdDrive;
319 dvdColl->GetNextHostDVDDrive(dvdDriveTemp, &dvdDrive);
320 dvdDriveTemp->Release();
321 }
322 dvdColl->Release();
323 } else
324 {
325 printf("Could not get host DVD drive collection\n");
326 }
327
328 IHostFloppyDriveCollection *floppyColl;
329 rc = host->GetHostFloppyDrives(&floppyColl);
330 if (SUCCEEDED(rc))
331 {
332 IHostFloppyDrive *floppyDrive = NULL;
333 floppyColl->GetNextHostFloppyDrive(floppyDrive, &floppyDrive);
334 while (floppyDrive)
335 {
336 BSTR driveName;
337 char *driveNameUtf8;
338 floppyDrive->GetDriveName(&driveName);
339 RTStrUcs2ToUtf8(&driveNameUtf8, (PCRTUCS2)driveName);
340 printf("Host floppy drive name: %s\n", driveNameUtf8);
341 RTStrFree(driveNameUtf8);
342 SysFreeString(driveName);
343 IHostFloppyDrive *floppyDriveTemp = floppyDrive;
344 floppyColl->GetNextHostFloppyDrive(floppyDriveTemp, &floppyDrive);
345 floppyDriveTemp->Release();
346 }
347 floppyColl->Release();
348 } else
349 {
350 printf("Could not get host floppy drive collection\n");
351 }
352 host->Release();
353 } else
354 {
355 printf("Call failed\n");
356 }
357 printf ("\n");
358#endif
359
360#if 0
361 // IVirtualBoxErrorInfo test
362 ////////////////////////////////////////////////////////////////////////////
363 {
364 // RPC calls
365
366 // call a method that will definitely fail
367 Guid uuid;
368 ComPtr <IHardDisk> hardDisk;
369 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
370 printf ("virtualBox->GetHardDisk(null-uuid)=%08X\n", rc);
371
372// {
373// com::ErrorInfo info (virtualBox);
374// PRINT_ERROR_INFO (info);
375// }
376
377 // call a method that will definitely succeed
378 Bstr version;
379 rc = virtualBox->COMGETTER(Version) (version.asOutParam());
380 printf ("virtualBox->COMGETTER(Version)=%08X\n", rc);
381
382 {
383 com::ErrorInfo info (virtualBox);
384 PRINT_ERROR_INFO (info);
385 }
386
387 // Local calls
388
389 // call a method that will definitely fail
390 ComPtr <IMachine> machine;
391 rc = session->COMGETTER(Machine)(machine.asOutParam());
392 printf ("session->COMGETTER(Machine)=%08X\n", rc);
393
394// {
395// com::ErrorInfo info (virtualBox);
396// PRINT_ERROR_INFO (info);
397// }
398
399 // call a method that will definitely succeed
400 SessionState_T state;
401 rc = session->COMGETTER(State) (&state);
402 printf ("session->COMGETTER(State)=%08X\n", rc);
403
404 {
405 com::ErrorInfo info (virtualBox);
406 PRINT_ERROR_INFO (info);
407 }
408 }
409#endif
410
411#if 0
412 // register the existing hard disk image
413 ///////////////////////////////////////////////////////////////////////////
414 do
415 {
416 ComPtr <IHardDisk> hd;
417 Bstr src = L"E:\\develop\\innotek\\images\\NewHardDisk.vdi";
418 printf ("Registerin the existing hard disk '%ls'...\n", src.raw());
419 CHECK_ERROR_BREAK (virtualBox, OpenHardDisk (src, hd.asOutParam()));
420 CHECK_ERROR_BREAK (virtualBox, RegisterHardDisk (hd));
421 }
422 while (FALSE);
423 printf ("\n");
424#endif
425
426#if 0
427 // find and unregister the existing hard disk image
428 ///////////////////////////////////////////////////////////////////////////
429 do
430 {
431 ComPtr <IVirtualDiskImage> vdi;
432 Bstr src = L"CreatorTest.vdi";
433 printf ("Unregistering the hard disk '%ls'...\n", src.raw());
434 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
435 ComPtr <IHardDisk> hd = vdi;
436 Guid id;
437 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
438 CHECK_ERROR_BREAK (virtualBox, UnregisterHardDisk (id, hd.asOutParam()));
439 }
440 while (FALSE);
441 printf ("\n");
442#endif
443
444#if 0
445 // clone the registered hard disk
446 ///////////////////////////////////////////////////////////////////////////
447 do
448 {
449#if defined __LINUX__
450 Bstr src = L"/mnt/hugaida/common/develop/innotek/images/freedos-linux.vdi";
451#else
452 Bstr src = L"E:/develop/innotek/images/freedos.vdi";
453#endif
454 Bstr dst = L"./clone.vdi";
455 RTPrintf ("Cloning '%ls' to '%ls'...\n", src.raw(), dst.raw());
456 ComPtr <IVirtualDiskImage> vdi;
457 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
458 ComPtr <IHardDisk> hd = vdi;
459 ComPtr <IProgress> progress;
460 CHECK_ERROR_BREAK (hd, CloneToImage (dst, vdi.asOutParam(), progress.asOutParam()));
461 RTPrintf ("Waiting for completion...\n");
462 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
463 ProgressErrorInfo ei (progress);
464 if (FAILED (ei.getResultCode()))
465 {
466 PRINT_ERROR_INFO (ei);
467 }
468 else
469 {
470 vdi->COMGETTER(FilePath) (dst.asOutParam());
471 RTPrintf ("Actual clone path is '%ls'\n", dst.raw());
472 }
473 }
474 while (FALSE);
475 printf ("\n");
476#endif
477
478#if 0
479 // find a registered hard disk by location
480 ///////////////////////////////////////////////////////////////////////////
481 do
482 {
483 ComPtr <IHardDisk> hd;
484 static const wchar_t *Names[] =
485 {
486#ifndef __LINUX__
487 L"E:/Develop/innotek/images/thinker/freedos.vdi",
488 L"E:/Develop/innotek/images/thinker/fReeDoS.vDI",
489 L"E:/Develop/innotek/images/vmdk/haiku.vmdk",
490#else
491 L"/mnt/host/common/Develop/innotek/images/maggot/freedos.vdi",
492 L"/mnt/host/common/Develop/innotek/images/maggot/fReeDoS.vDI",
493#endif
494 };
495 for (size_t i = 0; i < ELEMENTS (Names); ++ i)
496 {
497 Bstr src = Names [i];
498 printf ("Searching for hard disk '%ls'...\n", src.raw());
499 rc = virtualBox->FindHardDisk (src, hd.asOutParam());
500 if (SUCCEEDED (rc))
501 {
502 Guid id;
503 Bstr location;
504 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
505 CHECK_ERROR_BREAK (hd, COMGETTER(Location) (location.asOutParam()));
506 printf ("Found, UUID={%Vuuid}, location='%ls'.\n",
507 id.raw(), location.raw());
508 }
509 else
510 {
511 PRINT_ERROR_INFO (com::ErrorInfo (virtualBox));
512 }
513 }
514 }
515 while (FALSE);
516 printf ("\n");
517#endif
518
519#if 0
520 // access the machine in read-only mode
521 ///////////////////////////////////////////////////////////////////////////
522 do
523 {
524 ComPtr <IMachine> machine;
525 Bstr name = "Windows XP";
526 printf ("Getting a machine object named '%ls'...\n", name.raw());
527 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
528// printf ("Accessing the machine in read-only mode:\n");
529// readAndChangeMachineSettings (machine);
530// if (argc != 2)
531// {
532// printf("Error: a string has to be supplied!\n");
533// }
534// else
535// {
536// Bstr secureLabel = argv[1];
537// machine->COMSETTER(ExtraData)(L"VBoxSDL/SecureLabel", secureLabel);
538// }
539 }
540 while (FALSE);
541 printf ("\n");
542#endif
543
544#if 0
545 // create a new machine (w/o registering it)
546 ///////////////////////////////////////////////////////////////////////////
547 do
548 {
549 ComPtr <IMachine> machine;
550#if defined (__LINUX__)
551 Bstr baseDir = L"/tmp/vbox";
552#else
553 Bstr baseDir = L"C:\\vbox";
554#endif
555 Bstr name = L"machina";
556
557 printf ("Creating a new machine object (base dir '%ls', name '%ls')...\n",
558 baseDir.raw(), name.raw());
559 CHECK_ERROR_BREAK (virtualBox, CreateMachine (baseDir, name,
560 machine.asOutParam()));
561
562 printf ("Getting name...\n");
563 CHECK_ERROR_BREAK (machine, COMGETTER(Name) (name.asOutParam()));
564 printf ("Name: {%ls}\n", name.raw());
565
566 BOOL modified = FALSE;
567 printf ("Are any settings modified?...\n");
568 CHECK_ERROR_BREAK (machine, COMGETTER(SettingsModified) (&modified));
569 printf ("%s\n", modified ? "yes" : "no");
570
571 ASSERT_BREAK (modified == TRUE);
572
573 name = L"Kakaya prekrasnaya virtual'naya mashina!";
574 printf ("Setting new name ({%ls})...\n", name.raw());
575 CHECK_ERROR_BREAK (machine, COMSETTER(Name) (name));
576
577 printf ("Setting memory size to 111...\n");
578 CHECK_ERROR_BREAK (machine, COMSETTER(MemorySize) (111));
579
580 Bstr desc = L"This is an exemplary description.";
581 printf ("Setting description to \"%ls\"...\n", desc.raw());
582 CHECK_ERROR_BREAK (machine, COMSETTER(Description) (desc));
583
584 ComPtr <IGuestOSType> guestOSType;
585 Bstr type = L"os2warp45";
586 CHECK_ERROR_BREAK (virtualBox, GetGuestOSType (type, guestOSType.asOutParam()));
587
588 printf ("Saving new machine settings...\n");
589 CHECK_ERROR_BREAK (machine, SaveSettings());
590
591 printf ("Accessing the newly created machine:\n");
592 readAndChangeMachineSettings (machine);
593 }
594 while (FALSE);
595 printf ("\n");
596#endif
597
598#if 0
599 // enumerate host DVD drives
600 ///////////////////////////////////////////////////////////////////////////
601 do
602 {
603 ComPtr <IHost> host;
604 CHECK_RC_BREAK (virtualBox->COMGETTER(Host) (host.asOutParam()));
605
606 {
607 ComPtr <IHostDVDDriveCollection> coll;
608 CHECK_RC_BREAK (host->COMGETTER(DVDDrives) (coll.asOutParam()));
609 ComPtr <IHostDVDDriveEnumerator> enumerator;
610 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
611 BOOL hasmore;
612 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
613 {
614 ComPtr <IHostDVDDrive> drive;
615 CHECK_RC_BREAK (enumerator->GetNext (drive.asOutParam()));
616 Bstr name;
617 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
618 printf ("Host DVD drive: name={%ls}\n", name.raw());
619 }
620 CHECK_RC_BREAK (rc);
621
622 ComPtr <IHostDVDDrive> drive;
623 CHECK_ERROR (enumerator, GetNext (drive.asOutParam()));
624 CHECK_ERROR (coll, GetItemAt (1000, drive.asOutParam()));
625 CHECK_ERROR (coll, FindByName (Bstr ("R:"), drive.asOutParam()));
626 if (SUCCEEDED (rc))
627 {
628 Bstr name;
629 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
630 printf ("Found by name: name={%ls}\n", name.raw());
631 }
632 }
633 }
634 while (FALSE);
635 printf ("\n");
636#endif
637
638#if 0
639 // enumerate hard disks & dvd images
640 ///////////////////////////////////////////////////////////////////////////
641 do
642 {
643 {
644 ComPtr <IHardDiskCollection> coll;
645 CHECK_RC_BREAK (virtualBox->COMGETTER(HardDisks) (coll.asOutParam()));
646 ComPtr <IHardDiskEnumerator> enumerator;
647 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
648 BOOL hasmore;
649 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
650 {
651 ComPtr <IHardDisk> disk;
652 CHECK_RC_BREAK (enumerator->GetNext (disk.asOutParam()));
653 Guid id;
654 CHECK_RC_BREAK (disk->COMGETTER(Id) (id.asOutParam()));
655 Bstr path;
656 CHECK_RC_BREAK (disk->COMGETTER(FilePath) (path.asOutParam()));
657 printf ("Hard Disk: id={%s}, path={%ls}\n",
658 id.toString().raw(), path.raw());
659 Guid mid;
660 CHECK_RC_BREAK (
661 virtualBox->GetHardDiskUsage (id, ResourceUsage_AllUsage,
662 mid.asOutParam())
663 );
664 if (mid.isEmpty())
665 printf (" not used\n");
666 else
667 printf (" used by VM: {%s}\n", mid.toString().raw());
668 }
669 CHECK_RC_BREAK (rc);
670 }
671
672 {
673 ComPtr <IDVDImageCollection> coll;
674 CHECK_RC_BREAK (virtualBox->COMGETTER(DVDImages) (coll.asOutParam()));
675 ComPtr <IDVDImageEnumerator> enumerator;
676 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
677 BOOL hasmore;
678 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
679 {
680 ComPtr <IDVDImage> image;
681 CHECK_RC_BREAK (enumerator->GetNext (image.asOutParam()));
682 Guid id;
683 CHECK_RC_BREAK (image->COMGETTER(Id) (id.asOutParam()));
684 Bstr path;
685 CHECK_RC_BREAK (image->COMGETTER(FilePath) (path.asOutParam()));
686 printf ("CD/DVD Image: id={%s}, path={%ls}\n",
687 id.toString().raw(), path.raw());
688 Bstr mIDs;
689 CHECK_RC_BREAK (
690 virtualBox->GetDVDImageUsage (id, ResourceUsage_AllUsage,
691 mIDs.asOutParam())
692 );
693 if (mIDs.isNull())
694 printf (" not used\n");
695 else
696 printf (" used by VMs: {%ls}\n", mIDs.raw());
697 }
698 CHECK_RC_BREAK (rc);
699 }
700 }
701 while (FALSE);
702 printf ("\n");
703#endif
704
705#if 0
706 // open a (direct) session
707 ///////////////////////////////////////////////////////////////////////////
708 do
709 {
710 ComPtr <IMachine> machine;
711 Bstr name = L"dos";
712 printf ("Getting a machine object named '%ls'...\n", name.raw());
713 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
714 Guid guid;
715 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
716 printf ("Opening a session for this machine...\n");
717 CHECK_RC_BREAK (virtualBox->OpenSession (session, guid));
718#if 0
719 ComPtr <IMachine> sessionMachine;
720 printf ("Getting sessioned machine object...\n");
721 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
722 printf ("Accessing the machine within the session:\n");
723 readAndChangeMachineSettings (sessionMachine, machine);
724#endif
725#if 0
726 ComPtr <IConsole> console;
727 printf ("Getting the console object...\n");
728 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
729 printf ("Discarding the current machine state...\n");
730 ComPtr <IProgress> progress;
731 CHECK_ERROR_BREAK (console, DiscardCurrentState (progress.asOutParam()));
732 printf ("Waiting for completion...\n");
733 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
734 ProgressErrorInfo ei (progress);
735 if (FAILED (ei.getResultCode()))
736 {
737 PRINT_ERROR_INFO (ei);
738
739 ComPtr <IUnknown> initiator;
740 CHECK_ERROR_BREAK (progress, COMGETTER(Initiator) (initiator.asOutParam()));
741
742 printf ("initiator(unk) = %p\n", (IUnknown *) initiator);
743 printf ("console(unk) = %p\n", (IUnknown *) ComPtr <IUnknown> ((IConsole *) console));
744 printf ("console = %p\n", (IConsole *) console);
745 }
746#endif
747 session->Close();
748 }
749 while (FALSE);
750 printf ("\n");
751#endif
752
753#if 0
754 // open a remote session
755 ///////////////////////////////////////////////////////////////////////////
756 do
757 {
758 ComPtr <IMachine> machine;
759 Bstr name = L"dos";
760 printf ("Getting a machine object named '%ls'...\n", name.raw());
761 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
762 Guid guid;
763 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
764 printf ("Opening a remote session for this machine...\n");
765 ComPtr <IProgress> progress;
766 CHECK_RC_BREAK (virtualBox->OpenRemoteSession (session, guid, Bstr("gui"),
767 progress.asOutParam()));
768 printf ("Waiting for the session to open...\n");
769 CHECK_RC_BREAK (progress->WaitForCompletion (-1));
770 ComPtr <IMachine> sessionMachine;
771 printf ("Getting sessioned machine object...\n");
772 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
773 ComPtr <IConsole> console;
774 printf ("Getting console object...\n");
775 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
776 printf ("Press enter to pause the VM execution in the remote session...");
777 getchar();
778 CHECK_RC (console->Pause());
779 printf ("Press enter to close this session...");
780 getchar();
781 session->Close();
782 }
783 while (FALSE);
784 printf ("\n");
785#endif
786
787#if 0
788 // open an existing remote session
789 ///////////////////////////////////////////////////////////////////////////
790 do
791 {
792 ComPtr <IMachine> machine;
793 Bstr name = "dos";
794 printf ("Getting a machine object named '%ls'...\n", name.raw());
795 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
796 Guid guid;
797 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
798 printf ("Opening an existing remote session for this machine...\n");
799 CHECK_RC_BREAK (virtualBox->OpenExistingSession (session, guid));
800 ComPtr <IMachine> sessionMachine;
801 printf ("Getting sessioned machine object...\n");
802 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
803
804#if 0
805 Bstr extraDataKey = "VBoxSDL/SecureLabel";
806 Bstr extraData = "Das kommt jetzt noch viel krasser vom total konkreten API!";
807 CHECK_RC (sessionMachine->SetExtraData (extraDataKey, extraData));
808#endif
809#if 0
810 ComPtr <IConsole> console;
811 printf ("Getting console object...\n");
812 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
813 printf ("Press enter to pause the VM execution in the remote session...");
814 getchar();
815 CHECK_RC (console->Pause());
816 printf ("Press enter to close this session...");
817 getchar();
818#endif
819 session->Close();
820 }
821 while (FALSE);
822 printf ("\n");
823#endif
824
825 printf ("Press enter to release Session and VirtualBox instances...");
826 getchar();
827
828 // end "all-stuff" scope
829 ////////////////////////////////////////////////////////////////////////////
830 }
831 while (0);
832
833 printf("Press enter to shutdown COM...");
834 getchar();
835
836 com::Shutdown();
837
838 printf ("tstAPI FINISHED.\n");
839
840 return rc;
841}
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