VirtualBox

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

Last change on this file since 14683 was 14596, checked in by vboxsync, 16 years ago

Main: Implemented IHardDisk2::getProperty()/setProperty().

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