VirtualBox

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

Last change on this file since 4041 was 3834, checked in by vboxsync, 17 years ago

Main/testcase: More IUnknown Identity tests.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.0 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 1
255 // IUnknown identity test
256 ////////////////////////////////////////////////////////////////////////////
257 {
258 {
259 ComPtr <IVirtualBox> virtualBox2;
260
261 printf ("Creating one more VirtualBox object...\n");
262 CHECK_RC (virtualBox2.createLocalObject (CLSID_VirtualBox));
263 if (FAILED (rc))
264 {
265 CHECK_ERROR_NOCALL();
266 break;
267 }
268
269 printf ("IVirtualBox(virualBox)=%p IVirtualBox(virualBox2)=%p\n",
270 (IVirtualBox *) virtualBox, (IVirtualBox *) virtualBox2);
271 Assert ((IVirtualBox *) virtualBox == (IVirtualBox *) virtualBox2);
272
273 ComPtr <IUnknown> unk (virtualBox);
274 ComPtr <IUnknown> unk2;
275 unk2 = virtualBox2;
276
277 printf ("IUnknown(virualBox)=%p IUnknown(virualBox2)=%p\n",
278 (IUnknown *) unk, (IUnknown *) unk2);
279 Assert ((IUnknown *) unk == (IUnknown *) unk2);
280
281 ComPtr <IVirtualBox> vb = unk;
282 ComPtr <IVirtualBox> vb2 = unk;
283
284 printf ("IVirtualBox(IUnknown(virualBox))=%p IVirtualBox(IUnknown(virualBox2))=%p\n",
285 (IVirtualBox *) vb, (IVirtualBox *) vb2);
286 Assert ((IVirtualBox *) vb == (IVirtualBox *) vb2);
287 }
288
289 {
290 ComPtr <IHost> host;
291 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host)(host.asOutParam()));
292 printf (" IHost(host)=%p\n", (IHost *) host);
293 ComPtr <IUnknown> unk = host;
294 printf (" IUnknown(host)=%p\n", (IUnknown *) unk);
295 ComPtr <IHost> host_copy = unk;
296 printf (" IHost(host_copy)=%p\n", (IHost *) host_copy);
297 ComPtr <IUnknown> unk_copy = host_copy;
298 printf (" IUnknown(host_copy)=%p\n", (IUnknown *) unk_copy);
299 Assert ((IUnknown *) unk == (IUnknown *) unk_copy);
300
301 /* query IUnknown on IUnknown */
302 ComPtr <IUnknown> unk_copy_copy;
303 unk_copy.queryInterfaceTo (unk_copy_copy.asOutParam());
304 printf (" IUnknown(unk_copy)=%p\n", (IUnknown *) unk_copy_copy);
305 Assert ((IUnknown *) unk_copy == (IUnknown *) unk_copy_copy);
306 /* query IUnknown on IUnknown in the opposite direction */
307 unk_copy_copy.queryInterfaceTo (unk_copy.asOutParam());
308 printf (" IUnknown(unk_copy_copy)=%p\n", (IUnknown *) unk_copy);
309 Assert ((IUnknown *) unk_copy == (IUnknown *) unk_copy_copy);
310
311 /* query IUnknown again after releasing all previous IUnknown instances
312 * but keeping IHost -- it should remain the same (Identity Rule) */
313 IUnknown *oldUnk = unk;
314 unk.setNull();
315 unk_copy.setNull();
316 unk_copy_copy.setNull();
317 unk = host;
318 printf (" IUnknown(host)=%p\n", (IUnknown *) unk);
319 Assert (oldUnk == (IUnknown *) unk);
320 }
321
322// printf ("Will be now released (press Enter)...");
323// getchar();
324 }
325#endif
326
327 // create the event queue
328 // (here it is necessary only to process remaining XPCOM/IPC events
329 // after the session is closed)
330 EventQueue eventQ;
331
332 // some outdated stuff
333 ////////////////////////////////////////////////////////////////////////////
334
335#if 0
336 printf("Getting IHost interface...\n");
337 IHost *host;
338 rc = virtualBox->GetHost(&host);
339 if (SUCCEEDED(rc))
340 {
341 IHostDVDDriveCollection *dvdColl;
342 rc = host->GetHostDVDDrives(&dvdColl);
343 if (SUCCEEDED(rc))
344 {
345 IHostDVDDrive *dvdDrive = NULL;
346 dvdColl->GetNextHostDVDDrive(dvdDrive, &dvdDrive);
347 while (dvdDrive)
348 {
349 BSTR driveName;
350 char *driveNameUtf8;
351 dvdDrive->GetDriveName(&driveName);
352 RTStrUcs2ToUtf8(&driveNameUtf8, (PCRTUCS2)driveName);
353 printf("Host DVD drive name: %s\n", driveNameUtf8);
354 RTStrFree(driveNameUtf8);
355 SysFreeString(driveName);
356 IHostDVDDrive *dvdDriveTemp = dvdDrive;
357 dvdColl->GetNextHostDVDDrive(dvdDriveTemp, &dvdDrive);
358 dvdDriveTemp->Release();
359 }
360 dvdColl->Release();
361 } else
362 {
363 printf("Could not get host DVD drive collection\n");
364 }
365
366 IHostFloppyDriveCollection *floppyColl;
367 rc = host->GetHostFloppyDrives(&floppyColl);
368 if (SUCCEEDED(rc))
369 {
370 IHostFloppyDrive *floppyDrive = NULL;
371 floppyColl->GetNextHostFloppyDrive(floppyDrive, &floppyDrive);
372 while (floppyDrive)
373 {
374 BSTR driveName;
375 char *driveNameUtf8;
376 floppyDrive->GetDriveName(&driveName);
377 RTStrUcs2ToUtf8(&driveNameUtf8, (PCRTUCS2)driveName);
378 printf("Host floppy drive name: %s\n", driveNameUtf8);
379 RTStrFree(driveNameUtf8);
380 SysFreeString(driveName);
381 IHostFloppyDrive *floppyDriveTemp = floppyDrive;
382 floppyColl->GetNextHostFloppyDrive(floppyDriveTemp, &floppyDrive);
383 floppyDriveTemp->Release();
384 }
385 floppyColl->Release();
386 } else
387 {
388 printf("Could not get host floppy drive collection\n");
389 }
390 host->Release();
391 } else
392 {
393 printf("Call failed\n");
394 }
395 printf ("\n");
396#endif
397
398#if 0
399 // IVirtualBoxErrorInfo test
400 ////////////////////////////////////////////////////////////////////////////
401 {
402 // RPC calls
403
404 // call a method that will definitely fail
405 Guid uuid;
406 ComPtr <IHardDisk> hardDisk;
407 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
408 printf ("virtualBox->GetHardDisk(null-uuid)=%08X\n", rc);
409
410// {
411// com::ErrorInfo info (virtualBox);
412// PRINT_ERROR_INFO (info);
413// }
414
415 // call a method that will definitely succeed
416 Bstr version;
417 rc = virtualBox->COMGETTER(Version) (version.asOutParam());
418 printf ("virtualBox->COMGETTER(Version)=%08X\n", rc);
419
420 {
421 com::ErrorInfo info (virtualBox);
422 PRINT_ERROR_INFO (info);
423 }
424
425 // Local calls
426
427 // call a method that will definitely fail
428 ComPtr <IMachine> machine;
429 rc = session->COMGETTER(Machine)(machine.asOutParam());
430 printf ("session->COMGETTER(Machine)=%08X\n", rc);
431
432// {
433// com::ErrorInfo info (virtualBox);
434// PRINT_ERROR_INFO (info);
435// }
436
437 // call a method that will definitely succeed
438 SessionState_T state;
439 rc = session->COMGETTER(State) (&state);
440 printf ("session->COMGETTER(State)=%08X\n", rc);
441
442 {
443 com::ErrorInfo info (virtualBox);
444 PRINT_ERROR_INFO (info);
445 }
446 }
447#endif
448
449#if 0
450 // register the existing hard disk image
451 ///////////////////////////////////////////////////////////////////////////
452 do
453 {
454 ComPtr <IHardDisk> hd;
455 Bstr src = L"E:\\develop\\innotek\\images\\NewHardDisk.vdi";
456 printf ("Registerin the existing hard disk '%ls'...\n", src.raw());
457 CHECK_ERROR_BREAK (virtualBox, OpenHardDisk (src, hd.asOutParam()));
458 CHECK_ERROR_BREAK (virtualBox, RegisterHardDisk (hd));
459 }
460 while (FALSE);
461 printf ("\n");
462#endif
463
464#if 0
465 // find and unregister the existing hard disk image
466 ///////////////////////////////////////////////////////////////////////////
467 do
468 {
469 ComPtr <IVirtualDiskImage> vdi;
470 Bstr src = L"CreatorTest.vdi";
471 printf ("Unregistering the hard disk '%ls'...\n", src.raw());
472 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
473 ComPtr <IHardDisk> hd = vdi;
474 Guid id;
475 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
476 CHECK_ERROR_BREAK (virtualBox, UnregisterHardDisk (id, hd.asOutParam()));
477 }
478 while (FALSE);
479 printf ("\n");
480#endif
481
482#if 0
483 // clone the registered hard disk
484 ///////////////////////////////////////////////////////////////////////////
485 do
486 {
487#if defined RT_OS_LINUX
488 Bstr src = L"/mnt/hugaida/common/develop/innotek/images/freedos-linux.vdi";
489#else
490 Bstr src = L"E:/develop/innotek/images/freedos.vdi";
491#endif
492 Bstr dst = L"./clone.vdi";
493 RTPrintf ("Cloning '%ls' to '%ls'...\n", src.raw(), dst.raw());
494 ComPtr <IVirtualDiskImage> vdi;
495 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
496 ComPtr <IHardDisk> hd = vdi;
497 ComPtr <IProgress> progress;
498 CHECK_ERROR_BREAK (hd, CloneToImage (dst, vdi.asOutParam(), progress.asOutParam()));
499 RTPrintf ("Waiting for completion...\n");
500 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
501 ProgressErrorInfo ei (progress);
502 if (FAILED (ei.getResultCode()))
503 {
504 PRINT_ERROR_INFO (ei);
505 }
506 else
507 {
508 vdi->COMGETTER(FilePath) (dst.asOutParam());
509 RTPrintf ("Actual clone path is '%ls'\n", dst.raw());
510 }
511 }
512 while (FALSE);
513 printf ("\n");
514#endif
515
516#if 0
517 // find a registered hard disk by location
518 ///////////////////////////////////////////////////////////////////////////
519 do
520 {
521 ComPtr <IHardDisk> hd;
522 static const wchar_t *Names[] =
523 {
524#ifndef RT_OS_LINUX
525 L"E:/Develop/innotek/images/thinker/freedos.vdi",
526 L"E:/Develop/innotek/images/thinker/fReeDoS.vDI",
527 L"E:/Develop/innotek/images/vmdk/haiku.vmdk",
528#else
529 L"/mnt/host/common/Develop/innotek/images/maggot/freedos.vdi",
530 L"/mnt/host/common/Develop/innotek/images/maggot/fReeDoS.vDI",
531#endif
532 };
533 for (size_t i = 0; i < ELEMENTS (Names); ++ i)
534 {
535 Bstr src = Names [i];
536 printf ("Searching for hard disk '%ls'...\n", src.raw());
537 rc = virtualBox->FindHardDisk (src, hd.asOutParam());
538 if (SUCCEEDED (rc))
539 {
540 Guid id;
541 Bstr location;
542 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
543 CHECK_ERROR_BREAK (hd, COMGETTER(Location) (location.asOutParam()));
544 printf ("Found, UUID={%Vuuid}, location='%ls'.\n",
545 id.raw(), location.raw());
546 }
547 else
548 {
549 PRINT_ERROR_INFO (com::ErrorInfo (virtualBox));
550 }
551 }
552 }
553 while (FALSE);
554 printf ("\n");
555#endif
556
557#if 0
558 // access the machine in read-only mode
559 ///////////////////////////////////////////////////////////////////////////
560 do
561 {
562 ComPtr <IMachine> machine;
563 Bstr name = argc > 1 ? argv [1] : "dos";
564 printf ("Getting a machine object named '%ls'...\n", name.raw());
565 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
566 printf ("Accessing the machine in read-only mode:\n");
567 readAndChangeMachineSettings (machine);
568#if 0
569 if (argc != 2)
570 {
571 printf ("Error: a string has to be supplied!\n");
572 }
573 else
574 {
575 Bstr secureLabel = argv[1];
576 machine->COMSETTER(ExtraData)(L"VBoxSDL/SecureLabel", secureLabel);
577 }
578#endif
579 }
580 while (0);
581 printf ("\n");
582#endif
583
584#if 0
585 // create a new machine (w/o registering it)
586 ///////////////////////////////////////////////////////////////////////////
587 do
588 {
589 ComPtr <IMachine> machine;
590#if defined (RT_OS_LINUX)
591 Bstr baseDir = L"/tmp/vbox";
592#else
593 Bstr baseDir = L"C:\\vbox";
594#endif
595 Bstr name = L"machina";
596
597 printf ("Creating a new machine object (base dir '%ls', name '%ls')...\n",
598 baseDir.raw(), name.raw());
599 CHECK_ERROR_BREAK (virtualBox, CreateMachine (baseDir, name,
600 machine.asOutParam()));
601
602 printf ("Getting name...\n");
603 CHECK_ERROR_BREAK (machine, COMGETTER(Name) (name.asOutParam()));
604 printf ("Name: {%ls}\n", name.raw());
605
606 BOOL modified = FALSE;
607 printf ("Are any settings modified?...\n");
608 CHECK_ERROR_BREAK (machine, COMGETTER(SettingsModified) (&modified));
609 printf ("%s\n", modified ? "yes" : "no");
610
611 ASSERT_BREAK (modified == TRUE);
612
613 name = L"Kakaya prekrasnaya virtual'naya mashina!";
614 printf ("Setting new name ({%ls})...\n", name.raw());
615 CHECK_ERROR_BREAK (machine, COMSETTER(Name) (name));
616
617 printf ("Setting memory size to 111...\n");
618 CHECK_ERROR_BREAK (machine, COMSETTER(MemorySize) (111));
619
620 Bstr desc = L"This is an exemplary description.";
621 printf ("Setting description to \"%ls\"...\n", desc.raw());
622 CHECK_ERROR_BREAK (machine, COMSETTER(Description) (desc));
623
624 ComPtr <IGuestOSType> guestOSType;
625 Bstr type = L"os2warp45";
626 CHECK_ERROR_BREAK (virtualBox, GetGuestOSType (type, guestOSType.asOutParam()));
627
628 printf ("Saving new machine settings...\n");
629 CHECK_ERROR_BREAK (machine, SaveSettings());
630
631 printf ("Accessing the newly created machine:\n");
632 readAndChangeMachineSettings (machine);
633 }
634 while (FALSE);
635 printf ("\n");
636#endif
637
638#if 0
639 // enumerate host DVD drives
640 ///////////////////////////////////////////////////////////////////////////
641 do
642 {
643 ComPtr <IHost> host;
644 CHECK_RC_BREAK (virtualBox->COMGETTER(Host) (host.asOutParam()));
645
646 {
647 ComPtr <IHostDVDDriveCollection> coll;
648 CHECK_RC_BREAK (host->COMGETTER(DVDDrives) (coll.asOutParam()));
649 ComPtr <IHostDVDDriveEnumerator> enumerator;
650 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
651 BOOL hasmore;
652 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
653 {
654 ComPtr <IHostDVDDrive> drive;
655 CHECK_RC_BREAK (enumerator->GetNext (drive.asOutParam()));
656 Bstr name;
657 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
658 printf ("Host DVD drive: name={%ls}\n", name.raw());
659 }
660 CHECK_RC_BREAK (rc);
661
662 ComPtr <IHostDVDDrive> drive;
663 CHECK_ERROR (enumerator, GetNext (drive.asOutParam()));
664 CHECK_ERROR (coll, GetItemAt (1000, drive.asOutParam()));
665 CHECK_ERROR (coll, FindByName (Bstr ("R:"), drive.asOutParam()));
666 if (SUCCEEDED (rc))
667 {
668 Bstr name;
669 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
670 printf ("Found by name: name={%ls}\n", name.raw());
671 }
672 }
673 }
674 while (FALSE);
675 printf ("\n");
676#endif
677
678#if 0
679 // enumerate hard disks & dvd images
680 ///////////////////////////////////////////////////////////////////////////
681 do
682 {
683 {
684 ComPtr <IHardDiskCollection> coll;
685 CHECK_RC_BREAK (virtualBox->COMGETTER(HardDisks) (coll.asOutParam()));
686 ComPtr <IHardDiskEnumerator> enumerator;
687 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
688 BOOL hasmore;
689 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
690 {
691 ComPtr <IHardDisk> disk;
692 CHECK_RC_BREAK (enumerator->GetNext (disk.asOutParam()));
693 Guid id;
694 CHECK_RC_BREAK (disk->COMGETTER(Id) (id.asOutParam()));
695 Bstr path;
696 CHECK_RC_BREAK (disk->COMGETTER(FilePath) (path.asOutParam()));
697 printf ("Hard Disk: id={%s}, path={%ls}\n",
698 id.toString().raw(), path.raw());
699 Guid mid;
700 CHECK_RC_BREAK (
701 virtualBox->GetHardDiskUsage (id, ResourceUsage_AllUsage,
702 mid.asOutParam())
703 );
704 if (mid.isEmpty())
705 printf (" not used\n");
706 else
707 printf (" used by VM: {%s}\n", mid.toString().raw());
708 }
709 CHECK_RC_BREAK (rc);
710 }
711
712 {
713 ComPtr <IDVDImageCollection> coll;
714 CHECK_RC_BREAK (virtualBox->COMGETTER(DVDImages) (coll.asOutParam()));
715 ComPtr <IDVDImageEnumerator> enumerator;
716 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
717 BOOL hasmore;
718 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
719 {
720 ComPtr <IDVDImage> image;
721 CHECK_RC_BREAK (enumerator->GetNext (image.asOutParam()));
722 Guid id;
723 CHECK_RC_BREAK (image->COMGETTER(Id) (id.asOutParam()));
724 Bstr path;
725 CHECK_RC_BREAK (image->COMGETTER(FilePath) (path.asOutParam()));
726 printf ("CD/DVD Image: id={%s}, path={%ls}\n",
727 id.toString().raw(), path.raw());
728 Bstr mIDs;
729 CHECK_RC_BREAK (
730 virtualBox->GetDVDImageUsage (id, ResourceUsage_AllUsage,
731 mIDs.asOutParam())
732 );
733 if (mIDs.isNull())
734 printf (" not used\n");
735 else
736 printf (" used by VMs: {%ls}\n", mIDs.raw());
737 }
738 CHECK_RC_BREAK (rc);
739 }
740 }
741 while (FALSE);
742 printf ("\n");
743#endif
744
745#if 0
746 // open a (direct) session
747 ///////////////////////////////////////////////////////////////////////////
748 do
749 {
750 ComPtr <IMachine> machine;
751 Bstr name = argc > 1 ? argv [1] : "dos";
752 printf ("Getting a machine object named '%ls'...\n", name.raw());
753 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
754 Guid guid;
755 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
756 printf ("Opening a session for this machine...\n");
757 CHECK_RC_BREAK (virtualBox->OpenSession (session, guid));
758#if 1
759 ComPtr <IMachine> sessionMachine;
760 printf ("Getting sessioned machine object...\n");
761 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
762 printf ("Accessing the machine within the session:\n");
763 readAndChangeMachineSettings (sessionMachine, machine);
764#if 0
765 printf ("\n");
766 printf ("Enabling the VRDP server (must succeed even if the VM is saved):\n");
767 ComPtr <IVRDPServer> vrdp;
768 CHECK_ERROR_BREAK (sessionMachine, COMGETTER(VRDPServer) (vrdp.asOutParam()));
769 if (FAILED (vrdp->COMSETTER(Enabled) (TRUE)))
770 {
771 PRINT_ERROR_INFO (com::ErrorInfo (vrdp));
772 }
773 else
774 {
775 BOOL enabled = FALSE;
776 CHECK_ERROR_BREAK (vrdp, COMGETTER(Enabled) (&enabled));
777 printf ("VRDP server is %s\n", enabled ? "enabled" : "disabled");
778 }
779#endif
780#endif
781#if 0
782 ComPtr <IConsole> console;
783 printf ("Getting the console object...\n");
784 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
785 printf ("Discarding the current machine state...\n");
786 ComPtr <IProgress> progress;
787 CHECK_ERROR_BREAK (console, DiscardCurrentState (progress.asOutParam()));
788 printf ("Waiting for completion...\n");
789 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
790 ProgressErrorInfo ei (progress);
791 if (FAILED (ei.getResultCode()))
792 {
793 PRINT_ERROR_INFO (ei);
794
795 ComPtr <IUnknown> initiator;
796 CHECK_ERROR_BREAK (progress, COMGETTER(Initiator) (initiator.asOutParam()));
797
798 printf ("initiator(unk) = %p\n", (IUnknown *) initiator);
799 printf ("console(unk) = %p\n", (IUnknown *) ComPtr <IUnknown> ((IConsole *) console));
800 printf ("console = %p\n", (IConsole *) console);
801 }
802#endif
803 printf("Press enter to close session...");
804 getchar();
805 session->Close();
806 }
807 while (FALSE);
808 printf ("\n");
809#endif
810
811#if 0
812 // open a remote session
813 ///////////////////////////////////////////////////////////////////////////
814 do
815 {
816 ComPtr <IMachine> machine;
817 Bstr name = L"dos";
818 printf ("Getting a machine object named '%ls'...\n", name.raw());
819 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
820 Guid guid;
821 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
822 printf ("Opening a remote session for this machine...\n");
823 ComPtr <IProgress> progress;
824 CHECK_RC_BREAK (virtualBox->OpenRemoteSession (session, guid, Bstr("gui"),
825 progress.asOutParam()));
826 printf ("Waiting for the session to open...\n");
827 CHECK_RC_BREAK (progress->WaitForCompletion (-1));
828 ComPtr <IMachine> sessionMachine;
829 printf ("Getting sessioned machine object...\n");
830 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
831 ComPtr <IConsole> console;
832 printf ("Getting console object...\n");
833 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
834 printf ("Press enter to pause the VM execution in the remote session...");
835 getchar();
836 CHECK_RC (console->Pause());
837 printf ("Press enter to close this session...");
838 getchar();
839 session->Close();
840 }
841 while (FALSE);
842 printf ("\n");
843#endif
844
845#if 0
846 // open an existing remote session
847 ///////////////////////////////////////////////////////////////////////////
848 do
849 {
850 ComPtr <IMachine> machine;
851 Bstr name = "dos";
852 printf ("Getting a machine object named '%ls'...\n", name.raw());
853 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
854 Guid guid;
855 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
856 printf ("Opening an existing remote session for this machine...\n");
857 CHECK_RC_BREAK (virtualBox->OpenExistingSession (session, guid));
858 ComPtr <IMachine> sessionMachine;
859 printf ("Getting sessioned machine object...\n");
860 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
861
862#if 0
863 Bstr extraDataKey = "VBoxSDL/SecureLabel";
864 Bstr extraData = "Das kommt jetzt noch viel krasser vom total konkreten API!";
865 CHECK_RC (sessionMachine->SetExtraData (extraDataKey, extraData));
866#endif
867#if 0
868 ComPtr <IConsole> console;
869 printf ("Getting console object...\n");
870 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
871 printf ("Press enter to pause the VM execution in the remote session...");
872 getchar();
873 CHECK_RC (console->Pause());
874 printf ("Press enter to close this session...");
875 getchar();
876#endif
877 session->Close();
878 }
879 while (FALSE);
880 printf ("\n");
881#endif
882
883 printf ("Press enter to release Session and VirtualBox instances...");
884 getchar();
885
886 // end "all-stuff" scope
887 ////////////////////////////////////////////////////////////////////////////
888 }
889 while (0);
890
891 printf("Press enter to shutdown COM...");
892 getchar();
893
894 com::Shutdown();
895
896 printf ("tstAPI FINISHED.\n");
897
898 return rc;
899}
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