VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstVBoxAPIWin.cpp@ 2459

Last change on this file since 2459 was 2459, checked in by vboxsync, 17 years ago

added IErrorInfo testcase

File size: 4.5 KB
Line 
1/** @file
2 *
3 * tstVBoxAPIWin - sample program to illustrate the VirtualBox
4 * COM API for machine management on Windows.
5 It only uses standard C/C++ and COM semantics,
6 * no additional VBox classes/macros/helpers. To
7 * make things even easier to follow, only the
8 * standard Win32 API has been used. Typically,
9 * C++ developers would make use of Microsoft's
10 * ATL to ease development.
11 */
12
13/*
14 * Copyright (C) 2006 InnoTek Systemberatung GmbH
15 *
16 * This file is part of VirtualBox Open Source Edition (OSE), as
17 * available from http://www.virtualbox.org. This file is free software;
18 * you can redistribute it and/or modify it under the terms of the GNU
19 * General Public License as published by the Free Software Foundation,
20 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
21 * distribution. VirtualBox OSE is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY of any kind.
23 *
24 * If you received this file as part of a commercial VirtualBox
25 * distribution, then only the terms of your commercial VirtualBox
26 * license agreement apply instead of the previous paragraph.
27 */
28
29#include <stdio.h>
30#include "VirtualBox.h"
31
32
33int listVMs(IVirtualBox *virtualBox)
34{
35 HRESULT rc;
36
37 /*
38 * First we have to get a list of all registered VMs
39 */
40 IMachineCollection *collection = NULL;
41 IMachineEnumerator *enumerator = NULL;
42
43 do
44 {
45 rc = virtualBox->get_Machines(&collection);
46 if (SUCCEEDED(rc))
47 rc = collection->Enumerate(&enumerator);
48
49 if (SUCCEEDED(rc))
50 {
51 BOOL hasMore;
52 while (enumerator->HasMore(&hasMore), hasMore)
53 {
54 /*
55 * Get the machine object
56 */
57 IMachine *machine = NULL;
58 rc = enumerator->GetNext(&machine);
59 if (SUCCEEDED(rc))
60 {
61 BSTR str;
62
63 machine->get_Name(&str);
64 printf("Name: %S\n", str);
65
66 SysFreeString(str);
67
68 machine->Release();
69 }
70 }
71 }
72 } while (0);
73
74 if (enumerator)
75 enumerator->Release();
76 if (collection)
77 collection->Release();
78
79 return 0;
80}
81
82int testErrorInfo(IVirtualBox *virtualBox)
83{
84 HRESULT rc;
85
86 /* try to find a machine that doesn't exist */
87 IMachine *machine = NULL;
88 BSTR machineName = SysAllocString(L"Foobar");
89
90 rc = virtualBox->FindMachine(machineName, &machine);
91
92 if (FAILED(rc))
93 {
94 IErrorInfo *errorInfo;
95
96 rc = GetErrorInfo(0, &errorInfo);
97
98 if (FAILED(rc))
99 printf("Error getting error info! rc = 0x%x\n", rc);
100 else
101 {
102 BSTR errorDescription = NULL;
103
104 rc = errorInfo->GetDescription(&errorDescription);
105
106 if (FAILED(rc) || !errorDescription)
107 printf("Error getting error description! rc = 0x%x\n", rc);
108 else
109 {
110 printf("Successfully retrieved error description: %S\n", errorDescription);
111
112 SysFreeString(errorDescription);
113 }
114
115 errorInfo->Release();
116 }
117 }
118
119 if (machine)
120 machine->Release();
121
122 SysFreeString(machineName);
123
124 return 0;
125}
126
127
128int main(int argc, char *argv[])
129{
130 HRESULT rc;
131 IVirtualBox *virtualBox;
132
133 do
134 {
135 /* initialize the COM subsystem */
136 CoInitialize(NULL);
137
138 /* instantiate the VirtualBox root object */
139 rc = CoCreateInstance(CLSID_VirtualBox, /* the VirtualBox base object */
140 NULL, /* no aggregation */
141 CLSCTX_LOCAL_SERVER, /* the object lives in a server process on this machine */
142 IID_IVirtualBox, /* IID of the interface */
143 (void**)&virtualBox);
144
145 if (!SUCCEEDED(rc))
146 {
147 printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
148 break;
149 }
150
151 listVMs(virtualBox);
152
153 testErrorInfo(virtualBox);
154
155 /* release the VirtualBox object */
156 virtualBox->Release();
157
158 } while (0);
159
160 CoUninitialize();
161 return 0;
162}
163
164
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