VirtualBox

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

Last change on this file since 31570 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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