VirtualBox

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

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

*: s/%Vuuid/%RTuuid/g; s/%Vrc/%Rrc/g

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