VirtualBox

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

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

UCS-2 -> UTF-16.

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