VirtualBox

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

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

Main/VirtualBox: new parameter for overriding VM file existence check on creation (2nd try)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.6 KB
Line 
1/** @file
2 *
3 * tstAPI - test program for our COM/XPCOM interface
4 */
5
6/*
7 * Copyright (C) 2006-2010 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 (name, L"", baseDir, L"",
745 false,
746 machine.asOutParam()));
747
748 RTPrintf ("Getting name...\n");
749 CHECK_ERROR_BREAK (machine, COMGETTER(Name) (name.asOutParam()));
750 RTPrintf ("Name: {%ls}\n", name.raw());
751
752 BOOL modified = FALSE;
753 RTPrintf ("Are any settings modified?...\n");
754 CHECK_ERROR_BREAK (machine, COMGETTER(SettingsModified) (&modified));
755 RTPrintf ("%s\n", modified ? "yes" : "no");
756
757 ASSERT_BREAK (modified == TRUE);
758
759 name = L"Kakaya prekrasnaya virtual'naya mashina!";
760 RTPrintf ("Setting new name ({%ls})...\n", name.raw());
761 CHECK_ERROR_BREAK (machine, COMSETTER(Name) (name));
762
763 RTPrintf ("Setting memory size to 111...\n");
764 CHECK_ERROR_BREAK (machine, COMSETTER(MemorySize) (111));
765
766 Bstr desc = L"This is an exemplary description.";
767 RTPrintf ("Setting description to \"%ls\"...\n", desc.raw());
768 CHECK_ERROR_BREAK (machine, COMSETTER(Description) (desc));
769
770 ComPtr<IGuestOSType> guestOSType;
771 Bstr type = L"os2warp45";
772 CHECK_ERROR_BREAK (virtualBox, GetGuestOSType (type, guestOSType.asOutParam()));
773
774 RTPrintf ("Saving new machine settings...\n");
775 CHECK_ERROR_BREAK (machine, SaveSettings());
776
777 RTPrintf ("Accessing the newly created machine:\n");
778 readAndChangeMachineSettings (machine);
779 }
780 while (FALSE);
781 RTPrintf ("\n");
782#endif
783
784#if 0
785 // enumerate host DVD drives
786 ///////////////////////////////////////////////////////////////////////////
787 do
788 {
789 ComPtr<IHost> host;
790 CHECK_RC_BREAK (virtualBox->COMGETTER(Host) (host.asOutParam()));
791
792 {
793 ComPtr<IHostDVDDriveCollection> coll;
794 CHECK_RC_BREAK (host->COMGETTER(DVDDrives) (coll.asOutParam()));
795 ComPtr<IHostDVDDriveEnumerator> enumerator;
796 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
797 BOOL hasmore;
798 while (SUCCEEDED(enumerator->HasMore (&hasmore)) && hasmore)
799 {
800 ComPtr<IHostDVDDrive> drive;
801 CHECK_RC_BREAK (enumerator->GetNext (drive.asOutParam()));
802 Bstr name;
803 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
804 RTPrintf ("Host DVD drive: name={%ls}\n", name.raw());
805 }
806 CHECK_RC_BREAK (rc);
807
808 ComPtr<IHostDVDDrive> drive;
809 CHECK_ERROR (enumerator, GetNext (drive.asOutParam()));
810 CHECK_ERROR (coll, GetItemAt (1000, drive.asOutParam()));
811 CHECK_ERROR (coll, FindByName (Bstr ("R:"), drive.asOutParam()));
812 if (SUCCEEDED(rc))
813 {
814 Bstr name;
815 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
816 RTPrintf ("Found by name: name={%ls}\n", name.raw());
817 }
818 }
819 }
820 while (FALSE);
821 RTPrintf ("\n");
822#endif
823
824#if 0
825 // check for available hd backends
826 ///////////////////////////////////////////////////////////////////////////
827 {
828 RTPrintf("Supported hard disk backends: --------------------------\n");
829 ComPtr<ISystemProperties> systemProperties;
830 CHECK_ERROR_BREAK (virtualBox,
831 COMGETTER(SystemProperties) (systemProperties.asOutParam()));
832 com::SafeIfaceArray<IHardDiskFormat> hardDiskFormats;
833 CHECK_ERROR_BREAK (systemProperties,
834 COMGETTER(HardDiskFormats) (ComSafeArrayAsOutParam (hardDiskFormats)));
835
836 for (size_t i = 0; i < hardDiskFormats.size(); ++ i)
837 {
838 /* General information */
839 Bstr id;
840 CHECK_ERROR_BREAK (hardDiskFormats [i],
841 COMGETTER(Id) (id.asOutParam()));
842
843 Bstr description;
844 CHECK_ERROR_BREAK (hardDiskFormats [i],
845 COMGETTER(Id) (description.asOutParam()));
846
847 ULONG caps;
848 CHECK_ERROR_BREAK (hardDiskFormats [i],
849 COMGETTER(Capabilities) (&caps));
850
851 RTPrintf("Backend %u: id='%ls' description='%ls' capabilities=%#06x extensions='",
852 i, id.raw(), description.raw(), caps);
853
854 /* File extensions */
855 com::SafeArray<BSTR> fileExtensions;
856 CHECK_ERROR_BREAK (hardDiskFormats [i],
857 COMGETTER(FileExtensions) (ComSafeArrayAsOutParam (fileExtensions)));
858 for (size_t a = 0; a < fileExtensions.size(); ++ a)
859 {
860 RTPrintf ("%ls", Bstr (fileExtensions [a]).raw());
861 if (a != fileExtensions.size()-1)
862 RTPrintf (",");
863 }
864 RTPrintf ("'");
865
866 /* Configuration keys */
867 com::SafeArray<BSTR> propertyNames;
868 com::SafeArray<BSTR> propertyDescriptions;
869 com::SafeArray<ULONG> propertyTypes;
870 com::SafeArray<ULONG> propertyFlags;
871 com::SafeArray<BSTR> propertyDefaults;
872 CHECK_ERROR_BREAK (hardDiskFormats [i],
873 DescribeProperties (ComSafeArrayAsOutParam (propertyNames),
874 ComSafeArrayAsOutParam (propertyDescriptions),
875 ComSafeArrayAsOutParam (propertyTypes),
876 ComSafeArrayAsOutParam (propertyFlags),
877 ComSafeArrayAsOutParam (propertyDefaults)));
878
879 RTPrintf (" config=(");
880 if (propertyNames.size() > 0)
881 {
882 for (size_t a = 0; a < propertyNames.size(); ++ a)
883 {
884 RTPrintf ("key='%ls' desc='%ls' type=", Bstr (propertyNames [a]).raw(), Bstr (propertyDescriptions [a]).raw());
885 switch (propertyTypes [a])
886 {
887 case DataType_Int32Type: RTPrintf ("int"); break;
888 case DataType_Int8Type: RTPrintf ("byte"); break;
889 case DataType_StringType: RTPrintf ("string"); break;
890 }
891 RTPrintf (" flags=%#04x", propertyFlags [a]);
892 RTPrintf (" default='%ls'", Bstr (propertyDefaults [a]).raw());
893 if (a != propertyNames.size()-1)
894 RTPrintf (",");
895 }
896 }
897 RTPrintf (")\n");
898 }
899 RTPrintf("-------------------------------------------------------\n");
900 }
901#endif
902
903#if 0
904 // enumerate hard disks & dvd images
905 ///////////////////////////////////////////////////////////////////////////
906 do
907 {
908 {
909 com::SafeIfaceArray<IHardDisk> disks;
910 CHECK_ERROR_BREAK (virtualBox,
911 COMGETTER(HardDisks)(ComSafeArrayAsOutParam (disks)));
912
913 RTPrintf ("%u base hard disks registered (disks.isNull()=%d).\n",
914 disks.size(), disks.isNull());
915
916 for (size_t i = 0; i < disks.size(); ++ i)
917 {
918 Bstr loc;
919 CHECK_ERROR_BREAK (disks [i], COMGETTER(Location) (loc.asOutParam()));
920 Guid id;
921 CHECK_ERROR_BREAK (disks [i], COMGETTER(Id) (id.asOutParam()));
922 MediaState_T state;
923 CHECK_ERROR_BREAK (disks [i], COMGETTER(State) (&state));
924 Bstr format;
925 CHECK_ERROR_BREAK (disks [i], COMGETTER(Format) (format.asOutParam()));
926
927 RTPrintf (" disks[%u]: '%ls'\n"
928 " UUID: {%RTuuid}\n"
929 " State: %s\n"
930 " Format: %ls\n",
931 i, loc.raw(), id.raw(),
932 state == MediaState_NotCreated ? "Not Created" :
933 state == MediaState_Created ? "Created" :
934 state == MediaState_Inaccessible ? "Inaccessible" :
935 state == MediaState_LockedRead ? "Locked Read" :
936 state == MediaState_LockedWrite ? "Locked Write" :
937 "???",
938 format.raw());
939
940 if (state == MediaState_Inaccessible)
941 {
942 Bstr error;
943 CHECK_ERROR_BREAK (disks [i],
944 COMGETTER(LastAccessError)(error.asOutParam()));
945 RTPrintf (" Access Error: %ls\n", error.raw());
946 }
947
948 /* get usage */
949
950 RTPrintf (" Used by VMs:\n");
951
952 com::SafeGUIDArray ids;
953 CHECK_ERROR_BREAK (disks [i],
954 COMGETTER(MachineIds) (ComSafeArrayAsOutParam (ids)));
955 if (ids.size() == 0)
956 {
957 RTPrintf (" <not used>\n");
958 }
959 else
960 {
961 for (size_t j = 0; j < ids.size(); ++ j)
962 {
963 RTPrintf (" {%RTuuid}\n", &ids [j]);
964 }
965 }
966 }
967 }
968 {
969 com::SafeIfaceArray<IDVDImage> images;
970 CHECK_ERROR_BREAK (virtualBox,
971 COMGETTER(DVDImages) (ComSafeArrayAsOutParam (images)));
972
973 RTPrintf ("%u DVD images registered (images.isNull()=%d).\n",
974 images.size(), images.isNull());
975
976 for (size_t i = 0; i < images.size(); ++ i)
977 {
978 Bstr loc;
979 CHECK_ERROR_BREAK (images [i], COMGETTER(Location) (loc.asOutParam()));
980 Guid id;
981 CHECK_ERROR_BREAK (images [i], COMGETTER(Id) (id.asOutParam()));
982 MediaState_T state;
983 CHECK_ERROR_BREAK (images [i], COMGETTER(State) (&state));
984
985 RTPrintf (" images[%u]: '%ls'\n"
986 " UUID: {%RTuuid}\n"
987 " State: %s\n",
988 i, loc.raw(), id.raw(),
989 state == MediaState_NotCreated ? "Not Created" :
990 state == MediaState_Created ? "Created" :
991 state == MediaState_Inaccessible ? "Inaccessible" :
992 state == MediaState_LockedRead ? "Locked Read" :
993 state == MediaState_LockedWrite ? "Locked Write" :
994 "???");
995
996 if (state == MediaState_Inaccessible)
997 {
998 Bstr error;
999 CHECK_ERROR_BREAK (images [i],
1000 COMGETTER(LastAccessError)(error.asOutParam()));
1001 RTPrintf (" Access Error: %ls\n", error.raw());
1002 }
1003
1004 /* get usage */
1005
1006 RTPrintf (" Used by VMs:\n");
1007
1008 com::SafeGUIDArray ids;
1009 CHECK_ERROR_BREAK (images [i],
1010 COMGETTER(MachineIds) (ComSafeArrayAsOutParam (ids)));
1011 if (ids.size() == 0)
1012 {
1013 RTPrintf (" <not used>\n");
1014 }
1015 else
1016 {
1017 for (size_t j = 0; j < ids.size(); ++ j)
1018 {
1019 RTPrintf (" {%RTuuid}\n", &ids [j]);
1020 }
1021 }
1022 }
1023 }
1024 }
1025 while (FALSE);
1026 RTPrintf ("\n");
1027#endif
1028
1029#if 0
1030 // open a (direct) session
1031 ///////////////////////////////////////////////////////////////////////////
1032 do
1033 {
1034 ComPtr<IMachine> machine;
1035 Bstr name = argc > 1 ? argv [1] : "dos";
1036 RTPrintf ("Getting a machine object named '%ls'...\n", name.raw());
1037 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
1038 Guid guid;
1039 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
1040 RTPrintf ("Opening a session for this machine...\n");
1041 CHECK_RC_BREAK (virtualBox->OpenSession (session, guid));
1042#if 1
1043 ComPtr<IMachine> sessionMachine;
1044 RTPrintf ("Getting sessioned machine object...\n");
1045 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
1046 RTPrintf ("Accessing the machine within the session:\n");
1047 readAndChangeMachineSettings (sessionMachine, machine);
1048#if 0
1049 RTPrintf ("\n");
1050 RTPrintf ("Enabling the VRDP server (must succeed even if the VM is saved):\n");
1051 ComPtr<IVRDPServer> vrdp;
1052 CHECK_ERROR_BREAK (sessionMachine, COMGETTER(VRDPServer) (vrdp.asOutParam()));
1053 if (FAILED (vrdp->COMSETTER(Enabled) (TRUE)))
1054 {
1055 PRINT_ERROR_INFO (com::ErrorInfo (vrdp));
1056 }
1057 else
1058 {
1059 BOOL enabled = FALSE;
1060 CHECK_ERROR_BREAK (vrdp, COMGETTER(Enabled) (&enabled));
1061 RTPrintf ("VRDP server is %s\n", enabled ? "enabled" : "disabled");
1062 }
1063#endif
1064#endif
1065#if 0
1066 ComPtr<IConsole> console;
1067 RTPrintf ("Getting the console object...\n");
1068 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
1069 RTPrintf ("Discarding the current machine state...\n");
1070 ComPtr<IProgress> progress;
1071 CHECK_ERROR_BREAK (console, DiscardCurrentState (progress.asOutParam()));
1072 RTPrintf ("Waiting for completion...\n");
1073 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
1074 ProgressErrorInfo ei (progress);
1075 if (FAILED (ei.getResultCode()))
1076 {
1077 PRINT_ERROR_INFO (ei);
1078
1079 ComPtr<IUnknown> initiator;
1080 CHECK_ERROR_BREAK (progress, COMGETTER(Initiator) (initiator.asOutParam()));
1081
1082 RTPrintf ("initiator(unk) = %p\n", (IUnknown *) initiator);
1083 RTPrintf ("console(unk) = %p\n", (IUnknown *) ComPtr<IUnknown> ((IConsole *) console));
1084 RTPrintf ("console = %p\n", (IConsole *) console);
1085 }
1086#endif
1087 RTPrintf("Press enter to close session...");
1088 getchar();
1089 session->Close();
1090 }
1091 while (FALSE);
1092 RTPrintf ("\n");
1093#endif
1094
1095#if 0
1096 // open a remote session
1097 ///////////////////////////////////////////////////////////////////////////
1098 do
1099 {
1100 ComPtr<IMachine> machine;
1101 Bstr name = L"dos";
1102 RTPrintf ("Getting a machine object named '%ls'...\n", name.raw());
1103 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
1104 Guid guid;
1105 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
1106 RTPrintf ("Opening a remote session for this machine...\n");
1107 ComPtr<IProgress> progress;
1108 CHECK_RC_BREAK (virtualBox->OpenRemoteSession (session, guid, Bstr("gui"),
1109 NULL, progress.asOutParam()));
1110 RTPrintf ("Waiting for the session to open...\n");
1111 CHECK_RC_BREAK (progress->WaitForCompletion (-1));
1112 ComPtr<IMachine> sessionMachine;
1113 RTPrintf ("Getting sessioned machine object...\n");
1114 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
1115 ComPtr<IConsole> console;
1116 RTPrintf ("Getting console object...\n");
1117 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
1118 RTPrintf ("Press enter to pause the VM execution in the remote session...");
1119 getchar();
1120 CHECK_RC (console->Pause());
1121 RTPrintf ("Press enter to close this session...");
1122 getchar();
1123 session->Close();
1124 }
1125 while (FALSE);
1126 RTPrintf ("\n");
1127#endif
1128
1129#if 0
1130 // open an existing remote session
1131 ///////////////////////////////////////////////////////////////////////////
1132 do
1133 {
1134 ComPtr<IMachine> machine;
1135 Bstr name = "dos";
1136 RTPrintf ("Getting a machine object named '%ls'...\n", name.raw());
1137 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
1138 Guid guid;
1139 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
1140 RTPrintf ("Opening an existing remote session for this machine...\n");
1141 CHECK_RC_BREAK (virtualBox->OpenExistingSession (session, guid));
1142 ComPtr<IMachine> sessionMachine;
1143 RTPrintf ("Getting sessioned machine object...\n");
1144 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
1145
1146#if 0
1147 Bstr extraDataKey = "VBoxSDL/SecureLabel";
1148 Bstr extraData = "Das kommt jetzt noch viel krasser vom total konkreten API!";
1149 CHECK_RC (sessionMachine->SetExtraData (extraDataKey, extraData));
1150#endif
1151#if 0
1152 ComPtr<IConsole> console;
1153 RTPrintf ("Getting console object...\n");
1154 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
1155 RTPrintf ("Press enter to pause the VM execution in the remote session...");
1156 getchar();
1157 CHECK_RC (console->Pause());
1158 RTPrintf ("Press enter to close this session...");
1159 getchar();
1160#endif
1161 session->Close();
1162 }
1163 while (FALSE);
1164 RTPrintf ("\n");
1165#endif
1166
1167#if 0
1168 do {
1169 // Get host
1170 ComPtr<IHost> host;
1171 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host) (host.asOutParam()));
1172
1173 ULONG uMemSize, uMemAvail;
1174 CHECK_ERROR_BREAK (host, COMGETTER(MemorySize) (&uMemSize));
1175 RTPrintf("Total memory (MB): %u\n", uMemSize);
1176 CHECK_ERROR_BREAK (host, COMGETTER(MemoryAvailable) (&uMemAvail));
1177 RTPrintf("Free memory (MB): %u\n", uMemAvail);
1178 } while (0);
1179#endif
1180
1181#if 0
1182 do {
1183 // Get host
1184 ComPtr<IHost> host;
1185 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host) (host.asOutParam()));
1186
1187 com::SafeIfaceArray<IHostNetworkInterface> hostNetworkInterfaces;
1188 CHECK_ERROR_BREAK(host,
1189 COMGETTER(NetworkInterfaces) (ComSafeArrayAsOutParam (hostNetworkInterfaces)));
1190 if (hostNetworkInterfaces.size() > 0)
1191 {
1192 ComPtr<IHostNetworkInterface> networkInterface = hostNetworkInterfaces[0];
1193 Bstr interfaceName;
1194 networkInterface->COMGETTER(Name)(interfaceName.asOutParam());
1195 RTPrintf("Found %d network interfaces, testing with %lS...\n", hostNetworkInterfaces.size(), interfaceName.raw());
1196 Guid interfaceGuid;
1197 networkInterface->COMGETTER(Id)(interfaceGuid.asOutParam());
1198 // Find the interface by its name
1199 networkInterface.setNull();
1200 CHECK_ERROR_BREAK(host,
1201 FindHostNetworkInterfaceByName (interfaceName, networkInterface.asOutParam()));
1202 Guid interfaceGuid2;
1203 networkInterface->COMGETTER(Id)(interfaceGuid2.asOutParam());
1204 if (interfaceGuid2 != interfaceGuid)
1205 RTPrintf("Failed to retrieve an interface by name %lS.\n", interfaceName.raw());
1206 // Find the interface by its guid
1207 networkInterface.setNull();
1208 CHECK_ERROR_BREAK(host,
1209 FindHostNetworkInterfaceById (interfaceGuid, networkInterface.asOutParam()));
1210 Bstr interfaceName2;
1211 networkInterface->COMGETTER(Name)(interfaceName2.asOutParam());
1212 if (interfaceName != interfaceName2)
1213 RTPrintf("Failed to retrieve an interface by GUID %lS.\n", Bstr(interfaceGuid.toString()).raw());
1214 }
1215 else
1216 {
1217 RTPrintf("No network interfaces found!\n");
1218 }
1219 } while (0);
1220#endif
1221
1222#if 0 && defined (VBOX_WITH_RESOURCE_USAGE_API)
1223 do {
1224 // Get collector
1225 ComPtr<IPerformanceCollector> collector;
1226 CHECK_ERROR_BREAK (virtualBox,
1227 COMGETTER(PerformanceCollector) (collector.asOutParam()));
1228
1229
1230 // Fill base metrics array
1231 Bstr baseMetricNames[] = { L"CPU/Load,RAM/Usage" };
1232 com::SafeArray<BSTR> baseMetrics (1);
1233 baseMetricNames[0].cloneTo(&baseMetrics [0]);
1234
1235 // Get host
1236 ComPtr<IHost> host;
1237 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host) (host.asOutParam()));
1238
1239 // Get machine
1240 ComPtr<IMachine> machine;
1241 Bstr name = argc > 1 ? argv [1] : "dsl";
1242 Bstr sessionType = argc > 2 ? argv [2] : "vrdp";
1243 RTPrintf ("Getting a machine object named '%ls'...\n", name.raw());
1244 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
1245
1246 // Open session
1247 Guid guid;
1248 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
1249 RTPrintf ("Opening a remote session for this machine...\n");
1250 ComPtr<IProgress> progress;
1251 CHECK_RC_BREAK (virtualBox->OpenRemoteSession (session, guid, sessionType,
1252 NULL, progress.asOutParam()));
1253 RTPrintf ("Waiting for the session to open...\n");
1254 CHECK_RC_BREAK (progress->WaitForCompletion (-1));
1255 ComPtr<IMachine> sessionMachine;
1256 RTPrintf ("Getting sessioned machine object...\n");
1257 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
1258
1259 // Setup base metrics
1260 // Note that one needs to set up metrics after a session is open for a machine.
1261 com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
1262 com::SafeIfaceArray<IUnknown> objects(2);
1263 host.queryInterfaceTo(&objects[0]);
1264 machine.queryInterfaceTo(&objects[1]);
1265 CHECK_ERROR_BREAK (collector, SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
1266 ComSafeArrayAsInParam(objects), 1u, 10u,
1267 ComSafeArrayAsOutParam(affectedMetrics)) );
1268 listAffectedMetrics(virtualBox,
1269 ComSafeArrayAsInParam(affectedMetrics));
1270 affectedMetrics.setNull();
1271
1272 // Get console
1273 ComPtr<IConsole> console;
1274 RTPrintf ("Getting console object...\n");
1275 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
1276
1277 RTThreadSleep(5000); // Sleep for 5 seconds
1278
1279 RTPrintf("\nMetrics collected with VM running: --------------------\n");
1280 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1281
1282 // Pause
1283 //RTPrintf ("Press enter to pause the VM execution in the remote session...");
1284 //getchar();
1285 CHECK_RC (console->Pause());
1286
1287 RTThreadSleep(5000); // Sleep for 5 seconds
1288
1289 RTPrintf("\nMetrics collected with VM paused: ---------------------\n");
1290 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1291
1292 RTPrintf("\nDrop collected metrics: ----------------------------------------\n");
1293 CHECK_ERROR_BREAK (collector,
1294 SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
1295 ComSafeArrayAsInParam(objects),
1296 1u, 5u, ComSafeArrayAsOutParam(affectedMetrics)) );
1297 listAffectedMetrics(virtualBox,
1298 ComSafeArrayAsInParam(affectedMetrics));
1299 affectedMetrics.setNull();
1300 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1301
1302 com::SafeIfaceArray<IUnknown> vmObject(1);
1303 machine.queryInterfaceTo(&vmObject[0]);
1304
1305 RTPrintf("\nDisable collection of VM metrics: ------------------------------\n");
1306 CHECK_ERROR_BREAK (collector,
1307 DisableMetrics(ComSafeArrayAsInParam(baseMetrics),
1308 ComSafeArrayAsInParam(vmObject),
1309 ComSafeArrayAsOutParam(affectedMetrics)) );
1310 listAffectedMetrics(virtualBox,
1311 ComSafeArrayAsInParam(affectedMetrics));
1312 affectedMetrics.setNull();
1313 RTThreadSleep(5000); // Sleep for 5 seconds
1314 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1315
1316 RTPrintf("\nRe-enable collection of all metrics: ---------------------------\n");
1317 CHECK_ERROR_BREAK (collector,
1318 EnableMetrics(ComSafeArrayAsInParam(baseMetrics),
1319 ComSafeArrayAsInParam(objects),
1320 ComSafeArrayAsOutParam(affectedMetrics)) );
1321 listAffectedMetrics(virtualBox,
1322 ComSafeArrayAsInParam(affectedMetrics));
1323 affectedMetrics.setNull();
1324 RTThreadSleep(5000); // Sleep for 5 seconds
1325 queryMetrics(virtualBox, collector, ComSafeArrayAsInParam(objects));
1326
1327 // Power off
1328 RTPrintf ("Press enter to power off VM...");
1329 getchar();
1330 CHECK_RC (console->PowerDown());
1331 RTPrintf ("Press enter to close this session...");
1332 getchar();
1333 session->Close();
1334 } while (false);
1335#endif /* VBOX_WITH_RESOURCE_USAGE_API */
1336#if 0
1337 // check of OVF appliance handling
1338 ///////////////////////////////////////////////////////////////////////////
1339 do
1340 {
1341 Bstr ovf = argc > 1 ? argv [1] : "someOVF.ovf";
1342 RTPrintf ("Try to open %ls ...\n", ovf.raw());
1343
1344 ComPtr<IAppliance> appliance;
1345 CHECK_ERROR_BREAK (virtualBox,
1346 CreateAppliance (appliance.asOutParam()));
1347 CHECK_ERROR_BREAK (appliance,
1348 Read (ovf));
1349 Bstr path;
1350 CHECK_ERROR_BREAK (appliance, COMGETTER (Path)(path.asOutParam()));
1351 RTPrintf ("Successfully opened %ls.\n", path.raw());
1352 CHECK_ERROR_BREAK (appliance,
1353 Interpret());
1354 RTPrintf ("Successfully interpreted %ls.\n", path.raw());
1355 RTPrintf ("Appliance:\n");
1356 // Fetch all disks
1357 com::SafeArray<BSTR> retDisks;
1358 CHECK_ERROR_BREAK (appliance,
1359 COMGETTER (Disks)(ComSafeArrayAsOutParam (retDisks)));
1360 if (retDisks.size() > 0)
1361 {
1362 RTPrintf ("Disks:");
1363 for (unsigned i = 0; i < retDisks.size(); i++)
1364 RTPrintf (" %ls", Bstr (retDisks [i]).raw());
1365 RTPrintf ("\n");
1366 }
1367 /* Fetch all virtual system descriptions */
1368 com::SafeIfaceArray<IVirtualSystemDescription> retVSD;
1369 CHECK_ERROR_BREAK (appliance,
1370 COMGETTER (VirtualSystemDescriptions) (ComSafeArrayAsOutParam (retVSD)));
1371 if (retVSD.size() > 0)
1372 {
1373 for (unsigned i = 0; i < retVSD.size(); ++i)
1374 {
1375 com::SafeArray<VirtualSystemDescriptionType_T> retTypes;
1376 com::SafeArray<BSTR> retRefValues;
1377 com::SafeArray<BSTR> retOrigValues;
1378 com::SafeArray<BSTR> retAutoValues;
1379 com::SafeArray<BSTR> retConfiguration;
1380 CHECK_ERROR_BREAK (retVSD [i],
1381 GetDescription (ComSafeArrayAsOutParam (retTypes),
1382 ComSafeArrayAsOutParam (retRefValues),
1383 ComSafeArrayAsOutParam (retOrigValues),
1384 ComSafeArrayAsOutParam (retAutoValues),
1385 ComSafeArrayAsOutParam (retConfiguration)));
1386
1387 RTPrintf ("VirtualSystemDescription:\n");
1388 for (unsigned a = 0; a < retTypes.size(); ++a)
1389 {
1390 RTPrintf (" %d %ls %ls %ls\n",
1391 retTypes [a],
1392 Bstr (retOrigValues [a]).raw(),
1393 Bstr (retAutoValues [a]).raw(),
1394 Bstr (retConfiguration [a]).raw());
1395 }
1396 /* Show warnings from interpret */
1397 com::SafeArray<BSTR> retWarnings;
1398 CHECK_ERROR_BREAK (retVSD [i],
1399 GetWarnings(ComSafeArrayAsOutParam (retWarnings)));
1400 if (retWarnings.size() > 0)
1401 {
1402 RTPrintf ("The following warnings occurs on interpret:\n");
1403 for (unsigned r = 0; r < retWarnings.size(); ++r)
1404 RTPrintf ("%ls\n", Bstr (retWarnings [r]).raw());
1405 RTPrintf ("\n");
1406 }
1407 }
1408 RTPrintf ("\n");
1409 }
1410 RTPrintf ("Try to import the appliance ...\n");
1411 ComPtr<IProgress> progress;
1412 CHECK_ERROR_BREAK (appliance,
1413 ImportMachines (progress.asOutParam()));
1414 CHECK_ERROR (progress, WaitForCompletion (-1));
1415 if (SUCCEEDED(rc))
1416 {
1417 /* Check if the import was successfully */
1418 progress->COMGETTER (ResultCode)(&rc);
1419 if (FAILED (rc))
1420 {
1421 com::ProgressErrorInfo info (progress);
1422 if (info.isBasicAvailable())
1423 RTPrintf ("Error: failed to import appliance. Error message: %lS\n", info.getText().raw());
1424 else
1425 RTPrintf ("Error: failed to import appliance. No error message available!\n");
1426 }else
1427 RTPrintf ("Successfully imported the appliance.\n");
1428 }
1429
1430 }
1431 while (FALSE);
1432 RTPrintf ("\n");
1433#endif
1434
1435 RTPrintf ("Press enter to release Session and VirtualBox instances...");
1436 getchar();
1437
1438 // end "all-stuff" scope
1439 ////////////////////////////////////////////////////////////////////////////
1440 }
1441 while (0);
1442
1443 RTPrintf("Press enter to shutdown COM...");
1444 getchar();
1445
1446 com::Shutdown();
1447
1448 RTPrintf ("tstAPI FINISHED.\n");
1449
1450 return rc;
1451}
1452
1453#ifdef VBOX_WITH_RESOURCE_USAGE_API
1454static void queryMetrics (ComPtr<IVirtualBox> aVirtualBox,
1455 ComPtr<IPerformanceCollector> collector,
1456 ComSafeArrayIn (IUnknown *, objects))
1457{
1458 HRESULT rc;
1459
1460 //Bstr metricNames[] = { L"CPU/Load/User:avg,CPU/Load/System:avg,CPU/Load/Idle:avg,RAM/Usage/Total,RAM/Usage/Used:avg" };
1461 Bstr metricNames[] = { L"*" };
1462 com::SafeArray<BSTR> metrics (1);
1463 metricNames[0].cloneTo(&metrics [0]);
1464 com::SafeArray<BSTR> retNames;
1465 com::SafeIfaceArray<IUnknown> retObjects;
1466 com::SafeArray<BSTR> retUnits;
1467 com::SafeArray<ULONG> retScales;
1468 com::SafeArray<ULONG> retSequenceNumbers;
1469 com::SafeArray<ULONG> retIndices;
1470 com::SafeArray<ULONG> retLengths;
1471 com::SafeArray<LONG> retData;
1472 CHECK_ERROR (collector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
1473 ComSafeArrayInArg(objects),
1474 ComSafeArrayAsOutParam(retNames),
1475 ComSafeArrayAsOutParam(retObjects),
1476 ComSafeArrayAsOutParam(retUnits),
1477 ComSafeArrayAsOutParam(retScales),
1478 ComSafeArrayAsOutParam(retSequenceNumbers),
1479 ComSafeArrayAsOutParam(retIndices),
1480 ComSafeArrayAsOutParam(retLengths),
1481 ComSafeArrayAsOutParam(retData)) );
1482 RTPrintf("Object Metric Values\n"
1483 "---------- -------------------- --------------------------------------------\n");
1484 for (unsigned i = 0; i < retNames.size(); i++)
1485 {
1486 Bstr metricUnit(retUnits[i]);
1487 Bstr metricName(retNames[i]);
1488 RTPrintf("%-10ls %-20ls ", getObjectName(aVirtualBox, retObjects[i]).raw(), metricName.raw());
1489 const char *separator = "";
1490 for (unsigned j = 0; j < retLengths[i]; j++)
1491 {
1492 if (retScales[i] == 1)
1493 RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
1494 else
1495 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
1496 (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
1497 separator = ", ";
1498 }
1499 RTPrintf("\n");
1500 }
1501}
1502
1503static Bstr getObjectName(ComPtr<IVirtualBox> aVirtualBox,
1504 ComPtr<IUnknown> aObject)
1505{
1506 HRESULT rc;
1507
1508 ComPtr<IHost> host = aObject;
1509 if (!host.isNull())
1510 return Bstr("host");
1511
1512 ComPtr<IMachine> machine = aObject;
1513 if (!machine.isNull())
1514 {
1515 Bstr name;
1516 CHECK_ERROR(machine, COMGETTER(Name)(name.asOutParam()));
1517 if (SUCCEEDED(rc))
1518 return name;
1519 }
1520 return Bstr("unknown");
1521}
1522
1523static void listAffectedMetrics(ComPtr<IVirtualBox> aVirtualBox,
1524 ComSafeArrayIn(IPerformanceMetric*, aMetrics))
1525{
1526 HRESULT rc;
1527 com::SafeIfaceArray<IPerformanceMetric> metrics(ComSafeArrayInArg(aMetrics));
1528 if (metrics.size())
1529 {
1530 ComPtr<IUnknown> object;
1531 Bstr metricName;
1532 RTPrintf("The following metrics were modified:\n\n"
1533 "Object Metric\n"
1534 "---------- --------------------\n");
1535 for (size_t i = 0; i < metrics.size(); i++)
1536 {
1537 CHECK_ERROR(metrics[i], COMGETTER(Object)(object.asOutParam()));
1538 CHECK_ERROR(metrics[i], COMGETTER(MetricName)(metricName.asOutParam()));
1539 RTPrintf("%-10ls %-20ls\n",
1540 getObjectName(aVirtualBox, object).raw(), metricName.raw());
1541 }
1542 RTPrintf("\n");
1543 }
1544 else
1545 {
1546 RTPrintf("No metrics match the specified filter!\n");
1547 }
1548}
1549
1550#endif /* VBOX_WITH_RESOURCE_USAGE_API */
1551/* 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