VirtualBox

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

Last change on this file since 45733 was 43445, checked in by vboxsync, 12 years ago

Main/Metrics: Host network metrics, linux only (#6345)

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