VirtualBox

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

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

Biggest check-in ever. New source code headers for all (C) innotek files.

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