VirtualBox

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

Last change on this file since 5361 was 5028, checked in by vboxsync, 18 years ago

Main: Improved the hard disk testcase.

  • 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
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 0
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 1
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 ("Opening the existing hard disk '%ls'...\n", src.raw());
453 CHECK_ERROR_BREAK (virtualBox, OpenHardDisk (src, hd.asOutParam()));
454 printf ("Enter to continue...\n");
455 getchar();
456 printf ("Registering the existing hard disk '%ls'...\n", src.raw());
457 CHECK_ERROR_BREAK (virtualBox, RegisterHardDisk (hd));
458 printf ("Enter to continue...\n");
459 getchar();
460 }
461 while (FALSE);
462 printf ("\n");
463#endif
464
465#if 0
466 // find and unregister the existing hard disk image
467 ///////////////////////////////////////////////////////////////////////////
468 do
469 {
470 ComPtr <IVirtualDiskImage> vdi;
471 Bstr src = L"CreatorTest.vdi";
472 printf ("Unregistering the hard disk '%ls'...\n", src.raw());
473 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
474 ComPtr <IHardDisk> hd = vdi;
475 Guid id;
476 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
477 CHECK_ERROR_BREAK (virtualBox, UnregisterHardDisk (id, hd.asOutParam()));
478 }
479 while (FALSE);
480 printf ("\n");
481#endif
482
483#if 0
484 // clone the registered hard disk
485 ///////////////////////////////////////////////////////////////////////////
486 do
487 {
488#if defined RT_OS_LINUX
489 Bstr src = L"/mnt/hugaida/common/develop/innotek/images/freedos-linux.vdi";
490#else
491 Bstr src = L"E:/develop/innotek/images/freedos.vdi";
492#endif
493 Bstr dst = L"./clone.vdi";
494 RTPrintf ("Cloning '%ls' to '%ls'...\n", src.raw(), dst.raw());
495 ComPtr <IVirtualDiskImage> vdi;
496 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
497 ComPtr <IHardDisk> hd = vdi;
498 ComPtr <IProgress> progress;
499 CHECK_ERROR_BREAK (hd, CloneToImage (dst, vdi.asOutParam(), progress.asOutParam()));
500 RTPrintf ("Waiting for completion...\n");
501 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
502 ProgressErrorInfo ei (progress);
503 if (FAILED (ei.getResultCode()))
504 {
505 PRINT_ERROR_INFO (ei);
506 }
507 else
508 {
509 vdi->COMGETTER(FilePath) (dst.asOutParam());
510 RTPrintf ("Actual clone path is '%ls'\n", dst.raw());
511 }
512 }
513 while (FALSE);
514 printf ("\n");
515#endif
516
517#if 0
518 // find a registered hard disk by location
519 ///////////////////////////////////////////////////////////////////////////
520 do
521 {
522 ComPtr <IHardDisk> hd;
523 static const wchar_t *Names[] =
524 {
525#ifndef RT_OS_LINUX
526 L"E:/Develop/innotek/images/thinker/freedos.vdi",
527 L"E:/Develop/innotek/images/thinker/fReeDoS.vDI",
528 L"E:/Develop/innotek/images/vmdk/haiku.vmdk",
529#else
530 L"/mnt/host/common/Develop/innotek/images/maggot/freedos.vdi",
531 L"/mnt/host/common/Develop/innotek/images/maggot/fReeDoS.vDI",
532#endif
533 };
534 for (size_t i = 0; i < ELEMENTS (Names); ++ i)
535 {
536 Bstr src = Names [i];
537 printf ("Searching for hard disk '%ls'...\n", src.raw());
538 rc = virtualBox->FindHardDisk (src, hd.asOutParam());
539 if (SUCCEEDED (rc))
540 {
541 Guid id;
542 Bstr location;
543 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
544 CHECK_ERROR_BREAK (hd, COMGETTER(Location) (location.asOutParam()));
545 printf ("Found, UUID={%Vuuid}, location='%ls'.\n",
546 id.raw(), location.raw());
547 }
548 else
549 {
550 PRINT_ERROR_INFO (com::ErrorInfo (virtualBox));
551 }
552 }
553 }
554 while (FALSE);
555 printf ("\n");
556#endif
557
558#if 0
559 // access the machine in read-only mode
560 ///////////////////////////////////////////////////////////////////////////
561 do
562 {
563 ComPtr <IMachine> machine;
564 Bstr name = argc > 1 ? argv [1] : "dos";
565 printf ("Getting a machine object named '%ls'...\n", name.raw());
566 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
567 printf ("Accessing the machine in read-only mode:\n");
568 readAndChangeMachineSettings (machine);
569#if 0
570 if (argc != 2)
571 {
572 printf ("Error: a string has to be supplied!\n");
573 }
574 else
575 {
576 Bstr secureLabel = argv[1];
577 machine->COMSETTER(ExtraData)(L"VBoxSDL/SecureLabel", secureLabel);
578 }
579#endif
580 }
581 while (0);
582 printf ("\n");
583#endif
584
585#if 0
586 // create a new machine (w/o registering it)
587 ///////////////////////////////////////////////////////////////////////////
588 do
589 {
590 ComPtr <IMachine> machine;
591#if defined (RT_OS_LINUX)
592 Bstr baseDir = L"/tmp/vbox";
593#else
594 Bstr baseDir = L"C:\\vbox";
595#endif
596 Bstr name = L"machina";
597
598 printf ("Creating a new machine object (base dir '%ls', name '%ls')...\n",
599 baseDir.raw(), name.raw());
600 CHECK_ERROR_BREAK (virtualBox, CreateMachine (baseDir, name,
601 machine.asOutParam()));
602
603 printf ("Getting name...\n");
604 CHECK_ERROR_BREAK (machine, COMGETTER(Name) (name.asOutParam()));
605 printf ("Name: {%ls}\n", name.raw());
606
607 BOOL modified = FALSE;
608 printf ("Are any settings modified?...\n");
609 CHECK_ERROR_BREAK (machine, COMGETTER(SettingsModified) (&modified));
610 printf ("%s\n", modified ? "yes" : "no");
611
612 ASSERT_BREAK (modified == TRUE);
613
614 name = L"Kakaya prekrasnaya virtual'naya mashina!";
615 printf ("Setting new name ({%ls})...\n", name.raw());
616 CHECK_ERROR_BREAK (machine, COMSETTER(Name) (name));
617
618 printf ("Setting memory size to 111...\n");
619 CHECK_ERROR_BREAK (machine, COMSETTER(MemorySize) (111));
620
621 Bstr desc = L"This is an exemplary description.";
622 printf ("Setting description to \"%ls\"...\n", desc.raw());
623 CHECK_ERROR_BREAK (machine, COMSETTER(Description) (desc));
624
625 ComPtr <IGuestOSType> guestOSType;
626 Bstr type = L"os2warp45";
627 CHECK_ERROR_BREAK (virtualBox, GetGuestOSType (type, guestOSType.asOutParam()));
628
629 printf ("Saving new machine settings...\n");
630 CHECK_ERROR_BREAK (machine, SaveSettings());
631
632 printf ("Accessing the newly created machine:\n");
633 readAndChangeMachineSettings (machine);
634 }
635 while (FALSE);
636 printf ("\n");
637#endif
638
639#if 0
640 // enumerate host DVD drives
641 ///////////////////////////////////////////////////////////////////////////
642 do
643 {
644 ComPtr <IHost> host;
645 CHECK_RC_BREAK (virtualBox->COMGETTER(Host) (host.asOutParam()));
646
647 {
648 ComPtr <IHostDVDDriveCollection> coll;
649 CHECK_RC_BREAK (host->COMGETTER(DVDDrives) (coll.asOutParam()));
650 ComPtr <IHostDVDDriveEnumerator> enumerator;
651 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
652 BOOL hasmore;
653 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
654 {
655 ComPtr <IHostDVDDrive> drive;
656 CHECK_RC_BREAK (enumerator->GetNext (drive.asOutParam()));
657 Bstr name;
658 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
659 printf ("Host DVD drive: name={%ls}\n", name.raw());
660 }
661 CHECK_RC_BREAK (rc);
662
663 ComPtr <IHostDVDDrive> drive;
664 CHECK_ERROR (enumerator, GetNext (drive.asOutParam()));
665 CHECK_ERROR (coll, GetItemAt (1000, drive.asOutParam()));
666 CHECK_ERROR (coll, FindByName (Bstr ("R:"), drive.asOutParam()));
667 if (SUCCEEDED (rc))
668 {
669 Bstr name;
670 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
671 printf ("Found by name: name={%ls}\n", name.raw());
672 }
673 }
674 }
675 while (FALSE);
676 printf ("\n");
677#endif
678
679#if 0
680 // enumerate hard disks & dvd images
681 ///////////////////////////////////////////////////////////////////////////
682 do
683 {
684 {
685 ComPtr <IHardDiskCollection> coll;
686 CHECK_RC_BREAK (virtualBox->COMGETTER(HardDisks) (coll.asOutParam()));
687 ComPtr <IHardDiskEnumerator> enumerator;
688 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
689 BOOL hasmore;
690 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
691 {
692 ComPtr <IHardDisk> disk;
693 CHECK_RC_BREAK (enumerator->GetNext (disk.asOutParam()));
694 Guid id;
695 CHECK_RC_BREAK (disk->COMGETTER(Id) (id.asOutParam()));
696 Bstr path;
697 CHECK_RC_BREAK (disk->COMGETTER(FilePath) (path.asOutParam()));
698 printf ("Hard Disk: id={%s}, path={%ls}\n",
699 id.toString().raw(), path.raw());
700 Guid mid;
701 CHECK_RC_BREAK (
702 virtualBox->GetHardDiskUsage (id, ResourceUsage_AllUsage,
703 mid.asOutParam())
704 );
705 if (mid.isEmpty())
706 printf (" not used\n");
707 else
708 printf (" used by VM: {%s}\n", mid.toString().raw());
709 }
710 CHECK_RC_BREAK (rc);
711 }
712
713 {
714 ComPtr <IDVDImageCollection> coll;
715 CHECK_RC_BREAK (virtualBox->COMGETTER(DVDImages) (coll.asOutParam()));
716 ComPtr <IDVDImageEnumerator> enumerator;
717 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
718 BOOL hasmore;
719 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
720 {
721 ComPtr <IDVDImage> image;
722 CHECK_RC_BREAK (enumerator->GetNext (image.asOutParam()));
723 Guid id;
724 CHECK_RC_BREAK (image->COMGETTER(Id) (id.asOutParam()));
725 Bstr path;
726 CHECK_RC_BREAK (image->COMGETTER(FilePath) (path.asOutParam()));
727 printf ("CD/DVD Image: id={%s}, path={%ls}\n",
728 id.toString().raw(), path.raw());
729 Bstr mIDs;
730 CHECK_RC_BREAK (
731 virtualBox->GetDVDImageUsage (id, ResourceUsage_AllUsage,
732 mIDs.asOutParam())
733 );
734 if (mIDs.isNull())
735 printf (" not used\n");
736 else
737 printf (" used by VMs: {%ls}\n", mIDs.raw());
738 }
739 CHECK_RC_BREAK (rc);
740 }
741 }
742 while (FALSE);
743 printf ("\n");
744#endif
745
746#if 0
747 // open a (direct) session
748 ///////////////////////////////////////////////////////////////////////////
749 do
750 {
751 ComPtr <IMachine> machine;
752 Bstr name = argc > 1 ? argv [1] : "dos";
753 printf ("Getting a machine object named '%ls'...\n", name.raw());
754 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
755 Guid guid;
756 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
757 printf ("Opening a session for this machine...\n");
758 CHECK_RC_BREAK (virtualBox->OpenSession (session, guid));
759#if 1
760 ComPtr <IMachine> sessionMachine;
761 printf ("Getting sessioned machine object...\n");
762 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
763 printf ("Accessing the machine within the session:\n");
764 readAndChangeMachineSettings (sessionMachine, machine);
765#if 0
766 printf ("\n");
767 printf ("Enabling the VRDP server (must succeed even if the VM is saved):\n");
768 ComPtr <IVRDPServer> vrdp;
769 CHECK_ERROR_BREAK (sessionMachine, COMGETTER(VRDPServer) (vrdp.asOutParam()));
770 if (FAILED (vrdp->COMSETTER(Enabled) (TRUE)))
771 {
772 PRINT_ERROR_INFO (com::ErrorInfo (vrdp));
773 }
774 else
775 {
776 BOOL enabled = FALSE;
777 CHECK_ERROR_BREAK (vrdp, COMGETTER(Enabled) (&enabled));
778 printf ("VRDP server is %s\n", enabled ? "enabled" : "disabled");
779 }
780#endif
781#endif
782#if 0
783 ComPtr <IConsole> console;
784 printf ("Getting the console object...\n");
785 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
786 printf ("Discarding the current machine state...\n");
787 ComPtr <IProgress> progress;
788 CHECK_ERROR_BREAK (console, DiscardCurrentState (progress.asOutParam()));
789 printf ("Waiting for completion...\n");
790 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
791 ProgressErrorInfo ei (progress);
792 if (FAILED (ei.getResultCode()))
793 {
794 PRINT_ERROR_INFO (ei);
795
796 ComPtr <IUnknown> initiator;
797 CHECK_ERROR_BREAK (progress, COMGETTER(Initiator) (initiator.asOutParam()));
798
799 printf ("initiator(unk) = %p\n", (IUnknown *) initiator);
800 printf ("console(unk) = %p\n", (IUnknown *) ComPtr <IUnknown> ((IConsole *) console));
801 printf ("console = %p\n", (IConsole *) console);
802 }
803#endif
804 printf("Press enter to close session...");
805 getchar();
806 session->Close();
807 }
808 while (FALSE);
809 printf ("\n");
810#endif
811
812#if 0
813 // open a remote session
814 ///////////////////////////////////////////////////////////////////////////
815 do
816 {
817 ComPtr <IMachine> machine;
818 Bstr name = L"dos";
819 printf ("Getting a machine object named '%ls'...\n", name.raw());
820 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
821 Guid guid;
822 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
823 printf ("Opening a remote session for this machine...\n");
824 ComPtr <IProgress> progress;
825 CHECK_RC_BREAK (virtualBox->OpenRemoteSession (session, guid, Bstr("gui"),
826 NULL, progress.asOutParam()));
827 printf ("Waiting for the session to open...\n");
828 CHECK_RC_BREAK (progress->WaitForCompletion (-1));
829 ComPtr <IMachine> sessionMachine;
830 printf ("Getting sessioned machine object...\n");
831 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
832 ComPtr <IConsole> console;
833 printf ("Getting console object...\n");
834 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
835 printf ("Press enter to pause the VM execution in the remote session...");
836 getchar();
837 CHECK_RC (console->Pause());
838 printf ("Press enter to close this session...");
839 getchar();
840 session->Close();
841 }
842 while (FALSE);
843 printf ("\n");
844#endif
845
846#if 0
847 // open an existing remote session
848 ///////////////////////////////////////////////////////////////////////////
849 do
850 {
851 ComPtr <IMachine> machine;
852 Bstr name = "dos";
853 printf ("Getting a machine object named '%ls'...\n", name.raw());
854 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
855 Guid guid;
856 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
857 printf ("Opening an existing remote session for this machine...\n");
858 CHECK_RC_BREAK (virtualBox->OpenExistingSession (session, guid));
859 ComPtr <IMachine> sessionMachine;
860 printf ("Getting sessioned machine object...\n");
861 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
862
863#if 0
864 Bstr extraDataKey = "VBoxSDL/SecureLabel";
865 Bstr extraData = "Das kommt jetzt noch viel krasser vom total konkreten API!";
866 CHECK_RC (sessionMachine->SetExtraData (extraDataKey, extraData));
867#endif
868#if 0
869 ComPtr <IConsole> console;
870 printf ("Getting console object...\n");
871 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
872 printf ("Press enter to pause the VM execution in the remote session...");
873 getchar();
874 CHECK_RC (console->Pause());
875 printf ("Press enter to close this session...");
876 getchar();
877#endif
878 session->Close();
879 }
880 while (FALSE);
881 printf ("\n");
882#endif
883
884 printf ("Press enter to release Session and VirtualBox instances...");
885 getchar();
886
887 // end "all-stuff" scope
888 ////////////////////////////////////////////////////////////////////////////
889 }
890 while (0);
891
892 printf("Press enter to shutdown COM...");
893 getchar();
894
895 com::Shutdown();
896
897 printf ("tstAPI FINISHED.\n");
898
899 return rc;
900}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette