VirtualBox

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

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

Perf API: aggregators, data collection, Host RAM metrics, Linux RAM metrics, and more

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.8 KB
Line 
1/** @file
2 *
3 * tstAPI - test program for our COM/XPCOM interface
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24
25#include <VBox/com/com.h>
26#include <VBox/com/string.h>
27#include <VBox/com/array.h>
28#include <VBox/com/Guid.h>
29#include <VBox/com/ErrorInfo.h>
30#include <VBox/com/EventQueue.h>
31
32#include <VBox/com/VirtualBox.h>
33
34using namespace com;
35
36#define LOG_ENABLED
37#define LOG_GROUP LOG_GROUP_MAIN
38#define LOG_INSTANCE NULL
39#include <VBox/log.h>
40
41#include <iprt/runtime.h>
42#include <iprt/stream.h>
43
44#define printf RTPrintf
45
46// funcs
47///////////////////////////////////////////////////////////////////////////////
48
49HRESULT readAndChangeMachineSettings (IMachine *machine, IMachine *readonlyMachine = 0)
50{
51 HRESULT rc = S_OK;
52
53 Bstr name;
54 printf ("Getting machine name...\n");
55 CHECK_RC_RET (machine->COMGETTER(Name) (name.asOutParam()));
56 printf ("Name: {%ls}\n", name.raw());
57
58 printf("Getting machine GUID...\n");
59 Guid guid;
60 CHECK_RC (machine->COMGETTER(Id) (guid.asOutParam()));
61 if (SUCCEEDED (rc) && !guid.isEmpty()) {
62 printf ("Guid::toString(): {%s}\n", (const char *) guid.toString());
63 } else {
64 printf ("WARNING: there's no GUID!");
65 }
66
67 ULONG memorySize;
68 printf ("Getting memory size...\n");
69 CHECK_RC_RET (machine->COMGETTER(MemorySize) (&memorySize));
70 printf ("Memory size: %d\n", memorySize);
71
72 MachineState_T machineState;
73 printf ("Getting machine state...\n");
74 CHECK_RC_RET (machine->COMGETTER(State) (&machineState));
75 printf ("Machine state: %d\n", machineState);
76
77 BOOL modified;
78 printf ("Are any settings modified?...\n");
79 CHECK_RC (machine->COMGETTER(SettingsModified) (&modified));
80 if (SUCCEEDED (rc))
81 printf ("%s\n", modified ? "yes" : "no");
82
83 ULONG memorySizeBig = memorySize * 10;
84 printf("Changing memory size to %d...\n", memorySizeBig);
85 CHECK_RC (machine->COMSETTER(MemorySize) (memorySizeBig));
86
87 if (SUCCEEDED (rc))
88 {
89 printf ("Are any settings modified now?...\n");
90 CHECK_RC_RET (machine->COMGETTER(SettingsModified) (&modified));
91 printf ("%s\n", modified ? "yes" : "no");
92 ASSERT_RET (modified, 0);
93
94 ULONG memorySizeGot;
95 printf ("Getting memory size again...\n");
96 CHECK_RC_RET (machine->COMGETTER(MemorySize) (&memorySizeGot));
97 printf ("Memory size: %d\n", memorySizeGot);
98 ASSERT_RET (memorySizeGot == memorySizeBig, 0);
99
100 if (readonlyMachine)
101 {
102 printf ("Getting memory size of the counterpart readonly machine...\n");
103 ULONG memorySizeRO;
104 readonlyMachine->COMGETTER(MemorySize) (&memorySizeRO);
105 printf ("Memory size: %d\n", memorySizeRO);
106 ASSERT_RET (memorySizeRO != memorySizeGot, 0);
107 }
108
109 printf ("Discarding recent changes...\n");
110 CHECK_RC_RET (machine->DiscardSettings());
111 printf ("Are any settings modified after discarding?...\n");
112 CHECK_RC_RET (machine->COMGETTER(SettingsModified) (&modified));
113 printf ("%s\n", modified ? "yes" : "no");
114 ASSERT_RET (!modified, 0);
115
116 printf ("Getting memory size once more...\n");
117 CHECK_RC_RET (machine->COMGETTER(MemorySize) (&memorySizeGot));
118 printf ("Memory size: %d\n", memorySizeGot);
119 ASSERT_RET (memorySizeGot == memorySize, 0);
120
121 memorySize = memorySize > 128 ? memorySize / 2 : memorySize * 2;
122 printf("Changing memory size to %d...\n", memorySize);
123 CHECK_RC_RET (machine->COMSETTER(MemorySize) (memorySize));
124 }
125
126 Bstr desc;
127 printf ("Getting description...\n");
128 CHECK_ERROR_RET (machine, COMGETTER(Description) (desc.asOutParam()), rc);
129 printf ("Description is: \"%ls\"\n", desc.raw());
130
131 desc = L"This is an exemplary description (changed).";
132 printf ("Setting description to \"%ls\"...\n", desc.raw());
133 CHECK_ERROR_RET (machine, COMSETTER(Description) (desc), rc);
134
135 printf ("Saving machine settings...\n");
136 CHECK_RC (machine->SaveSettings());
137 if (SUCCEEDED (rc))
138 {
139 printf ("Are any settings modified after saving?...\n");
140 CHECK_RC_RET (machine->COMGETTER(SettingsModified) (&modified));
141 printf ("%s\n", modified ? "yes" : "no");
142 ASSERT_RET (!modified, 0);
143
144 if (readonlyMachine) {
145 printf ("Getting memory size of the counterpart readonly machine...\n");
146 ULONG memorySizeRO;
147 readonlyMachine->COMGETTER(MemorySize) (&memorySizeRO);
148 printf ("Memory size: %d\n", memorySizeRO);
149 ASSERT_RET (memorySizeRO == memorySize, 0);
150 }
151 }
152
153 Bstr extraDataKey = L"Blafasel";
154 Bstr extraData;
155 printf ("Getting extra data key {%ls}...\n", extraDataKey.raw());
156 CHECK_RC_RET (machine->GetExtraData (extraDataKey, extraData.asOutParam()));
157 if (!extraData.isEmpty()) {
158 printf ("Extra data value: {%ls}\n", extraData.raw());
159 } else {
160 if (extraData.isNull())
161 printf ("No extra data exists\n");
162 else
163 printf ("Extra data is empty\n");
164 }
165
166 if (extraData.isEmpty())
167 extraData = L"Das ist die Berliner Luft, Luft, Luft...";
168 else
169 extraData.setNull();
170 printf (
171 "Setting extra data key {%ls} to {%ls}...\n",
172 extraDataKey.raw(), extraData.raw()
173 );
174 CHECK_RC (machine->SetExtraData (extraDataKey, extraData));
175
176 if (SUCCEEDED (rc)) {
177 printf ("Getting extra data key {%ls} again...\n", extraDataKey.raw());
178 CHECK_RC_RET (machine->GetExtraData (extraDataKey, extraData.asOutParam()));
179 if (!extraData.isEmpty()) {
180 printf ("Extra data value: {%ls}\n", extraData.raw());
181 } else {
182 if (extraData.isNull())
183 printf ("No extra data exists\n");
184 else
185 printf ("Extra data is empty\n");
186 }
187 }
188
189 return rc;
190}
191
192// main
193///////////////////////////////////////////////////////////////////////////////
194
195int main(int argc, char *argv[])
196{
197 /*
198 * Initialize the VBox runtime without loading
199 * the support driver.
200 */
201 RTR3Init(false);
202
203 HRESULT rc;
204
205 {
206 char homeDir [RTPATH_MAX];
207 GetVBoxUserHomeDirectory (homeDir, sizeof (homeDir));
208 printf ("VirtualBox Home Directory = '%s'\n", homeDir);
209 }
210
211 printf ("Initializing COM...\n");
212
213 CHECK_RC_RET (com::Initialize());
214
215 do
216 {
217 // scopes all the stuff till shutdown
218 ////////////////////////////////////////////////////////////////////////////
219
220 ComPtr <IVirtualBox> virtualBox;
221 ComPtr <ISession> session;
222
223#if 0
224 // Utf8Str test
225 ////////////////////////////////////////////////////////////////////////////
226
227 Utf8Str nullUtf8Str;
228 printf ("nullUtf8Str='%s'\n", nullUtf8Str.raw());
229
230 Utf8Str simpleUtf8Str = "simpleUtf8Str";
231 printf ("simpleUtf8Str='%s'\n", simpleUtf8Str.raw());
232
233 Utf8Str utf8StrFmt = Utf8StrFmt ("[0=%d]%s[1=%d]",
234 0, "utf8StrFmt", 1);
235 printf ("utf8StrFmt='%s'\n", utf8StrFmt.raw());
236
237#endif
238
239 printf ("Creating VirtualBox object...\n");
240 CHECK_RC (virtualBox.createLocalObject (CLSID_VirtualBox));
241 if (FAILED (rc))
242 {
243 CHECK_ERROR_NOCALL();
244 break;
245 }
246
247 printf ("Creating Session object...\n");
248 CHECK_RC (session.createInprocObject (CLSID_Session));
249 if (FAILED (rc))
250 {
251 CHECK_ERROR_NOCALL();
252 break;
253 }
254
255#if 0
256 // IUnknown identity test
257 ////////////////////////////////////////////////////////////////////////////
258 {
259 {
260 ComPtr <IVirtualBox> virtualBox2;
261
262 printf ("Creating one more VirtualBox object...\n");
263 CHECK_RC (virtualBox2.createLocalObject (CLSID_VirtualBox));
264 if (FAILED (rc))
265 {
266 CHECK_ERROR_NOCALL();
267 break;
268 }
269
270 printf ("IVirtualBox(virualBox)=%p IVirtualBox(virualBox2)=%p\n",
271 (IVirtualBox *) virtualBox, (IVirtualBox *) virtualBox2);
272 Assert ((IVirtualBox *) virtualBox == (IVirtualBox *) virtualBox2);
273
274 ComPtr <IUnknown> unk (virtualBox);
275 ComPtr <IUnknown> unk2;
276 unk2 = virtualBox2;
277
278 printf ("IUnknown(virualBox)=%p IUnknown(virualBox2)=%p\n",
279 (IUnknown *) unk, (IUnknown *) unk2);
280 Assert ((IUnknown *) unk == (IUnknown *) unk2);
281
282 ComPtr <IVirtualBox> vb = unk;
283 ComPtr <IVirtualBox> vb2 = unk;
284
285 printf ("IVirtualBox(IUnknown(virualBox))=%p IVirtualBox(IUnknown(virualBox2))=%p\n",
286 (IVirtualBox *) vb, (IVirtualBox *) vb2);
287 Assert ((IVirtualBox *) vb == (IVirtualBox *) vb2);
288 }
289
290 {
291 ComPtr <IHost> host;
292 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host)(host.asOutParam()));
293 printf (" IHost(host)=%p\n", (IHost *) host);
294 ComPtr <IUnknown> unk = host;
295 printf (" IUnknown(host)=%p\n", (IUnknown *) unk);
296 ComPtr <IHost> host_copy = unk;
297 printf (" IHost(host_copy)=%p\n", (IHost *) host_copy);
298 ComPtr <IUnknown> unk_copy = host_copy;
299 printf (" IUnknown(host_copy)=%p\n", (IUnknown *) unk_copy);
300 Assert ((IUnknown *) unk == (IUnknown *) unk_copy);
301
302 /* query IUnknown on IUnknown */
303 ComPtr <IUnknown> unk_copy_copy;
304 unk_copy.queryInterfaceTo (unk_copy_copy.asOutParam());
305 printf (" IUnknown(unk_copy)=%p\n", (IUnknown *) unk_copy_copy);
306 Assert ((IUnknown *) unk_copy == (IUnknown *) unk_copy_copy);
307 /* query IUnknown on IUnknown in the opposite direction */
308 unk_copy_copy.queryInterfaceTo (unk_copy.asOutParam());
309 printf (" IUnknown(unk_copy_copy)=%p\n", (IUnknown *) unk_copy);
310 Assert ((IUnknown *) unk_copy == (IUnknown *) unk_copy_copy);
311
312 /* query IUnknown again after releasing all previous IUnknown instances
313 * but keeping IHost -- it should remain the same (Identity Rule) */
314 IUnknown *oldUnk = unk;
315 unk.setNull();
316 unk_copy.setNull();
317 unk_copy_copy.setNull();
318 unk = host;
319 printf (" IUnknown(host)=%p\n", (IUnknown *) unk);
320 Assert (oldUnk == (IUnknown *) unk);
321 }
322
323// printf ("Will be now released (press Enter)...");
324// getchar();
325 }
326#endif
327
328 // create the event queue
329 // (here it is necessary only to process remaining XPCOM/IPC events
330 // after the session is closed)
331 EventQueue eventQ;
332
333#if 0
334 // the simplest COM API test
335 ////////////////////////////////////////////////////////////////////////////
336 {
337 Bstr version;
338 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Version) (version.asOutParam()));
339 printf ("VirtualBox version = %ls\n", version.raw());
340 }
341#endif
342
343#if 1
344 // Array test
345 ////////////////////////////////////////////////////////////////////////////
346 {
347 printf ("Calling IVirtualBox::Machines...\n");
348
349 com::SafeIfaceArray <IMachine> machines;
350 CHECK_ERROR_BREAK (virtualBox,
351 COMGETTER(Machines2) (ComSafeArrayAsOutParam (machines)));
352
353 printf ("%u machines registered (machines.isNull()=%d).\n",
354 machines.size(), machines.isNull());
355
356 for (size_t i = 0; i < machines.size(); ++ i)
357 {
358 Bstr name;
359 CHECK_ERROR_BREAK (machines [i], COMGETTER(Name) (name.asOutParam()));
360 printf ("machines[%u]='%s'\n", i, Utf8Str (name).raw());
361 }
362 }
363#endif
364
365#if 0
366 // some outdated stuff
367 ////////////////////////////////////////////////////////////////////////////
368
369 printf("Getting IHost interface...\n");
370 IHost *host;
371 rc = virtualBox->GetHost(&host);
372 if (SUCCEEDED(rc))
373 {
374 IHostDVDDriveCollection *dvdColl;
375 rc = host->GetHostDVDDrives(&dvdColl);
376 if (SUCCEEDED(rc))
377 {
378 IHostDVDDrive *dvdDrive = NULL;
379 dvdColl->GetNextHostDVDDrive(dvdDrive, &dvdDrive);
380 while (dvdDrive)
381 {
382 BSTR driveName;
383 char *driveNameUtf8;
384 dvdDrive->GetDriveName(&driveName);
385 RTUtf16ToUtf8((PCRTUTF16)driveName, &driveNameUtf8);
386 printf("Host DVD drive name: %s\n", driveNameUtf8);
387 RTStrFree(driveNameUtf8);
388 SysFreeString(driveName);
389 IHostDVDDrive *dvdDriveTemp = dvdDrive;
390 dvdColl->GetNextHostDVDDrive(dvdDriveTemp, &dvdDrive);
391 dvdDriveTemp->Release();
392 }
393 dvdColl->Release();
394 } else
395 {
396 printf("Could not get host DVD drive collection\n");
397 }
398
399 IHostFloppyDriveCollection *floppyColl;
400 rc = host->GetHostFloppyDrives(&floppyColl);
401 if (SUCCEEDED(rc))
402 {
403 IHostFloppyDrive *floppyDrive = NULL;
404 floppyColl->GetNextHostFloppyDrive(floppyDrive, &floppyDrive);
405 while (floppyDrive)
406 {
407 BSTR driveName;
408 char *driveNameUtf8;
409 floppyDrive->GetDriveName(&driveName);
410 RTUtf16ToUtf8((PCRTUTF16)driveName, &driveNameUtf8);
411 printf("Host floppy drive name: %s\n", driveNameUtf8);
412 RTStrFree(driveNameUtf8);
413 SysFreeString(driveName);
414 IHostFloppyDrive *floppyDriveTemp = floppyDrive;
415 floppyColl->GetNextHostFloppyDrive(floppyDriveTemp, &floppyDrive);
416 floppyDriveTemp->Release();
417 }
418 floppyColl->Release();
419 } else
420 {
421 printf("Could not get host floppy drive collection\n");
422 }
423 host->Release();
424 } else
425 {
426 printf("Call failed\n");
427 }
428 printf ("\n");
429#endif
430
431#if 0
432 // IVirtualBoxErrorInfo test
433 ////////////////////////////////////////////////////////////////////////////
434 {
435 // RPC calls
436
437 // call a method that will definitely fail
438 Guid uuid;
439 ComPtr <IHardDisk> hardDisk;
440 rc = virtualBox->GetHardDisk(uuid, hardDisk.asOutParam());
441 printf ("virtualBox->GetHardDisk(null-uuid)=%08X\n", rc);
442
443// {
444// com::ErrorInfo info (virtualBox);
445// PRINT_ERROR_INFO (info);
446// }
447
448 // call a method that will definitely succeed
449 Bstr version;
450 rc = virtualBox->COMGETTER(Version) (version.asOutParam());
451 printf ("virtualBox->COMGETTER(Version)=%08X\n", rc);
452
453 {
454 com::ErrorInfo info (virtualBox);
455 PRINT_ERROR_INFO (info);
456 }
457
458 // Local calls
459
460 // call a method that will definitely fail
461 ComPtr <IMachine> machine;
462 rc = session->COMGETTER(Machine)(machine.asOutParam());
463 printf ("session->COMGETTER(Machine)=%08X\n", rc);
464
465// {
466// com::ErrorInfo info (virtualBox);
467// PRINT_ERROR_INFO (info);
468// }
469
470 // call a method that will definitely succeed
471 SessionState_T state;
472 rc = session->COMGETTER(State) (&state);
473 printf ("session->COMGETTER(State)=%08X\n", rc);
474
475 {
476 com::ErrorInfo info (virtualBox);
477 PRINT_ERROR_INFO (info);
478 }
479 }
480#endif
481
482#if 0
483 // register the existing hard disk image
484 ///////////////////////////////////////////////////////////////////////////
485 do
486 {
487 ComPtr <IHardDisk> hd;
488 Bstr src = L"E:\\develop\\innotek\\images\\NewHardDisk.vdi";
489 printf ("Opening the existing hard disk '%ls'...\n", src.raw());
490 CHECK_ERROR_BREAK (virtualBox, OpenHardDisk (src, hd.asOutParam()));
491 printf ("Enter to continue...\n");
492 getchar();
493 printf ("Registering the existing hard disk '%ls'...\n", src.raw());
494 CHECK_ERROR_BREAK (virtualBox, RegisterHardDisk (hd));
495 printf ("Enter to continue...\n");
496 getchar();
497 }
498 while (FALSE);
499 printf ("\n");
500#endif
501
502#if 0
503 // find and unregister the existing hard disk image
504 ///////////////////////////////////////////////////////////////////////////
505 do
506 {
507 ComPtr <IVirtualDiskImage> vdi;
508 Bstr src = L"CreatorTest.vdi";
509 printf ("Unregistering the hard disk '%ls'...\n", src.raw());
510 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
511 ComPtr <IHardDisk> hd = vdi;
512 Guid id;
513 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
514 CHECK_ERROR_BREAK (virtualBox, UnregisterHardDisk (id, hd.asOutParam()));
515 }
516 while (FALSE);
517 printf ("\n");
518#endif
519
520#if 0
521 // clone the registered hard disk
522 ///////////////////////////////////////////////////////////////////////////
523 do
524 {
525#if defined RT_OS_LINUX
526 Bstr src = L"/mnt/hugaida/common/develop/innotek/images/freedos-linux.vdi";
527#else
528 Bstr src = L"E:/develop/innotek/images/freedos.vdi";
529#endif
530 Bstr dst = L"./clone.vdi";
531 RTPrintf ("Cloning '%ls' to '%ls'...\n", src.raw(), dst.raw());
532 ComPtr <IVirtualDiskImage> vdi;
533 CHECK_ERROR_BREAK (virtualBox, FindVirtualDiskImage (src, vdi.asOutParam()));
534 ComPtr <IHardDisk> hd = vdi;
535 ComPtr <IProgress> progress;
536 CHECK_ERROR_BREAK (hd, CloneToImage (dst, vdi.asOutParam(), progress.asOutParam()));
537 RTPrintf ("Waiting for completion...\n");
538 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
539 ProgressErrorInfo ei (progress);
540 if (FAILED (ei.getResultCode()))
541 {
542 PRINT_ERROR_INFO (ei);
543 }
544 else
545 {
546 vdi->COMGETTER(FilePath) (dst.asOutParam());
547 RTPrintf ("Actual clone path is '%ls'\n", dst.raw());
548 }
549 }
550 while (FALSE);
551 printf ("\n");
552#endif
553
554#if 0
555 // find a registered hard disk by location
556 ///////////////////////////////////////////////////////////////////////////
557 do
558 {
559 ComPtr <IHardDisk> hd;
560 static const wchar_t *Names[] =
561 {
562#ifndef RT_OS_LINUX
563 L"E:/Develop/innotek/images/thinker/freedos.vdi",
564 L"E:/Develop/innotek/images/thinker/fReeDoS.vDI",
565 L"E:/Develop/innotek/images/vmdk/haiku.vmdk",
566#else
567 L"/mnt/host/common/Develop/innotek/images/maggot/freedos.vdi",
568 L"/mnt/host/common/Develop/innotek/images/maggot/fReeDoS.vDI",
569#endif
570 };
571 for (size_t i = 0; i < ELEMENTS (Names); ++ i)
572 {
573 Bstr src = Names [i];
574 printf ("Searching for hard disk '%ls'...\n", src.raw());
575 rc = virtualBox->FindHardDisk (src, hd.asOutParam());
576 if (SUCCEEDED (rc))
577 {
578 Guid id;
579 Bstr location;
580 CHECK_ERROR_BREAK (hd, COMGETTER(Id) (id.asOutParam()));
581 CHECK_ERROR_BREAK (hd, COMGETTER(Location) (location.asOutParam()));
582 printf ("Found, UUID={%Vuuid}, location='%ls'.\n",
583 id.raw(), location.raw());
584 }
585 else
586 {
587 PRINT_ERROR_INFO (com::ErrorInfo (virtualBox));
588 }
589 }
590 }
591 while (FALSE);
592 printf ("\n");
593#endif
594
595#if 0
596 // access the machine in read-only mode
597 ///////////////////////////////////////////////////////////////////////////
598 do
599 {
600 ComPtr <IMachine> machine;
601 Bstr name = argc > 1 ? argv [1] : "dos";
602 printf ("Getting a machine object named '%ls'...\n", name.raw());
603 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
604 printf ("Accessing the machine in read-only mode:\n");
605 readAndChangeMachineSettings (machine);
606#if 0
607 if (argc != 2)
608 {
609 printf ("Error: a string has to be supplied!\n");
610 }
611 else
612 {
613 Bstr secureLabel = argv[1];
614 machine->COMSETTER(ExtraData)(L"VBoxSDL/SecureLabel", secureLabel);
615 }
616#endif
617 }
618 while (0);
619 printf ("\n");
620#endif
621
622#if 0
623 // create a new machine (w/o registering it)
624 ///////////////////////////////////////////////////////////////////////////
625 do
626 {
627 ComPtr <IMachine> machine;
628#if defined (RT_OS_LINUX)
629 Bstr baseDir = L"/tmp/vbox";
630#else
631 Bstr baseDir = L"C:\\vbox";
632#endif
633 Bstr name = L"machina";
634
635 printf ("Creating a new machine object (base dir '%ls', name '%ls')...\n",
636 baseDir.raw(), name.raw());
637 CHECK_ERROR_BREAK (virtualBox, CreateMachine (baseDir, name,
638 machine.asOutParam()));
639
640 printf ("Getting name...\n");
641 CHECK_ERROR_BREAK (machine, COMGETTER(Name) (name.asOutParam()));
642 printf ("Name: {%ls}\n", name.raw());
643
644 BOOL modified = FALSE;
645 printf ("Are any settings modified?...\n");
646 CHECK_ERROR_BREAK (machine, COMGETTER(SettingsModified) (&modified));
647 printf ("%s\n", modified ? "yes" : "no");
648
649 ASSERT_BREAK (modified == TRUE);
650
651 name = L"Kakaya prekrasnaya virtual'naya mashina!";
652 printf ("Setting new name ({%ls})...\n", name.raw());
653 CHECK_ERROR_BREAK (machine, COMSETTER(Name) (name));
654
655 printf ("Setting memory size to 111...\n");
656 CHECK_ERROR_BREAK (machine, COMSETTER(MemorySize) (111));
657
658 Bstr desc = L"This is an exemplary description.";
659 printf ("Setting description to \"%ls\"...\n", desc.raw());
660 CHECK_ERROR_BREAK (machine, COMSETTER(Description) (desc));
661
662 ComPtr <IGuestOSType> guestOSType;
663 Bstr type = L"os2warp45";
664 CHECK_ERROR_BREAK (virtualBox, GetGuestOSType (type, guestOSType.asOutParam()));
665
666 printf ("Saving new machine settings...\n");
667 CHECK_ERROR_BREAK (machine, SaveSettings());
668
669 printf ("Accessing the newly created machine:\n");
670 readAndChangeMachineSettings (machine);
671 }
672 while (FALSE);
673 printf ("\n");
674#endif
675
676#if 0
677 // enumerate host DVD drives
678 ///////////////////////////////////////////////////////////////////////////
679 do
680 {
681 ComPtr <IHost> host;
682 CHECK_RC_BREAK (virtualBox->COMGETTER(Host) (host.asOutParam()));
683
684 {
685 ComPtr <IHostDVDDriveCollection> coll;
686 CHECK_RC_BREAK (host->COMGETTER(DVDDrives) (coll.asOutParam()));
687 ComPtr <IHostDVDDriveEnumerator> enumerator;
688 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
689 BOOL hasmore;
690 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
691 {
692 ComPtr <IHostDVDDrive> drive;
693 CHECK_RC_BREAK (enumerator->GetNext (drive.asOutParam()));
694 Bstr name;
695 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
696 printf ("Host DVD drive: name={%ls}\n", name.raw());
697 }
698 CHECK_RC_BREAK (rc);
699
700 ComPtr <IHostDVDDrive> drive;
701 CHECK_ERROR (enumerator, GetNext (drive.asOutParam()));
702 CHECK_ERROR (coll, GetItemAt (1000, drive.asOutParam()));
703 CHECK_ERROR (coll, FindByName (Bstr ("R:"), drive.asOutParam()));
704 if (SUCCEEDED (rc))
705 {
706 Bstr name;
707 CHECK_RC_BREAK (drive->COMGETTER(Name) (name.asOutParam()));
708 printf ("Found by name: name={%ls}\n", name.raw());
709 }
710 }
711 }
712 while (FALSE);
713 printf ("\n");
714#endif
715
716#if 0
717 // enumerate hard disks & dvd images
718 ///////////////////////////////////////////////////////////////////////////
719 do
720 {
721 {
722 ComPtr <IHardDiskCollection> coll;
723 CHECK_RC_BREAK (virtualBox->COMGETTER(HardDisks) (coll.asOutParam()));
724 ComPtr <IHardDiskEnumerator> enumerator;
725 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
726 BOOL hasmore;
727 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
728 {
729 ComPtr <IHardDisk> disk;
730 CHECK_RC_BREAK (enumerator->GetNext (disk.asOutParam()));
731 Guid id;
732 CHECK_RC_BREAK (disk->COMGETTER(Id) (id.asOutParam()));
733 Bstr path;
734 CHECK_RC_BREAK (disk->COMGETTER(FilePath) (path.asOutParam()));
735 printf ("Hard Disk: id={%s}, path={%ls}\n",
736 id.toString().raw(), path.raw());
737 Guid mid;
738 CHECK_RC_BREAK (
739 virtualBox->GetHardDiskUsage (id, ResourceUsage_All,
740 mid.asOutParam())
741 );
742 if (mid.isEmpty())
743 printf (" not used\n");
744 else
745 printf (" used by VM: {%s}\n", mid.toString().raw());
746 }
747 CHECK_RC_BREAK (rc);
748 }
749
750 {
751 ComPtr <IDVDImageCollection> coll;
752 CHECK_RC_BREAK (virtualBox->COMGETTER(DVDImages) (coll.asOutParam()));
753 ComPtr <IDVDImageEnumerator> enumerator;
754 CHECK_RC_BREAK (coll->Enumerate (enumerator.asOutParam()));
755 BOOL hasmore;
756 while (SUCCEEDED (enumerator->HasMore (&hasmore)) && hasmore)
757 {
758 ComPtr <IDVDImage> image;
759 CHECK_RC_BREAK (enumerator->GetNext (image.asOutParam()));
760 Guid id;
761 CHECK_RC_BREAK (image->COMGETTER(Id) (id.asOutParam()));
762 Bstr path;
763 CHECK_RC_BREAK (image->COMGETTER(FilePath) (path.asOutParam()));
764 printf ("CD/DVD Image: id={%s}, path={%ls}\n",
765 id.toString().raw(), path.raw());
766 Bstr mIDs;
767 CHECK_RC_BREAK (
768 virtualBox->GetDVDImageUsage (id, ResourceUsage_All,
769 mIDs.asOutParam())
770 );
771 if (mIDs.isNull())
772 printf (" not used\n");
773 else
774 printf (" used by VMs: {%ls}\n", mIDs.raw());
775 }
776 CHECK_RC_BREAK (rc);
777 }
778 }
779 while (FALSE);
780 printf ("\n");
781#endif
782
783#if 0
784 // open a (direct) session
785 ///////////////////////////////////////////////////////////////////////////
786 do
787 {
788 ComPtr <IMachine> machine;
789 Bstr name = argc > 1 ? argv [1] : "dos";
790 printf ("Getting a machine object named '%ls'...\n", name.raw());
791 CHECK_ERROR_BREAK (virtualBox, FindMachine (name, machine.asOutParam()));
792 Guid guid;
793 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
794 printf ("Opening a session for this machine...\n");
795 CHECK_RC_BREAK (virtualBox->OpenSession (session, guid));
796#if 1
797 ComPtr <IMachine> sessionMachine;
798 printf ("Getting sessioned machine object...\n");
799 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
800 printf ("Accessing the machine within the session:\n");
801 readAndChangeMachineSettings (sessionMachine, machine);
802#if 0
803 printf ("\n");
804 printf ("Enabling the VRDP server (must succeed even if the VM is saved):\n");
805 ComPtr <IVRDPServer> vrdp;
806 CHECK_ERROR_BREAK (sessionMachine, COMGETTER(VRDPServer) (vrdp.asOutParam()));
807 if (FAILED (vrdp->COMSETTER(Enabled) (TRUE)))
808 {
809 PRINT_ERROR_INFO (com::ErrorInfo (vrdp));
810 }
811 else
812 {
813 BOOL enabled = FALSE;
814 CHECK_ERROR_BREAK (vrdp, COMGETTER(Enabled) (&enabled));
815 printf ("VRDP server is %s\n", enabled ? "enabled" : "disabled");
816 }
817#endif
818#endif
819#if 0
820 ComPtr <IConsole> console;
821 printf ("Getting the console object...\n");
822 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
823 printf ("Discarding the current machine state...\n");
824 ComPtr <IProgress> progress;
825 CHECK_ERROR_BREAK (console, DiscardCurrentState (progress.asOutParam()));
826 printf ("Waiting for completion...\n");
827 CHECK_ERROR_BREAK (progress, WaitForCompletion (-1));
828 ProgressErrorInfo ei (progress);
829 if (FAILED (ei.getResultCode()))
830 {
831 PRINT_ERROR_INFO (ei);
832
833 ComPtr <IUnknown> initiator;
834 CHECK_ERROR_BREAK (progress, COMGETTER(Initiator) (initiator.asOutParam()));
835
836 printf ("initiator(unk) = %p\n", (IUnknown *) initiator);
837 printf ("console(unk) = %p\n", (IUnknown *) ComPtr <IUnknown> ((IConsole *) console));
838 printf ("console = %p\n", (IConsole *) console);
839 }
840#endif
841 printf("Press enter to close session...");
842 getchar();
843 session->Close();
844 }
845 while (FALSE);
846 printf ("\n");
847#endif
848
849#if 0
850 // open a remote session
851 ///////////////////////////////////////////////////////////////////////////
852 do
853 {
854 ComPtr <IMachine> machine;
855 Bstr name = L"dos";
856 printf ("Getting a machine object named '%ls'...\n", name.raw());
857 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
858 Guid guid;
859 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
860 printf ("Opening a remote session for this machine...\n");
861 ComPtr <IProgress> progress;
862 CHECK_RC_BREAK (virtualBox->OpenRemoteSession (session, guid, Bstr("gui"),
863 NULL, progress.asOutParam()));
864 printf ("Waiting for the session to open...\n");
865 CHECK_RC_BREAK (progress->WaitForCompletion (-1));
866 ComPtr <IMachine> sessionMachine;
867 printf ("Getting sessioned machine object...\n");
868 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
869 ComPtr <IConsole> console;
870 printf ("Getting console object...\n");
871 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
872 printf ("Press enter to pause the VM execution in the remote session...");
873 getchar();
874 CHECK_RC (console->Pause());
875 printf ("Press enter to close this session...");
876 getchar();
877 session->Close();
878 }
879 while (FALSE);
880 printf ("\n");
881#endif
882
883#if 0
884 // open an existing remote session
885 ///////////////////////////////////////////////////////////////////////////
886 do
887 {
888 ComPtr <IMachine> machine;
889 Bstr name = "dos";
890 printf ("Getting a machine object named '%ls'...\n", name.raw());
891 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
892 Guid guid;
893 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
894 printf ("Opening an existing remote session for this machine...\n");
895 CHECK_RC_BREAK (virtualBox->OpenExistingSession (session, guid));
896 ComPtr <IMachine> sessionMachine;
897 printf ("Getting sessioned machine object...\n");
898 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
899
900#if 0
901 Bstr extraDataKey = "VBoxSDL/SecureLabel";
902 Bstr extraData = "Das kommt jetzt noch viel krasser vom total konkreten API!";
903 CHECK_RC (sessionMachine->SetExtraData (extraDataKey, extraData));
904#endif
905#if 0
906 ComPtr <IConsole> console;
907 printf ("Getting console object...\n");
908 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
909 printf ("Press enter to pause the VM execution in the remote session...");
910 getchar();
911 CHECK_RC (console->Pause());
912 printf ("Press enter to close this session...");
913 getchar();
914#endif
915 session->Close();
916 }
917 while (FALSE);
918 printf ("\n");
919#endif
920
921#if 1
922 do {
923 Bstr baseMetricNames[] = { L"CPU/Load,RAM/Usage" };
924 com::SafeArray<BSTR> baseMetrics (1);
925 baseMetricNames[0].cloneTo (&baseMetrics [0]);
926
927 ComPtr <IHost> host;
928 CHECK_ERROR_BREAK (virtualBox, COMGETTER(Host) (host.asOutParam()));
929 ComPtr <IPerformanceCollector> collector;
930 CHECK_ERROR_BREAK (virtualBox,
931 COMGETTER(PerformanceCollector) (collector.asOutParam()));
932
933 com::SafeIfaceArray<IUnknown> objects(1);
934 host.queryInterfaceTo(&objects[0]);
935 CHECK_ERROR_BREAK (collector, SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
936 ComSafeArrayAsInParam(objects), 1u, 10u) );
937 RTThreadSleep(3000); /* Sleep 10 seconds. */
938
939 Bstr metricNames[] = { L"CPU/Load/User:avg,CPU/Load/System:avg,CPU/Load/Idle:avg,RAM/Usage/Total.RAM/Usage/Used:avg" };
940 com::SafeArray<BSTR> metrics (1);
941 metricNames[0].cloneTo (&metrics [0]);
942 com::SafeArray<BSTR> retNames;
943 com::SafeIfaceArray<IUnknown> retObjects;
944 com::SafeArray<ULONG> retIndices;
945 com::SafeArray<ULONG> retLengths;
946 com::SafeArray<LONG> retData;
947 CHECK_ERROR_BREAK (collector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
948 ComSafeArrayAsInParam(objects),
949 ComSafeArrayAsOutParam(retNames),
950 ComSafeArrayAsOutParam(retObjects),
951 ComSafeArrayAsOutParam(retIndices),
952 ComSafeArrayAsOutParam(retLengths),
953 ComSafeArrayAsOutParam(retData)) );
954 for (unsigned i = 0; i < retNames.size(); i++)
955 {
956 Bstr metricName(retNames[i]);
957 printf("%ls", metricName.raw());
958 for (unsigned j = 0; j < retLengths[i]; j++)
959 {
960 printf(" %d", retData[retIndices[i] + j]);
961 }
962 printf("\n");
963 }
964 } while (0);
965#endif
966#if 0
967 for (int i = 0; i < 10; i++)
968 {
969 ULONG user, system, idle;
970 host->GetProcessorUsage(&user, &system, &idle);
971 printf("user=%u system=%u idle=%u\n", user/10000000, system/10000000, idle/10000000);
972 }
973#endif
974
975#if 0
976 {
977 ComPtr <IMachine> machine;
978 Bstr name = argc > 1 ? argv [1] : "dsl";
979 printf ("Getting a machine object named '%ls'...\n", name.raw());
980 CHECK_RC_BREAK (virtualBox->FindMachine (name, machine.asOutParam()));
981 Guid guid;
982 CHECK_RC_BREAK (machine->COMGETTER(Id) (guid.asOutParam()));
983 printf ("Opening a remote session for this machine...\n");
984 ComPtr <IProgress> progress;
985 CHECK_RC_BREAK (virtualBox->OpenRemoteSession (session, guid, Bstr("gui"),
986 NULL, progress.asOutParam()));
987 printf ("Waiting for the session to open...\n");
988 CHECK_RC_BREAK (progress->WaitForCompletion (-1));
989 ComPtr <IMachine> sessionMachine;
990 printf ("Getting sessioned machine object...\n");
991 CHECK_RC_BREAK (session->COMGETTER(Machine) (sessionMachine.asOutParam()));
992 ComPtr <IConsole> console;
993 printf ("Getting console object...\n");
994 CHECK_RC_BREAK (session->COMGETTER(Console) (console.asOutParam()));
995 for (int i = 0; i < 10; i++)
996 {
997 ComPtr <IHost> host;
998 CHECK_RC_BREAK (virtualBox->COMGETTER(Host) (host.asOutParam()));
999 ULONG user, system;
1000 sessionMachine->GetProcessorUsage(&user, &system);
1001 printf("VM: user=%u system=%u\n", user/10000000, system/10000000);
1002 RTThreadSleep(1000);
1003 }
1004 printf ("Press enter to pause the VM execution in the remote session...");
1005 getchar();
1006 CHECK_RC (console->Pause());
1007 for (int i = 0; i < 10; i++)
1008 {
1009 ComPtr <IHost> host;
1010 CHECK_RC_BREAK (virtualBox->COMGETTER(Host) (host.asOutParam()));
1011 ULONG user, system;
1012 sessionMachine->GetProcessorUsage(&user, &system);
1013 printf("VM: user=%u system=%u\n", user/10000000, system/10000000);
1014 RTThreadSleep(1000);
1015 }
1016 printf ("Press enter to power off VM...");
1017 getchar();
1018 CHECK_RC (console->PowerDown());
1019 printf ("Press enter to close this session...");
1020 getchar();
1021 session->Close();
1022 }
1023#endif
1024
1025 printf ("Press enter to release Session and VirtualBox instances...");
1026 getchar();
1027
1028 // end "all-stuff" scope
1029 ////////////////////////////////////////////////////////////////////////////
1030 }
1031 while (0);
1032
1033 printf("Press enter to shutdown COM...");
1034 getchar();
1035
1036 com::Shutdown();
1037
1038 printf ("tstAPI FINISHED.\n");
1039
1040 return rc;
1041}
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