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-2007 innotek 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 (GPL) as published by the Free Software
|
---|
20 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
21 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
22 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
23 | */
|
---|
24 |
|
---|
25 | /*
|
---|
26 | * PURPOSE OF THIS SAMPLE PROGRAM
|
---|
27 | * ------------------------------
|
---|
28 | *
|
---|
29 | * This sample program is intended to demonstrate the minimal code necessary
|
---|
30 | * to use VirtualBox COM API for learning puroses only. The program uses pure
|
---|
31 | * Win32 API and doesn't have any extra dependencies to let you better
|
---|
32 | * understand what is going on when a client talks to the VirtualBox core
|
---|
33 | * using the COM framework.
|
---|
34 | *
|
---|
35 | * However, if you want to write a real application, it is highly recommended
|
---|
36 | * to use our MS COM XPCOM Glue library and helper C++ classes. This way, you
|
---|
37 | * will get at least the following benefits:
|
---|
38 | *
|
---|
39 | * a) better portability: both the MS COM (used on Windows) and XPCOM (used
|
---|
40 | * everywhere else) VirtualBox client application from the same source code
|
---|
41 | * (including common smart C++ templates for automatic interface pointer
|
---|
42 | * reference counter and string data management);
|
---|
43 | * b) simpler XPCOM initialization and shutdown (only a signle method call
|
---|
44 | * that does everything right).
|
---|
45 | *
|
---|
46 | * Currently, there is no separate sample program that uses the VirtualBox MS
|
---|
47 | * COM XPCOM Glue library. Please refer to the sources of stock VirtualBox
|
---|
48 | * applications such as the VirtualBox GUI frontend or the VBoxManage command
|
---|
49 | * line frontend.
|
---|
50 | */
|
---|
51 |
|
---|
52 |
|
---|
53 | #include <stdio.h>
|
---|
54 | #include "VirtualBox.h"
|
---|
55 |
|
---|
56 |
|
---|
57 | int listVMs(IVirtualBox *virtualBox)
|
---|
58 | {
|
---|
59 | HRESULT rc;
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * First we have to get a list of all registered VMs
|
---|
63 | */
|
---|
64 | SAFEARRAY *machinesArray = NULL;
|
---|
65 |
|
---|
66 | rc = virtualBox->get_Machines2(&machinesArray);
|
---|
67 | if (SUCCEEDED(rc))
|
---|
68 | {
|
---|
69 | IMachine **machines;
|
---|
70 | rc = SafeArrayAccessData (machinesArray, (void **) &machines);
|
---|
71 | if (SUCCEEDED(rc))
|
---|
72 | {
|
---|
73 | for (ULONG i = 0; i < machinesArray->rgsabound[0].cElements; ++i)
|
---|
74 | {
|
---|
75 | BSTR str;
|
---|
76 |
|
---|
77 | rc = machines[i]->get_Name(&str);
|
---|
78 | if (SUCCEEDED(rc))
|
---|
79 | {
|
---|
80 | printf("Name: %S\n", str);
|
---|
81 | SysFreeString(str);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | SafeArrayUnaccessData (machinesArray);
|
---|
86 | }
|
---|
87 |
|
---|
88 | SafeArrayDestroy (machinesArray);
|
---|
89 | }
|
---|
90 |
|
---|
91 | return 0;
|
---|
92 | }
|
---|
93 |
|
---|
94 | int testErrorInfo(IVirtualBox *virtualBox)
|
---|
95 | {
|
---|
96 | HRESULT rc;
|
---|
97 |
|
---|
98 | /* try to find a machine that doesn't exist */
|
---|
99 | IMachine *machine = NULL;
|
---|
100 | BSTR machineName = SysAllocString(L"Foobar");
|
---|
101 |
|
---|
102 | rc = virtualBox->FindMachine(machineName, &machine);
|
---|
103 |
|
---|
104 | if (FAILED(rc))
|
---|
105 | {
|
---|
106 | IErrorInfo *errorInfo;
|
---|
107 |
|
---|
108 | rc = GetErrorInfo(0, &errorInfo);
|
---|
109 |
|
---|
110 | if (FAILED(rc))
|
---|
111 | printf("Error getting error info! rc = 0x%x\n", rc);
|
---|
112 | else
|
---|
113 | {
|
---|
114 | BSTR errorDescription = NULL;
|
---|
115 |
|
---|
116 | rc = errorInfo->GetDescription(&errorDescription);
|
---|
117 |
|
---|
118 | if (FAILED(rc) || !errorDescription)
|
---|
119 | printf("Error getting error description! rc = 0x%x\n", rc);
|
---|
120 | else
|
---|
121 | {
|
---|
122 | printf("Successfully retrieved error description: %S\n", errorDescription);
|
---|
123 |
|
---|
124 | SysFreeString(errorDescription);
|
---|
125 | }
|
---|
126 |
|
---|
127 | errorInfo->Release();
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (machine)
|
---|
132 | machine->Release();
|
---|
133 |
|
---|
134 | SysFreeString(machineName);
|
---|
135 |
|
---|
136 | return 0;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | int main(int argc, char *argv[])
|
---|
141 | {
|
---|
142 | HRESULT rc;
|
---|
143 | IVirtualBox *virtualBox;
|
---|
144 |
|
---|
145 | do
|
---|
146 | {
|
---|
147 | /* initialize the COM subsystem */
|
---|
148 | CoInitialize(NULL);
|
---|
149 |
|
---|
150 | /* instantiate the VirtualBox root object */
|
---|
151 | rc = CoCreateInstance(CLSID_VirtualBox, /* the VirtualBox base object */
|
---|
152 | NULL, /* no aggregation */
|
---|
153 | CLSCTX_LOCAL_SERVER, /* the object lives in a server process on this machine */
|
---|
154 | IID_IVirtualBox, /* IID of the interface */
|
---|
155 | (void**)&virtualBox);
|
---|
156 |
|
---|
157 | if (!SUCCEEDED(rc))
|
---|
158 | {
|
---|
159 | printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
|
---|
160 | break;
|
---|
161 | }
|
---|
162 |
|
---|
163 | listVMs(virtualBox);
|
---|
164 |
|
---|
165 | testErrorInfo(virtualBox);
|
---|
166 |
|
---|
167 | /* release the VirtualBox object */
|
---|
168 | virtualBox->Release();
|
---|
169 |
|
---|
170 | } while (0);
|
---|
171 |
|
---|
172 | CoUninitialize();
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|