VirtualBox

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

Last change on this file since 16684 was 16568, checked in by vboxsync, 16 years ago

OVF: add warnings support; RAM is MB now

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.7 KB
Line 
1/** @file
2 *
3 * tstAPI - test program for our COM/XPCOM interface
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
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/array.h>
28#include <VBox/com/Guid.h>
29#include <VBox/com/ErrorInfo.h>
30#include <VBox/com/errorprint_legacy.h>
31#include <VBox/com/EventQueue.h>
32
33#include <VBox/com/VirtualBox.h>
34
35using namespace com;
36
37#define LOG_ENABLED
38#define LOG_GROUP LOG_GROUP_MAIN
39#define LOG_INSTANCE NULL
40#include <VBox/log.h>
41
42#include <iprt/initterm.h>
43#include <iprt/path.h>
44#include <iprt/param.h>
45#include <iprt/stream.h>
46
47#define printf RTPrintf
48
49
50// forward declarations
51///////////////////////////////////////////////////////////////////////////////
52
53static Bstr getObjectName(ComPtr<IVirtualBox> aVirtualBox,
54 ComPtr<IUnknown> aObject);
55static void queryMetrics (ComPtr<IVirtualBox> aVirtualBox,
56 ComPtr <IPerformanceCollector> collector,
57 ComSafeArrayIn (IUnknown *, objects));
58static void listAffectedMetrics(ComPtr<IVirtualBox> aVirtualBox,
59 ComSafeArrayIn(IPerformanceMetric*, aMetrics));
60
61// funcs
62///////////////////////////////////////////////////////////////////////////////
63
64HRESULT readAndChangeMachineSettings (IMachine *machine, IMachine *readonlyMachine = 0)
65{
66 HRESULT rc = S_OK;
67
68 Bstr name;
69 printf ("Getting machine name...\n");
70 CHECK_RC_RET (machine->COMGETTER(Name) (name.asOutParam()));
71 printf ("Name: {%ls}\n", name.raw());
72
73 printf("Getting machine GUID...\n");
74 Guid guid;
75 CHECK_RC (machine->COMGETTER(Id) (guid.asOutParam()));
76 if (SUCCEEDED (rc) && !guid.isEmpty()) {
77 printf ("Guid::toString(): {%s}\n", (const char *) guid.toString());
78 } else {
79 printf ("WARNING: there's no GUID!");
80 }
81
82 ULONG memorySize;
83 printf ("Getting memory size...\n");
84 CHECK_RC_RET (machine->COMGETTER(MemorySize) (&memorySize));
85 printf ("Memory size: %d\n", memorySize);
86
87 MachineState_T machineState;
88 printf ("Getting machine state...\n");
89 CHECK_RC_RET (machine->COMGETTER(State) (&machineState));
90 printf ("Machine state: %d\n", machineState);
91
92 BOOL modified;
93 printf ("Are any settings modified?...\n");
94 CHECK_RC (machine->COMGETTER(SettingsModified) (&modified));
95 if (SUCCEEDED (rc))
96 printf ("%s\n", modified ? "yes" : "no");
97
98 ULONG memorySizeBig = memorySize * 10;
99 printf("Changing memory size to %d...\n", memorySizeBig);
100 CHECK_RC (machine->COMSETTER(MemorySize) (memorySizeBig));
101
102 if (SUCCEEDED (rc))
103 {
104 printf ("Are any settings modified now?...\n");
105 CHECK_RC_RET (machine->COMGETTER(SettingsModified) (&modified));
106 printf ("%s\n", modified ? "yes" : "no");
107 ASSERT_RET (modified, 0);
108
109 ULONG memorySizeGot;
110 printf ("Getting memory size again...\n");
111 CHECK_RC_RET (machine->COMGETTER(MemorySize) (&memorySizeGot));
112 printf ("Memory size: %d\n", memorySizeGot);
113 ASSERT_RET (memorySizeGot == memorySizeBig, 0);
114
115 if (readonlyMachine)
116 {
117 printf ("Getting memory size of the counterpart readonly machine...\n");
118 ULONG memorySizeRO;
119 readonlyMachine->COMGETTER(MemorySize) (&memorySizeRO);
120 printf ("Memory size: %d\n", memorySizeRO);
121 ASSERT_RET (memorySizeRO != memorySizeGot, 0);
122 }
123
124 printf ("Discarding recent changes...\n");
125 CHECK_RC_RET (machine->DiscardSettings());
126 printf ("Are any settings modified after discarding?...\n");
127 CHECK_RC_RET (machine->COMGETTER(SettingsModified) (&modified));
128 printf ("%s\n", modified ? "yes" : "no");
129 ASSERT_RET (!modified, 0);
130
131 printf ("Getting memory size once more...\n");
132 CHECK_RC_RET (machine->COMGETTER(MemorySize) (&memorySizeGot));
133 printf ("Memory size: %d\n", memorySizeGot);
134 ASSERT_RET (memorySizeGot == memorySize, 0);
135
136 memorySize = memorySize > 128 ? memorySize / 2 : memorySize * 2;
137 printf("Changing memory size to %d...\n", memorySize);
138 CHECK_RC_RET (machine->COMSETTER(MemorySize) (memorySize));
139 }
140
141 Bstr desc;
142 printf ("Getting description...\n");
143 CHECK_ERROR_RET (machine, COMGETTER(Description) (desc.asOutParam()), rc);
144 printf ("Description is: \"%ls\"\n", desc.raw());
145
146 desc = L"This is an exemplary description (changed).";
147 printf ("Setting description to \"%ls\"...\n", desc.raw());
148 CHECK_ERROR_RET (machine, COMSETTER(Description) (desc), rc);
149
150 printf ("Saving machine settings...\n");
151 CHECK_RC (machine->SaveSettings());
152 if (SUCCEEDED (rc))
153 {
154 printf ("Are any settings modified after saving?...\n");
155 CHECK_RC_RET (machine->COMGETTER(SettingsModified) (&modified));
156 printf ("%s\n", modified ? "yes" : "no");
157 ASSERT_RET (!modified, 0);
158
159 if (readonlyMachine) {
160 printf ("Getting memory size of the counterpart readonly machine...\n");
161 ULONG memorySizeRO;
162 readonlyMachine->COMGETTER(MemorySize) (&memorySizeRO);
163 printf ("Memory size: %d\n", memorySizeRO);
164 ASSERT_RET (memorySizeRO == memorySize, 0);
165 }
166 }
167
168 Bstr extraDataKey = L"Blafasel";
169 Bstr extraData;
170 printf ("Getting extra data key {%ls}...\n", extraDataKey.raw());
171 CHECK_RC_RET (machine->GetExtraData (extraDataKey, extraData.asOutParam()));
172 if (!extraData.isEmpty()) {
173 printf ("Extra data value: {%ls}\n", extraData.raw());
174 } else {
175 if (extraData.isNull())
176 printf ("No extra data exists\n");
177 else
178 printf ("Extra data is empty\n");
179 }
180
181 if (extraData.isEmpty())
182 extraData = L"Das ist die Berliner Luft, Luft, Luft...";
183 else
184 extraData.setNull();
185 printf (
186 "Setting extra data key {%ls} to {%ls}...\n",
187 extraDataKey.raw(), extraData.raw()
188 );
189 CHECK_RC (machine->SetExtraData (extraDataKey, extraData));
190
191 if (SUCCEEDED (rc)) {
192 printf ("Getting extra data key {%ls} again...\n", extraDataKey.raw());
193 CHECK_RC_RET (machine->GetExtraData (extraDataKey, extraData.asOutParam()));
194 if (!extraData.isEmpty()) {
195 printf ("Extra data value: {%ls}\n", extraData.raw());
196 } else {
197 if (extraData.isNull())
198 printf ("No extra data exists\n");
199 else
200 printf ("Extra data is empty\n");
201 }
202 }
203
204 return rc;
205}
206
207// main
208///////////////////////////////////////////////////////////////////////////////
209
210int main(int argc, char *argv[])
211{
212 /*
213 * Initialize the VBox runtime without loading
214 * the support driver.
215 */
216 RTR3Init();
217
218 HRESULT rc;
219
220 {
221 char homeDir [RTPATH_MAX];
222 GetVBoxUserHomeDirectory (homeDir, sizeof (homeDir));
223 printf ("VirtualBox Home Directory = '%s'\n", homeDir);
224 }
225
226 printf ("Initializing COM...\n");
227
228 CHECK_RC_RET (com::Initialize());
229
230 do
231 {
232 // scopes all the stuff till shutdown
233 ////////////////////////////////////////////////////////////////////////////
234
235 ComPtr <IVirtualBox> virtualBox;
236 ComPtr <ISession> session;
237
238#if 0
239 // Utf8Str test
240 ////////////////////////////////////////////////////////////////////////////
241
242 Utf8Str nullUtf8Str;
243 printf ("nullUtf8Str='%s'\n", nullUtf8Str.raw());
244
245 Utf8Str simpleUtf8Str = "simpleUtf8Str";
246 printf ("simpleUtf8Str='%s'\n", simpleUtf8Str.raw());
247
248 Utf8Str utf8StrFmt = Utf8StrFmt ("[0=%d]%s[1=%d]",
249 0, "utf8StrFmt", 1);
250 printf ("utf8StrFmt='%s'\n", utf8StrFmt.raw());
251
252#endif
253
254 printf ("Creating VirtualBox object...\n");
255 CHECK_RC (virtualBox.createLocalObject (CLSID_VirtualBox));
256 if (FAILED (rc))
257 {
258 CHECK_ERROR_NOCALL();
259 break;
260 }
261
262 printf ("Creating Session object...\n");
263 CHECK_RC (session.createInprocObject (CLSID_Session));
264 if (FAILED (rc))
265 {
266 CHECK_ERROR_NOCALL();
267 break;
268 }
269
270#if 0
271 // IUnknown identity test
272 ////////////////////////////////////////////////////////////////////////////
273 {
274 {
275 ComPtr <IVirtualBox> virtualBox2;
276
277 printf ("Creating one more VirtualBox object...\n");
278 CHECK_RC (virtualBox2.createLocalObject (CLSID_VirtualBox));
279 if (FAILED (rc))
280 {
281 CHECK_ERROR_NOCALL();
282 break;
283 }
284
285 printf ("IVirtualBox(virtualBox)=%p IVirtualBox(virtualBox2)=%p\n",
286 (IVirtualBox *) virtualBox, (IVirtualBox *) virtualBox2);
287 Assert ((IVirtualBox *) virtualBox == (IVirtualBox *) virtualBox2);
288
289 ComPtr <IUnknown> unk (virtualBox);
290 ComPtr <IUnknown> unk2;
291 unk2 = virtualBox2;
292
293 printf ("IUnknown(virtualBox)=%p IUnknown(virtualBox2)=%p\n",
294 (IUnknown *) unk, (IUnknown *) unk2);
295 Assert ((IUnknown *) unk == (IUnknown *) unk2);
296
297 ComPtr <IVirtualBox> vb = unk;
298 ComPtr <IVirtualBox> vb2 = unk;
299
300 printf ("IVirtualBox(IUnknown(virtualBox))=%p IVirtualBox(IUnknown(virtualBox2))=%p\n",
301 (IVirtualBox *) vb, (IVirtualBox *) vb2);
302 Assert ((IVirtualBox *) vb == (IVirtualBox *) vb2);
303 }
304
305 {
306 ComPtr <IHost> host;
307 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host)(host.asOutParam()));
308 printf (" IHost(host)=%p\n", (IHost *) host);
309 ComPtr <IUnknown> unk = host;
310 printf (" IUnknown(host)=%p\n", (IUnknown *) unk);
311 ComPtr <IHost> host_copy = unk;
312 printf (" IHost(host_copy)=%p\n", (IHost *) host_copy);
313 ComPtr <IUnknown> unk_copy = host_copy;
314 printf (" IUnknown(host_copy)=%p\n", (IUnknown *) unk_copy);
315 Assert ((IUnknown *) unk == (IUnknown *) unk_copy);
316
317 /* query IUnknown on IUnknown */
318 ComPtr <IUnknown> unk_copy_copy;
319 unk_copy.queryInterfaceTo (unk_copy_copy.asOutParam());
320 printf (" IUnknown(unk_copy)=%p\n", (IUnknown *) unk_copy_copy);
321 Assert ((IUnknown *) unk_copy == (IUnknown *) unk_copy_copy);
322 /* query IUnknown on IUnknown in the opposite direction */
323 unk_copy_copy.queryInterfaceTo (unk_copy.asOutParam());
324 printf (" IUnknown(unk_copy_copy)=%p\n", (IUnknown *) unk_copy);
325 Assert ((IUnknown *) unk_copy == (IUnknown *) unk_copy_copy);
326
327 /* query IUnknown again after releasing all previous IUnknown instances
328 * but keeping IHost -- it should remain the same (Identity Rule) */
329 IUnknown *oldUnk = unk;
330 unk.setNull();
331 unk_copy.setNull();
332 unk_copy_copy.setNull();
333 unk = host;
334 printf (" IUnknown(host)=%p\n", (IUnknown *) unk);
335 Assert (oldUnk == (IUnknown *) unk);
336 }
337
338// printf ("Will be now released (press Enter)...");
339// getchar();
340 }
341#endif
342
343 // create the event queue
344 // (here it is necessary only to process remaining XPCOM/IPC events
345 // after the session is closed)
346 EventQueue eventQ;
347
348#if 0
349 // the simplest COM API test
350 ////////////////////////////////////////////////////////////////////////////
351 {
352 Bstr version;
353 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Version) (version.asOutParam()));
354 printf ("VirtualBox version = %ls\n", version.raw());
355 }
356#endif
357
358#if 0
359 // Array test
360 ////////////////////////////////////////////////////////////////////////////
361 {
362 printf ("Calling IVirtualBox::Machines...\n");
363
364 com::SafeIfaceArray <IMachine> machines;
365 CHECK_ERROR_BREAK (virtualBox,
366 COMGETTER(Machines2) (ComSafeArrayAsOutParam (machines)));
367
368 printf ("%u machines registered (machines.isNull()=%d).\n",
369 machines.size(), machines.isNull());
370
371 for (size_t i = 0; i < machines.size(); ++ i)
372 {
373 Bstr name;
374 CHECK_ERROR_BREAK (machines [i], COMGETTER(Name) (name.asOutParam()));
375 printf ("machines[%u]='%s'\n", i, Utf8Str (name).raw());
376 }
377
378#if 0
379 {
380 printf ("Testing [out] arrays...\n");
381 com::SafeGUIDArray uuids;
382 CHECK_ERROR_BREAK (virtualBox,
383 COMGETTER(Uuids) (ComSafeArrayAsOutParam (uuids)));
384
385 for (size_t i = 0; i < uuids.size(); ++ i)
386 printf ("uuids[%u]=%Vuuid\n", i, &uuids [i]);
387 }
388
389 {
390 printf ("Testing [in] arrays...\n");
391 com::SafeGUIDArray uuids (5);
392 for (size_t i = 0; i < uuids.size(); ++ i)
393 {
394 Guid id;
395 id.create();
396 uuids [i] = id;
397 printf ("uuids[%u]=%Vuuid\n", i, &uuids [i]);
398 }
399
400 CHECK_ERROR_BREAK (virtualBox,
401 SetUuids (ComSafeArrayAsInParam (uuids)));
402 }
403#endif
404
405 }
406#endif
407
408#if 0
409 // some outdated stuff
410 ////////////////////////////////////////////////////////////////////////////
411
412 printf("Getting IHost interface...\n");
413 IHost *host;
414 rc = virtualBox->GetHost(&host);
415 if (SUCCEEDED(rc))
416 {
417 IHostDVDDriveCollection *dvdColl;
418 rc = host->GetHostDVDDrives(&dvdColl);
419 if (SUCCEEDED(rc))
420 {
421 IHostDVDDrive *dvdDrive = NULL;
422 dvdColl->GetNextHostDVDDrive(dvdDrive, &dvdDrive);
423 while (dvdDrive)
424 {
425 BSTR driveName;
426 char *driveNameUtf8;
427 dvdDrive->GetDriveName(&driveName);
428 RTUtf16ToUtf8((PCRTUTF16)driveName, &driveNameUtf8);
429 printf("Host DVD drive name: %s\n", driveNameUtf8);
430 RTStrFree(driveNameUtf8);
431 SysFreeString(driveName);
432 IHostDVDDrive *dvdDriveTemp = dvdDrive;
433 dvdColl->GetNextHostDVDDrive(dvdDriveTemp, &dvdDrive);
434 dvdDriveTemp->Release();
435 }
436 dvdColl->Release();
437 } else
438 {
439 printf("Could not get host DVD drive collection\n");
440 }
441
442 IHostFloppyDriveCollection *floppyColl;
443 rc = host->GetHostFloppyDrives(&floppyColl);
444 if (SUCCEEDED(rc))
445 {
446 IHostFloppyDrive *floppyDrive = NULL;
447 floppyColl->GetNextHostFloppyDrive(floppyDrive, &floppyDrive);
448 while (floppyDrive)
449 {
450 BSTR driveName;
451 char *driveNameUtf8;
452 floppyDrive->GetDriveName(&driveName);
453 RTUtf16ToUtf8((PCRTUTF16)driveName, &driveNameUtf8);
454 printf("Host floppy drive name: %s\n", driveNameUtf8);
455 RTStrFree(driveNameUtf8);
456 SysFreeString(driveName);
457 IHostFloppyDrive *floppyDriveTemp = floppyDrive;
458 floppyColl->GetNextHostFloppyDrive(floppyDriveTemp, &floppyDrive);
459 floppyDriveTemp->Release();
460 }
461 floppyColl->Release();
462 } else
463 {
464 printf("Could not get host floppy drive collection\n");
465 }
466 host->Release();
467 } else
468 {
469 printf("Call failed\n");
470 }
471 printf ("\n");
472#endif
473
474#if 0
475 // IVirtualBoxErrorInfo test
476 ////////////////////////////////////////////////////////////////////////////
477 {
478 // RPC calls
479
480 // call a method that will definitely fail
481 Guid uuid;
482 ComPtr <IHardDisk> hardDisk;
483 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
484 printf ("virtualBox->GetHardDisk(null-uuid)=%08X\n", rc);
485
486// {
487// com::ErrorInfo info (virtualBox);
488// PRINT_ERROR_INFO (info);
489// }
490
491 // call a method that will definitely succeed
492 Bstr version;
493 rc = virtualBox->COMGETTER(Version) (version.asOutParam());
494 printf ("virtualBox->COMGETTER(Version)=%08X\n", rc);
495
496 {
497 com::ErrorInfo info (virtualBox);
498 PRINT_ERROR_INFO (info);
499 }
500
501 // Local calls
502
503 // call a method that will definitely fail
504 ComPtr <IMachine> machine;
505 rc = session->COMGETTER(Machine)(machine.asOutParam());
506 printf ("session->COMGETTER(Machine)=%08X\n", rc);
507
508// {
509// com::ErrorInfo info (virtualBox);
510// PRINT_ERROR_INFO (info);
511// }
512
513 // call a method that will definitely succeed
514 SessionState_T state;
515 rc = session->COMGETTER(State) (&state);
516 printf ("session->COMGETTER(State)=%08X\n", rc);
517
518 {
519 com::ErrorInfo info (virtualBox);
520 PRINT_ERROR_INFO (info);
521 }
522 }
523#endif
524
525#if 0
526 // register the existing hard disk image
527 ///////////////////////////////////////////////////////////////////////////
528 do
529 {
530 ComPtr <IHardDisk> hd;
531 Bstr src = L"E:\\develop\\innotek\\images\\NewHardDisk.vdi";
532 printf ("Opening the existing hard disk '%ls'...\n", src.raw());
533 CHECK_ERROR_BREAK (virtualBox, OpenHardDisk (src, hd.asOutParam()));
534 printf ("Enter to continue...\n");
535 getchar();
536 printf ("Registering the existing hard disk '%ls'...\n", src.raw());
537 CHECK_ERROR_BREAK (virtualBox, RegisterHardDisk (hd));
538 printf ("Enter to continue...\n");
539 getchar();
540 }
541 while (FALSE);
542 printf ("\n");
543#endif
544
545#if 0
546 // find and unregister the existing hard disk image
547 ///////////////////////////////////////////////////////////////////////////
548 do
549 {
550 ComPtr <IVirtualDiskImage> vdi;
551 Bstr src = L"CreatorTest.vdi";
552 printf ("Unregistering the hard disk '%ls'...\n", src.raw());
553 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
554 ComPtr <IHardDisk> hd = vdi;
555 Guid id;
556 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
557 CHECK_ERROR_BREAK (virtualBox, UnregisterHardDisk (id, hd.asOutParam()));
558 }
559 while (FALSE);
560 printf ("\n");
561#endif
562
563#if 0
564 // clone the registered hard disk
565 ///////////////////////////////////////////////////////////////////////////
566 do
567 {
568#if defined RT_OS_LINUX
569 Bstr src = L"/mnt/hugaida/common/develop/innotek/images/freedos-linux.vdi";
570#else
571 Bstr src = L"E:/develop/innotek/images/freedos.vdi";
572#endif
573 Bstr dst = L"./clone.vdi";
574 RTPrintf ("Cloning '%ls' to '%ls'...\n", src.raw(), dst.raw());
575 ComPtr <IVirtualDiskImage> vdi;
576 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
577 ComPtr <IHardDisk> hd = vdi;
578 ComPtr <IProgress> progress;
579 CHECK_ERROR_BREAK (hd, CloneToImage (dst, vdi.asOutParam(), progress.asOutParam()));
580 RTPrintf ("Waiting for completion...\n");
581 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
582 ProgressErrorInfo ei (progress);
583 if (FAILED (ei.getResultCode()))
584 {
585 PRINT_ERROR_INFO (ei);
586 }
587 else
588 {
589 vdi->COMGETTER(FilePath) (dst.asOutParam());
590 RTPrintf ("Actual clone path is '%ls'\n", dst.raw());
591 }
592 }
593 while (FALSE);
594 printf ("\n");
595#endif
596
597#if 0
598 // find a registered hard disk by location and get properties
599 ///////////////////////////////////////////////////////////////////////////
600 do
601 {
602 ComPtr <IHardDisk2> hd;
603 static const wchar_t *Names[] =
604 {
605#ifndef RT_OS_LINUX
606 L"freedos.vdi",
607 L"MS-DOS.vmdk",
608 L"iscsi",
609 L"some/path/and/disk.vdi",
610#else
611 L"xp.vdi",
612 L"Xp.vDI",
613#endif
614 };
615
616 printf ("\n");
617
618 for (size_t i = 0; i < RT_ELEMENTS (Names); ++ i)
619 {
620 Bstr src = Names [i];
621 printf ("Searching for hard disk '%ls'...\n", src.raw());
622 rc = virtualBox->FindHardDisk2 (src, hd.asOutParam());
623 if (SUCCEEDED (rc))
624 {
625 Guid id;
626 Bstr location;
627 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
628 CHECK_ERROR_BREAK (hd, COMGETTER(Location) (location.asOutParam()));
629 printf ("Found, UUID={%Vuuid}, location='%ls'.\n",
630 id.raw(), location.raw());
631
632 com::SafeArray <BSTR> names;
633 com::SafeArray <BSTR> values;
634
635 CHECK_ERROR_BREAK (hd, GetProperties (NULL,
636 ComSafeArrayAsOutParam (names),
637 ComSafeArrayAsOutParam (values)));
638
639 printf ("Properties:\n");
640 for (size_t i = 0; i < names.size(); ++ i)
641 printf (" %ls = %ls\n", names [i], values [i]);
642
643 if (names.size() == 0)
644 printf (" <none>\n");
645
646#if 0
647 Bstr name ("TargetAddress");
648 Bstr value = Utf8StrFmt ("lalala (%llu)", RTTimeMilliTS());
649
650 printf ("Settings property %ls to %ls...\n", name.raw(), value.raw());
651 CHECK_ERROR (hd, SetProperty (name, value));
652#endif
653 }
654 else
655 {
656 com::ErrorInfo info (virtualBox);
657 PRINT_ERROR_INFO (info);
658 }
659 printf ("\n");
660 }
661 }
662 while (FALSE);
663 printf ("\n");
664#endif
665
666#if 0
667 // access the machine in read-only mode
668 ///////////////////////////////////////////////////////////////////////////
669 do
670 {
671 ComPtr <IMachine> machine;
672 Bstr name = argc > 1 ? argv [1] : "dos";
673 printf ("Getting a machine object named '%ls'...\n", name.raw());
674 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
675 printf ("Accessing the machine in read-only mode:\n");
676 readAndChangeMachineSettings (machine);
677#if 0
678 if (argc != 2)
679 {
680 printf ("Error: a string has to be supplied!\n");
681 }
682 else
683 {
684 Bstr secureLabel = argv[1];
685 machine->COMSETTER(ExtraData)(L"VBoxSDL/SecureLabel", secureLabel);
686 }
687#endif
688 }
689 while (0);
690 printf ("\n");
691#endif
692
693#if 0
694 // create a new machine (w/o registering it)
695 ///////////////////////////////////////////////////////////////////////////
696 do
697 {
698 ComPtr <IMachine> machine;
699#if defined (RT_OS_LINUX)
700 Bstr baseDir = L"/tmp/vbox";
701#else
702 Bstr baseDir = L"C:\\vbox";
703#endif
704 Bstr name = L"machina";
705
706 printf ("Creating a new machine object (base dir '%ls', name '%ls')...\n",
707 baseDir.raw(), name.raw());
708 CHECK_ERROR_BREAK (virtualBox, CreateMachine (baseDir, name,
709 machine.asOutParam()));
710
711 printf ("Getting name...\n");
712 CHECK_ERROR_BREAK (machine, COMGETTER(Name) (name.asOutParam()));
713 printf ("Name: {%ls}\n", name.raw());
714
715 BOOL modified = FALSE;
716 printf ("Are any settings modified?...\n");
717 CHECK_ERROR_BREAK (machine, COMGETTER(SettingsModified) (&modified));
718 printf ("%s\n", modified ? "yes" : "no");
719
720 ASSERT_BREAK (modified == TRUE);
721
722 name = L"Kakaya prekrasnaya virtual'naya mashina!";
723 printf ("Setting new name ({%ls})...\n", name.raw());
724 CHECK_ERROR_BREAK (machine, COMSETTER(Name) (name));
725
726 printf ("Setting memory size to 111...\n");
727 CHECK_ERROR_BREAK (machine, COMSETTER(MemorySize) (111));
728
729 Bstr desc = L"This is an exemplary description.";
730 printf ("Setting description to \"%ls\"...\n", desc.raw());
731 CHECK_ERROR_BREAK (machine, COMSETTER(Description) (desc));
732
733 ComPtr <IGuestOSType> guestOSType;
734 Bstr type = L"os2warp45";
735 CHECK_ERROR_BREAK (virtualBox, GetGuestOSType (type, guestOSType.asOutParam()));
736
737 printf ("Saving new machine settings...\n");
738 CHECK_ERROR_BREAK (machine, SaveSettings());
739
740 printf ("Accessing the newly created machine:\n");
741 readAndChangeMachineSettings (machine);
742 }
743 while (FALSE);
744 printf ("\n");
745#endif
746
747#if 0
748 // enumerate host DVD drives
749 ///////////////////////////////////////////////////////////////////////////
750 do
751 {
752 ComPtr <IHost> host;
753 CHECK_RC_BREAK (virtualBox->COMGETTER(Host) (host.asOutParam()));
754
755 {
756 ComPtr <IHostDVDDriveCollection> coll;
757 CHECK_RC_BREAK (host->COMGETTER(DVDDrives) (coll.asOutParam()));
758 ComPtr <IHostDVDDriveEnumerator> enumerator;
759 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
760 BOOL hasmore;
761 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
762 {
763 ComPtr <IHostDVDDrive> drive;
764 CHECK_RC_BREAK (enumerator->GetNext (drive.asOutParam()));
765 Bstr name;
766 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
767 printf ("Host DVD drive: name={%ls}\n", name.raw());
768 }
769 CHECK_RC_BREAK (rc);
770
771 ComPtr <IHostDVDDrive> drive;
772 CHECK_ERROR (enumerator, GetNext (drive.asOutParam()));
773 CHECK_ERROR (coll, GetItemAt (1000, drive.asOutParam()));
774 CHECK_ERROR (coll, FindByName (Bstr ("R:"), drive.asOutParam()));
775 if (SUCCEEDED (rc))
776 {
777 Bstr name;
778 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
779 printf ("Found by name: name={%ls}\n", name.raw());
780 }
781 }
782 }
783 while (FALSE);
784 printf ("\n");
785#endif
786
787#if 0
788 // check for available hd backends
789 ///////////////////////////////////////////////////////////////////////////
790 {
791 RTPrintf("Supported hard disk backends: --------------------------\n");
792 ComPtr<ISystemProperties> systemProperties;
793 CHECK_ERROR_BREAK (virtualBox,
794 COMGETTER(SystemProperties) (systemProperties.asOutParam()));
795 com::SafeIfaceArray <IHardDiskFormat> hardDiskFormats;
796 CHECK_ERROR_BREAK (systemProperties,
797 COMGETTER(HardDiskFormats) (ComSafeArrayAsOutParam (hardDiskFormats)));
798
799 for (size_t i = 0; i < hardDiskFormats.size(); ++ i)
800 {
801 /* General information */
802 Bstr id;
803 CHECK_ERROR_BREAK (hardDiskFormats [i],
804 COMGETTER(Id) (id.asOutParam()));
805
806 Bstr description;
807 CHECK_ERROR_BREAK (hardDiskFormats [i],
808 COMGETTER(Id) (description.asOutParam()));
809
810 ULONG caps;
811 CHECK_ERROR_BREAK (hardDiskFormats [i],
812 COMGETTER(Capabilities) (&caps));
813
814 RTPrintf("Backend %u: id='%ls' description='%ls' capabilities=%#06x extensions='",
815 i, id.raw(), description.raw(), caps);
816
817 /* File extensions */
818 com::SafeArray <BSTR> fileExtensions;
819 CHECK_ERROR_BREAK (hardDiskFormats [i],
820 COMGETTER(FileExtensions) (ComSafeArrayAsOutParam (fileExtensions)));
821 for (size_t a = 0; a < fileExtensions.size(); ++ a)
822 {
823 RTPrintf ("%ls", Bstr (fileExtensions [a]).raw());
824 if (a != fileExtensions.size()-1)
825 RTPrintf (",");
826 }
827 RTPrintf ("'");
828
829 /* Configuration keys */
830 com::SafeArray <BSTR> propertyNames;
831 com::SafeArray <BSTR> propertyDescriptions;
832 com::SafeArray <ULONG> propertyTypes;
833 com::SafeArray <ULONG> propertyFlags;
834 com::SafeArray <BSTR> propertyDefaults;
835 CHECK_ERROR_BREAK (hardDiskFormats [i],
836 DescribeProperties (ComSafeArrayAsOutParam (propertyNames),
837 ComSafeArrayAsOutParam (propertyDescriptions),
838 ComSafeArrayAsOutParam (propertyTypes),
839 ComSafeArrayAsOutParam (propertyFlags),
840 ComSafeArrayAsOutParam (propertyDefaults)));
841
842 RTPrintf (" config=(");
843 if (propertyNames.size() > 0)
844 {
845 for (size_t a = 0; a < propertyNames.size(); ++ a)
846 {
847 RTPrintf ("key='%ls' desc='%ls' type=", Bstr (propertyNames [a]).raw(), Bstr (propertyDescriptions [a]).raw());
848 switch (propertyTypes [a])
849 {
850 case DataType_Int32Type: RTPrintf ("int"); break;
851 case DataType_Int8Type: RTPrintf ("byte"); break;
852 case DataType_StringType: RTPrintf ("string"); break;
853 }
854 RTPrintf (" flags=%#04x", propertyFlags [a]);
855 RTPrintf (" default='%ls'", Bstr (propertyDefaults [a]).raw());
856 if (a != propertyNames.size()-1)
857 RTPrintf (",");
858 }
859 }
860 RTPrintf (")\n");
861 }
862 RTPrintf("-------------------------------------------------------\n");
863 }
864#endif
865
866#if 0
867 // enumerate hard disks & dvd images
868 ///////////////////////////////////////////////////////////////////////////
869 do
870 {
871 {
872 com::SafeIfaceArray <IHardDisk2> disks;
873 CHECK_ERROR_BREAK (virtualBox,
874 COMGETTER(HardDisks2) (ComSafeArrayAsOutParam (disks)));
875
876 printf ("%u base hard disks registered (disks.isNull()=%d).\n",
877 disks.size(), disks.isNull());
878
879 for (size_t i = 0; i < disks.size(); ++ i)
880 {
881 Bstr loc;
882 CHECK_ERROR_BREAK (disks [i], COMGETTER(Location) (loc.asOutParam()));
883 Guid id;
884 CHECK_ERROR_BREAK (disks [i], COMGETTER(Id) (id.asOutParam()));
885 MediaState_T state;
886 CHECK_ERROR_BREAK (disks [i], COMGETTER(State) (&state));
887 Bstr format;
888 CHECK_ERROR_BREAK (disks [i], COMGETTER(Format) (format.asOutParam()));
889
890 printf (" disks[%u]: '%ls'\n"
891 " UUID: {%Vuuid}\n"
892 " State: %s\n"
893 " Format: %ls\n",
894 i, loc.raw(), id.raw(),
895 state == MediaState_NotCreated ? "Not Created" :
896 state == MediaState_Created ? "Created" :
897 state == MediaState_Inaccessible ? "Inaccessible" :
898 state == MediaState_LockedRead ? "Locked Read" :
899 state == MediaState_LockedWrite ? "Locked Write" :
900 "???",
901 format.raw());
902
903 if (state == MediaState_Inaccessible)
904 {
905 Bstr error;
906 CHECK_ERROR_BREAK (disks [i],
907 COMGETTER(LastAccessError)(error.asOutParam()));
908 printf (" Access Error: %ls\n", error.raw());
909 }
910
911 /* get usage */
912
913 printf (" Used by VMs:\n");
914
915 com::SafeGUIDArray ids;
916 CHECK_ERROR_BREAK (disks [i],
917 COMGETTER(MachineIds) (ComSafeArrayAsOutParam (ids)));
918 if (ids.size() == 0)
919 {
920 printf (" <not used>\n");
921 }
922 else
923 {
924 for (size_t j = 0; j < ids.size(); ++ j)
925 {
926 printf (" {%Vuuid}\n", &ids [i]);
927 }
928 }
929 }
930 }
931 {
932 com::SafeIfaceArray <IDVDImage2> images;
933 CHECK_ERROR_BREAK (virtualBox,
934 COMGETTER(DVDImages) (ComSafeArrayAsOutParam (images)));
935
936 printf ("%u DVD images registered (images.isNull()=%d).\n",
937 images.size(), images.isNull());
938
939 for (size_t i = 0; i < images.size(); ++ i)
940 {
941 Bstr loc;
942 CHECK_ERROR_BREAK (images [i], COMGETTER(Location) (loc.asOutParam()));
943 Guid id;
944 CHECK_ERROR_BREAK (images [i], COMGETTER(Id) (id.asOutParam()));
945 MediaState_T state;
946 CHECK_ERROR_BREAK (images [i], COMGETTER(State) (&state));
947
948 printf (" images[%u]: '%ls'\n"
949 " UUID: {%Vuuid}\n"
950 " State: %s\n",
951 i, loc.raw(), id.raw(),
952 state == MediaState_NotCreated ? "Not Created" :
953 state == MediaState_Created ? "Created" :
954 state == MediaState_Inaccessible ? "Inaccessible" :
955 state == MediaState_LockedRead ? "Locked Read" :
956 state == MediaState_LockedWrite ? "Locked Write" :
957 "???");
958
959 if (state == MediaState_Inaccessible)
960 {
961 Bstr error;
962 CHECK_ERROR_BREAK (images [i],
963 COMGETTER(LastAccessError)(error.asOutParam()));
964 printf (" Access Error: %ls\n", error.raw());
965 }
966
967 /* get usage */
968
969 printf (" Used by VMs:\n");
970
971 com::SafeGUIDArray ids;
972 CHECK_ERROR_BREAK (images [i],
973 COMGETTER(MachineIds) (ComSafeArrayAsOutParam (ids)));
974 if (ids.size() == 0)
975 {
976 printf (" <not used>\n");
977 }
978 else
979 {
980 for (size_t j = 0; j < ids.size(); ++ j)
981 {
982 printf (" {%Vuuid}\n", &ids [i]);
983 }
984 }
985 }
986 }
987 }
988 while (FALSE);
989 printf ("\n");
990#endif
991
992#if 0
993 // open a (direct) session
994 ///////////////////////////////////////////////////////////////////////////
995 do
996 {
997 ComPtr <IMachine> machine;
998 Bstr name = argc > 1 ? argv [1] : "dos";
999 printf ("Getting a machine object named '%ls'...\n", name.raw());
1000 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
1001 Guid guid;
1002 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
1003 printf ("Opening a session for this machine...\n");
1004 CHECK_RC_BREAK (virtualBox->OpenSession (session, guid));
1005#if 1
1006 ComPtr <IMachine> sessionMachine;
1007 printf ("Getting sessioned machine object...\n");
1008 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
1009 printf ("Accessing the machine within the session:\n");
1010 readAndChangeMachineSettings (sessionMachine, machine);
1011#if 0
1012 printf ("\n");
1013 printf ("Enabling the VRDP server (must succeed even if the VM is saved):\n");
1014 ComPtr <IVRDPServer> vrdp;
1015 CHECK_ERROR_BREAK (sessionMachine, COMGETTER(VRDPServer) (vrdp.asOutParam()));
1016 if (FAILED (vrdp->COMSETTER(Enabled) (TRUE)))
1017 {
1018 PRINT_ERROR_INFO (com::ErrorInfo (vrdp));
1019 }
1020 else
1021 {
1022 BOOL enabled = FALSE;
1023 CHECK_ERROR_BREAK (vrdp, COMGETTER(Enabled) (&enabled));
1024 printf ("VRDP server is %s\n", enabled ? "enabled" : "disabled");
1025 }
1026#endif
1027#endif
1028#if 0
1029 ComPtr <IConsole> console;
1030 printf ("Getting the console object...\n");
1031 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
1032 printf ("Discarding the current machine state...\n");
1033 ComPtr <IProgress> progress;
1034 CHECK_ERROR_BREAK (console, DiscardCurrentState (progress.asOutParam()));
1035 printf ("Waiting for completion...\n");
1036 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
1037 ProgressErrorInfo ei (progress);
1038 if (FAILED (ei.getResultCode()))
1039 {
1040 PRINT_ERROR_INFO (ei);
1041
1042 ComPtr <IUnknown> initiator;
1043 CHECK_ERROR_BREAK (progress, COMGETTER(Initiator) (initiator.asOutParam()));
1044
1045 printf ("initiator(unk) = %p\n", (IUnknown *) initiator);
1046 printf ("console(unk) = %p\n", (IUnknown *) ComPtr <IUnknown> ((IConsole *) console));
1047 printf ("console = %p\n", (IConsole *) console);
1048 }
1049#endif
1050 printf("Press enter to close session...");
1051 getchar();
1052 session->Close();
1053 }
1054 while (FALSE);
1055 printf ("\n");
1056#endif
1057
1058#if 0
1059 // open a remote session
1060 ///////////////////////////////////////////////////////////////////////////
1061 do
1062 {
1063 ComPtr <IMachine> machine;
1064 Bstr name = L"dos";
1065 printf ("Getting a machine object named '%ls'...\n", name.raw());
1066 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
1067 Guid guid;
1068 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
1069 printf ("Opening a remote session for this machine...\n");
1070 ComPtr <IProgress> progress;
1071 CHECK_RC_BREAK (virtualBox->OpenRemoteSession (session, guid, Bstr("gui"),
1072 NULL, progress.asOutParam()));
1073 printf ("Waiting for the session to open...\n");
1074 CHECK_RC_BREAK (progress->WaitForCompletion (-1));
1075 ComPtr <IMachine> sessionMachine;
1076 printf ("Getting sessioned machine object...\n");
1077 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
1078 ComPtr <IConsole> console;
1079 printf ("Getting console object...\n");
1080 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
1081 printf ("Press enter to pause the VM execution in the remote session...");
1082 getchar();
1083 CHECK_RC (console->Pause());
1084 printf ("Press enter to close this session...");
1085 getchar();
1086 session->Close();
1087 }
1088 while (FALSE);
1089 printf ("\n");
1090#endif
1091
1092#if 0
1093 // open an existing remote session
1094 ///////////////////////////////////////////////////////////////////////////
1095 do
1096 {
1097 ComPtr <IMachine> machine;
1098 Bstr name = "dos";
1099 printf ("Getting a machine object named '%ls'...\n", name.raw());
1100 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
1101 Guid guid;
1102 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
1103 printf ("Opening an existing remote session for this machine...\n");
1104 CHECK_RC_BREAK (virtualBox->OpenExistingSession (session, guid));
1105 ComPtr <IMachine> sessionMachine;
1106 printf ("Getting sessioned machine object...\n");
1107 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
1108
1109#if 0
1110 Bstr extraDataKey = "VBoxSDL/SecureLabel";
1111 Bstr extraData = "Das kommt jetzt noch viel krasser vom total konkreten API!";
1112 CHECK_RC (sessionMachine->SetExtraData (extraDataKey, extraData));
1113#endif
1114#if 0
1115 ComPtr <IConsole> console;
1116 printf ("Getting console object...\n");
1117 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
1118 printf ("Press enter to pause the VM execution in the remote session...");
1119 getchar();
1120 CHECK_RC (console->Pause());
1121 printf ("Press enter to close this session...");
1122 getchar();
1123#endif
1124 session->Close();
1125 }
1126 while (FALSE);
1127 printf ("\n");
1128#endif
1129
1130#if 0
1131 do {
1132 // Get host
1133 ComPtr <IHost> host;
1134 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host) (host.asOutParam()));
1135
1136 ULONG uMemSize, uMemAvail;
1137 CHECK_ERROR_BREAK (host, COMGETTER(MemorySize) (&uMemSize));
1138 printf("Total memory (MB): %u\n", uMemSize);
1139 CHECK_ERROR_BREAK (host, COMGETTER(MemoryAvailable) (&uMemAvail));
1140 printf("Free memory (MB): %u\n", uMemAvail);
1141 } while (0);
1142#endif
1143
1144#if 0
1145 do {
1146 // Get host
1147 ComPtr <IHost> host;
1148 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host) (host.asOutParam()));
1149
1150 com::SafeIfaceArray <IHostNetworkInterface> hostNetworkInterfaces;
1151 CHECK_ERROR_BREAK(host,
1152 COMGETTER(NetworkInterfaces) (ComSafeArrayAsOutParam (hostNetworkInterfaces)));
1153 if (hostNetworkInterfaces.size() > 0)
1154 {
1155 ComPtr<IHostNetworkInterface> networkInterface = hostNetworkInterfaces[0];
1156 Bstr interfaceName;
1157 networkInterface->COMGETTER(Name)(interfaceName.asOutParam());
1158 printf("Found %d network interfaces, testing with %lS...\n", hostNetworkInterfaces.size(), interfaceName.raw());
1159 Guid interfaceGuid;
1160 networkInterface->COMGETTER(Id)(interfaceGuid.asOutParam());
1161 // Find the interface by its name
1162 networkInterface.setNull();
1163 CHECK_ERROR_BREAK(host,
1164 FindHostNetworkInterfaceByName (interfaceName, networkInterface.asOutParam()));
1165 Guid interfaceGuid2;
1166 networkInterface->COMGETTER(Id)(interfaceGuid2.asOutParam());
1167 if (interfaceGuid2 != interfaceGuid)
1168 printf("Failed to retrieve an interface by name %lS.\n", interfaceName.raw());
1169 // Find the interface by its guid
1170 networkInterface.setNull();
1171 CHECK_ERROR_BREAK(host,
1172 FindHostNetworkInterfaceById (interfaceGuid, networkInterface.asOutParam()));
1173 Bstr interfaceName2;
1174 networkInterface->COMGETTER(Name)(interfaceName2.asOutParam());
1175 if (interfaceName != interfaceName2)
1176 printf("Failed to retrieve an interface by GUID %lS.\n", Bstr(interfaceGuid.toString()).raw());
1177 }
1178 else
1179 {
1180 printf("No network interfaces found!\n");
1181 }
1182 } while (0);
1183#endif
1184
1185#if 0 && defined (VBOX_WITH_RESOURCE_USAGE_API)
1186 do {
1187 // Get collector
1188 ComPtr <IPerformanceCollector> collector;
1189 CHECK_ERROR_BREAK (virtualBox,
1190 COMGETTER(PerformanceCollector) (collector.asOutParam()));
1191
1192
1193 // Fill base metrics array
1194 Bstr baseMetricNames[] = { L"CPU/Load,RAM/Usage" };
1195 com::SafeArray<BSTR> baseMetrics (1);
1196 baseMetricNames[0].cloneTo (&baseMetrics [0]);
1197
1198 // Get host
1199 ComPtr <IHost> host;
1200 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host) (host.asOutParam()));
1201
1202 // Get machine
1203 ComPtr <IMachine> machine;
1204 Bstr name = argc > 1 ? argv [1] : "dsl";
1205 Bstr sessionType = argc > 2 ? argv [2] : "vrdp";
1206 printf ("Getting a machine object named '%ls'...\n", name.raw());
1207 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
1208
1209 // Open session
1210 Guid guid;
1211 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
1212 printf ("Opening a remote session for this machine...\n");
1213 ComPtr <IProgress> progress;
1214 CHECK_RC_BREAK (virtualBox->OpenRemoteSession (session, guid, sessionType,
1215 NULL, progress.asOutParam()));
1216 printf ("Waiting for the session to open...\n");
1217 CHECK_RC_BREAK (progress->WaitForCompletion (-1));
1218 ComPtr <IMachine> sessionMachine;
1219 printf ("Getting sessioned machine object...\n");
1220 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
1221
1222 // Setup base metrics
1223 // Note that one needs to set up metrics after a session is open for a machine.
1224 com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
1225 com::SafeIfaceArray<IUnknown> objects(2);
1226 host.queryInterfaceTo(&objects[0]);
1227 machine.queryInterfaceTo(&objects[1]);
1228 CHECK_ERROR_BREAK (collector, SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
1229 ComSafeArrayAsInParam(objects), 1u, 10u,
1230 ComSafeArrayAsOutParam(affectedMetrics)) );
1231 listAffectedMetrics(virtualBox,
1232 ComSafeArrayAsInParam(affectedMetrics));
1233 affectedMetrics.setNull();
1234
1235 // Get console
1236 ComPtr <IConsole> console;
1237 printf ("Getting console object...\n");
1238 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
1239
1240 RTThreadSleep(5000); // Sleep for 5 seconds
1241
1242 printf("\nMetrics collected with VM running: --------------------\n");
1243 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1244
1245 // Pause
1246 //printf ("Press enter to pause the VM execution in the remote session...");
1247 //getchar();
1248 CHECK_RC (console->Pause());
1249
1250 RTThreadSleep(5000); // Sleep for 5 seconds
1251
1252 printf("\nMetrics collected with VM paused: ---------------------\n");
1253 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1254
1255 printf("\nDrop collected metrics: ----------------------------------------\n");
1256 CHECK_ERROR_BREAK (collector,
1257 SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
1258 ComSafeArrayAsInParam(objects),
1259 1u, 5u, ComSafeArrayAsOutParam(affectedMetrics)) );
1260 listAffectedMetrics(virtualBox,
1261 ComSafeArrayAsInParam(affectedMetrics));
1262 affectedMetrics.setNull();
1263 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1264
1265 com::SafeIfaceArray<IUnknown> vmObject(1);
1266 machine.queryInterfaceTo(&vmObject[0]);
1267
1268 printf("\nDisable collection of VM metrics: ------------------------------\n");
1269 CHECK_ERROR_BREAK (collector,
1270 DisableMetrics(ComSafeArrayAsInParam(baseMetrics),
1271 ComSafeArrayAsInParam(vmObject),
1272 ComSafeArrayAsOutParam(affectedMetrics)) );
1273 listAffectedMetrics(virtualBox,
1274 ComSafeArrayAsInParam(affectedMetrics));
1275 affectedMetrics.setNull();
1276 RTThreadSleep(5000); // Sleep for 5 seconds
1277 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1278
1279 printf("\nRe-enable collection of all metrics: ---------------------------\n");
1280 CHECK_ERROR_BREAK (collector,
1281 EnableMetrics(ComSafeArrayAsInParam(baseMetrics),
1282 ComSafeArrayAsInParam(objects),
1283 ComSafeArrayAsOutParam(affectedMetrics)) );
1284 listAffectedMetrics(virtualBox,
1285 ComSafeArrayAsInParam(affectedMetrics));
1286 affectedMetrics.setNull();
1287 RTThreadSleep(5000); // Sleep for 5 seconds
1288 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1289
1290 // Power off
1291 printf ("Press enter to power off VM...");
1292 getchar();
1293 CHECK_RC (console->PowerDown());
1294 printf ("Press enter to close this session...");
1295 getchar();
1296 session->Close();
1297 } while (false);
1298#endif /* VBOX_WITH_RESOURCE_USAGE_API */
1299#if 0
1300 // check of OVF appliance handling
1301 ///////////////////////////////////////////////////////////////////////////
1302 do
1303 {
1304 Bstr ovf = argc > 1 ? argv [1] : "someOVF.ovf";
1305 RTPrintf ("Try to open %ls ...\n", ovf.raw());
1306
1307 ComPtr <IAppliance> appliance;
1308 CHECK_ERROR_BREAK (virtualBox,
1309 OpenAppliance (ovf, appliance.asOutParam()));
1310 Bstr path;
1311 CHECK_ERROR_BREAK (appliance, COMGETTER (Path)(path.asOutParam()));
1312 RTPrintf ("Successfully opened %ls.\n", path.raw());
1313 CHECK_ERROR_BREAK (appliance,
1314 Interpret());
1315 RTPrintf ("Successfully interpreted %ls.\n", path.raw());
1316 RTPrintf ("Appliance:\n");
1317 // Fetch all disks
1318 com::SafeArray<BSTR> retDisks;
1319 CHECK_ERROR_BREAK (appliance,
1320 COMGETTER (Disks)(ComSafeArrayAsOutParam (retDisks)));
1321 if (retDisks.size() > 0)
1322 {
1323 RTPrintf ("Disks:");
1324 for (unsigned i = 0; i < retDisks.size(); i++)
1325 printf (" %ls", Bstr (retDisks [i]).raw());
1326 RTPrintf ("\n");
1327 }
1328 /* Fetch all virtual system descriptions */
1329 com::SafeIfaceArray<IVirtualSystemDescription> retVSD;
1330 CHECK_ERROR_BREAK (appliance,
1331 COMGETTER (VirtualSystemDescriptions) (ComSafeArrayAsOutParam (retVSD)));
1332 if (retVSD.size() > 0)
1333 {
1334 for (unsigned i = 0; i < retVSD.size(); ++i)
1335 {
1336 com::SafeArray<VirtualSystemDescriptionType_T> retTypes;
1337 com::SafeArray<BSTR> retRefValues;
1338 com::SafeArray<BSTR> retOrigValues;
1339 com::SafeArray<BSTR> retAutoValues;
1340 com::SafeArray<BSTR> retConfiguration;
1341 CHECK_ERROR_BREAK (retVSD [i],
1342 GetDescription (ComSafeArrayAsOutParam (retTypes),
1343 ComSafeArrayAsOutParam (retRefValues),
1344 ComSafeArrayAsOutParam (retOrigValues),
1345 ComSafeArrayAsOutParam (retAutoValues),
1346 ComSafeArrayAsOutParam (retConfiguration)));
1347
1348 RTPrintf ("VirtualSystemDescription:\n");
1349 for (unsigned a = 0; a < retTypes.size(); ++a)
1350 {
1351 printf (" %d %ls %ls %ls\n",
1352 retTypes [a],
1353 Bstr (retOrigValues [a]).raw(),
1354 Bstr (retAutoValues [a]).raw(),
1355 Bstr (retConfiguration [a]).raw());
1356 }
1357 /* Show warnings from interpret */
1358 com::SafeArray<BSTR> retWarnings;
1359 CHECK_ERROR_BREAK (retVSD [i],
1360 GetWarnings(ComSafeArrayAsOutParam (retWarnings)));
1361 if (retWarnings.size() > 0)
1362 {
1363 RTPrintf ("The following warnings occurs on interpret:\n");
1364 for (unsigned r = 0; r < retWarnings.size(); ++r)
1365 RTPrintf ("%ls\n", Bstr (retWarnings [r]).raw());
1366 RTPrintf ("\n");
1367 }
1368 }
1369 RTPrintf ("\n");
1370 }
1371 RTPrintf ("Try to import the appliance ...\n");
1372 ComPtr<IProgress> progress;
1373 CHECK_ERROR_BREAK (appliance,
1374 ImportAppliance (progress.asOutParam()));
1375 CHECK_ERROR (progress, WaitForCompletion (-1));
1376 if (SUCCEEDED (rc))
1377 {
1378 /* Check if the import was successfully */
1379 progress->COMGETTER (ResultCode)(&rc);
1380 if (FAILED (rc))
1381 {
1382 com::ProgressErrorInfo info (progress);
1383 if (info.isBasicAvailable())
1384 RTPrintf ("Error: failed to import appliance. Error message: %lS\n", info.getText().raw());
1385 else
1386 RTPrintf ("Error: failed to import appliance. No error message available!\n");
1387 }else
1388 RTPrintf ("Successfully imported the appliance.\n");
1389 }
1390
1391 }
1392 while (FALSE);
1393 printf ("\n");
1394#endif
1395
1396 printf ("Press enter to release Session and VirtualBox instances...");
1397 getchar();
1398
1399 // end "all-stuff" scope
1400 ////////////////////////////////////////////////////////////////////////////
1401 }
1402 while (0);
1403
1404 printf("Press enter to shutdown COM...");
1405 getchar();
1406
1407 com::Shutdown();
1408
1409 printf ("tstAPI FINISHED.\n");
1410
1411 return rc;
1412}
1413
1414#ifdef VBOX_WITH_RESOURCE_USAGE_API
1415static void queryMetrics (ComPtr<IVirtualBox> aVirtualBox,
1416 ComPtr <IPerformanceCollector> collector,
1417 ComSafeArrayIn (IUnknown *, objects))
1418{
1419 HRESULT rc;
1420
1421 //Bstr metricNames[] = { L"CPU/Load/User:avg,CPU/Load/System:avg,CPU/Load/Idle:avg,RAM/Usage/Total,RAM/Usage/Used:avg" };
1422 Bstr metricNames[] = { L"*" };
1423 com::SafeArray<BSTR> metrics (1);
1424 metricNames[0].cloneTo (&metrics [0]);
1425 com::SafeArray<BSTR> retNames;
1426 com::SafeIfaceArray<IUnknown> retObjects;
1427 com::SafeArray<BSTR> retUnits;
1428 com::SafeArray<ULONG> retScales;
1429 com::SafeArray<ULONG> retSequenceNumbers;
1430 com::SafeArray<ULONG> retIndices;
1431 com::SafeArray<ULONG> retLengths;
1432 com::SafeArray<LONG> retData;
1433 CHECK_ERROR (collector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
1434 ComSafeArrayInArg(objects),
1435 ComSafeArrayAsOutParam(retNames),
1436 ComSafeArrayAsOutParam(retObjects),
1437 ComSafeArrayAsOutParam(retUnits),
1438 ComSafeArrayAsOutParam(retScales),
1439 ComSafeArrayAsOutParam(retSequenceNumbers),
1440 ComSafeArrayAsOutParam(retIndices),
1441 ComSafeArrayAsOutParam(retLengths),
1442 ComSafeArrayAsOutParam(retData)) );
1443 RTPrintf("Object Metric Values\n"
1444 "---------- -------------------- --------------------------------------------\n");
1445 for (unsigned i = 0; i < retNames.size(); i++)
1446 {
1447 Bstr metricUnit(retUnits[i]);
1448 Bstr metricName(retNames[i]);
1449 RTPrintf("%-10ls %-20ls ", getObjectName(aVirtualBox, retObjects[i]).raw(), metricName.raw());
1450 const char *separator = "";
1451 for (unsigned j = 0; j < retLengths[i]; j++)
1452 {
1453 if (retScales[i] == 1)
1454 RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
1455 else
1456 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
1457 (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
1458 separator = ", ";
1459 }
1460 RTPrintf("\n");
1461 }
1462}
1463
1464static Bstr getObjectName(ComPtr<IVirtualBox> aVirtualBox,
1465 ComPtr<IUnknown> aObject)
1466{
1467 HRESULT rc;
1468
1469 ComPtr<IHost> host = aObject;
1470 if (!host.isNull())
1471 return Bstr("host");
1472
1473 ComPtr<IMachine> machine = aObject;
1474 if (!machine.isNull())
1475 {
1476 Bstr name;
1477 CHECK_ERROR(machine, COMGETTER(Name)(name.asOutParam()));
1478 if (SUCCEEDED(rc))
1479 return name;
1480 }
1481 return Bstr("unknown");
1482}
1483
1484static void listAffectedMetrics(ComPtr<IVirtualBox> aVirtualBox,
1485 ComSafeArrayIn(IPerformanceMetric*, aMetrics))
1486{
1487 HRESULT rc;
1488 com::SafeIfaceArray<IPerformanceMetric> metrics(ComSafeArrayInArg(aMetrics));
1489 if (metrics.size())
1490 {
1491 ComPtr<IUnknown> object;
1492 Bstr metricName;
1493 RTPrintf("The following metrics were modified:\n\n"
1494 "Object Metric\n"
1495 "---------- --------------------\n");
1496 for (size_t i = 0; i < metrics.size(); i++)
1497 {
1498 CHECK_ERROR(metrics[i], COMGETTER(Object)(object.asOutParam()));
1499 CHECK_ERROR(metrics[i], COMGETTER(MetricName)(metricName.asOutParam()));
1500 RTPrintf("%-10ls %-20ls\n",
1501 getObjectName(aVirtualBox, object).raw(), metricName.raw());
1502 }
1503 RTPrintf("\n");
1504 }
1505 else
1506 {
1507 RTPrintf("No metrics match the specified filter!\n");
1508 }
1509}
1510
1511#endif /* VBOX_WITH_RESOURCE_USAGE_API */
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