1 | /* $Revision: 48225 $ */
|
---|
2 | /** @file tstXPCOMCGlue.c
|
---|
3 | * Demonstrator program to illustrate use of C bindings of Main API.
|
---|
4 | *
|
---|
5 | * Linux only at the moment due to shared library magic in the Makefile.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2009-2013 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #include "VBoxXPCOMCGlue.h"
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <string.h>
|
---|
26 | #include <stdlib.h>
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Start a VM.
|
---|
30 | *
|
---|
31 | * @param virtualBox ptr to IVirtualBox object
|
---|
32 | * @param session ptr to ISession object
|
---|
33 | * @param id identifies the machine to start
|
---|
34 | */
|
---|
35 | static void startVM(IVirtualBox *virtualBox, ISession *session, PRUnichar *id)
|
---|
36 | {
|
---|
37 | nsresult rc;
|
---|
38 | IMachine *machine = NULL;
|
---|
39 | IProgress *progress = NULL;
|
---|
40 | PRUnichar *env = NULL;
|
---|
41 | PRUnichar *sessionType;
|
---|
42 |
|
---|
43 | rc = virtualBox->vtbl->FindMachine(virtualBox, id, &machine);
|
---|
44 |
|
---|
45 | if (NS_FAILED(rc) || !machine)
|
---|
46 | {
|
---|
47 | fprintf(stderr, "Error: Couldn't get the machine handle.\n");
|
---|
48 | return;
|
---|
49 | }
|
---|
50 |
|
---|
51 | g_pVBoxFuncs->pfnUtf8ToUtf16("gui", &sessionType);
|
---|
52 |
|
---|
53 | rc = machine->vtbl->LaunchVMProcess(machine,
|
---|
54 | session,
|
---|
55 | sessionType,
|
---|
56 | env,
|
---|
57 | &progress
|
---|
58 | );
|
---|
59 |
|
---|
60 | g_pVBoxFuncs->pfnUtf16Free(sessionType);
|
---|
61 |
|
---|
62 | if (NS_FAILED(rc))
|
---|
63 | {
|
---|
64 | fprintf(stderr, "Error: OpenRemoteSession failed.\n");
|
---|
65 | }
|
---|
66 | else
|
---|
67 | {
|
---|
68 | PRBool completed;
|
---|
69 | PRInt32 resultCode;
|
---|
70 |
|
---|
71 | printf("Waiting for the remote session to open...\n");
|
---|
72 | progress->vtbl->WaitForCompletion(progress, -1);
|
---|
73 |
|
---|
74 | rc = progress->vtbl->GetCompleted(progress, &completed);
|
---|
75 | if (NS_FAILED(rc))
|
---|
76 | {
|
---|
77 | fprintf (stderr, "Error: GetCompleted status failed.\n");
|
---|
78 | }
|
---|
79 |
|
---|
80 | progress->vtbl->GetResultCode(progress, &resultCode);
|
---|
81 | if (NS_FAILED(resultCode))
|
---|
82 | {
|
---|
83 | IVirtualBoxErrorInfo *errorInfo;
|
---|
84 | PRUnichar *textUtf16;
|
---|
85 | char *text;
|
---|
86 |
|
---|
87 | progress->vtbl->GetErrorInfo(progress, &errorInfo);
|
---|
88 | errorInfo->vtbl->GetText(errorInfo, &textUtf16);
|
---|
89 | g_pVBoxFuncs->pfnUtf16ToUtf8(textUtf16, &text);
|
---|
90 | printf("Error: %s\n", text);
|
---|
91 |
|
---|
92 | g_pVBoxFuncs->pfnComUnallocMem(textUtf16);
|
---|
93 | g_pVBoxFuncs->pfnUtf8Free(text);
|
---|
94 | }
|
---|
95 | else
|
---|
96 | {
|
---|
97 | fprintf(stderr, "Remote session has been successfully opened.\n");
|
---|
98 | }
|
---|
99 | progress->vtbl->nsisupports.Release((nsISupports *)progress);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /* It's important to always release resources. */
|
---|
103 | machine->vtbl->nsisupports.Release((nsISupports *)machine);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * List the registered VMs.
|
---|
108 | *
|
---|
109 | * @param virtualBox ptr to IVirtualBox object
|
---|
110 | * @param session ptr to ISession object
|
---|
111 | */
|
---|
112 | static void listVMs(IVirtualBox *virtualBox, ISession *session)
|
---|
113 | {
|
---|
114 | nsresult rc;
|
---|
115 | IMachine **machines = NULL;
|
---|
116 | PRUint32 machineCnt = 0;
|
---|
117 | PRUint32 i;
|
---|
118 | unsigned start_id;
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Get the list of all registered VMs.
|
---|
122 | */
|
---|
123 |
|
---|
124 | rc = virtualBox->vtbl->GetMachines(virtualBox, &machineCnt, &machines);
|
---|
125 | if (NS_FAILED(rc))
|
---|
126 | {
|
---|
127 | fprintf(stderr, "could not get list of machines, rc=%08x\n",
|
---|
128 | (unsigned)rc);
|
---|
129 | return;
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (machineCnt == 0)
|
---|
133 | {
|
---|
134 | printf("\tNo VMs\n");
|
---|
135 | return;
|
---|
136 | }
|
---|
137 |
|
---|
138 | printf("VM List:\n\n");
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * Iterate through the collection.
|
---|
142 | */
|
---|
143 |
|
---|
144 | for (i = 0; i < machineCnt; ++i)
|
---|
145 | {
|
---|
146 | IMachine *machine = machines[i];
|
---|
147 | PRBool isAccessible = PR_FALSE;
|
---|
148 |
|
---|
149 | printf("\tMachine #%u\n", (unsigned)i);
|
---|
150 |
|
---|
151 | if (!machine)
|
---|
152 | {
|
---|
153 | printf("\t(skipped, NULL)\n");
|
---|
154 | continue;
|
---|
155 | }
|
---|
156 |
|
---|
157 | machine->vtbl->GetAccessible(machine, &isAccessible);
|
---|
158 |
|
---|
159 | if (isAccessible)
|
---|
160 | {
|
---|
161 | PRUnichar *machineNameUtf16;
|
---|
162 | char *machineName;
|
---|
163 |
|
---|
164 | machine->vtbl->GetName(machine, &machineNameUtf16);
|
---|
165 | g_pVBoxFuncs->pfnUtf16ToUtf8(machineNameUtf16,&machineName);
|
---|
166 | printf("\tName: %s\n", machineName);
|
---|
167 |
|
---|
168 | g_pVBoxFuncs->pfnUtf8Free(machineName);
|
---|
169 | g_pVBoxFuncs->pfnComUnallocMem(machineNameUtf16);
|
---|
170 | }
|
---|
171 | else
|
---|
172 | {
|
---|
173 | printf("\tName: <inaccessible>\n");
|
---|
174 | }
|
---|
175 |
|
---|
176 | {
|
---|
177 | PRUnichar *uuidUtf16 = NULL;
|
---|
178 | char *uuidUtf8 = NULL;
|
---|
179 |
|
---|
180 | machine->vtbl->GetId(machine, &uuidUtf16);
|
---|
181 | g_pVBoxFuncs->pfnUtf16ToUtf8(uuidUtf16, &uuidUtf8);
|
---|
182 | printf("\tUUID: %s\n", uuidUtf8);
|
---|
183 |
|
---|
184 | g_pVBoxFuncs->pfnUtf8Free(uuidUtf8);
|
---|
185 | g_pVBoxFuncs->pfnUtf16Free(uuidUtf16);
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (isAccessible)
|
---|
189 | {
|
---|
190 | {
|
---|
191 | PRUnichar *configFile;
|
---|
192 | char *configFile1 = calloc((size_t)64, (size_t)1);
|
---|
193 |
|
---|
194 | machine->vtbl->GetSettingsFilePath(machine, &configFile);
|
---|
195 | g_pVBoxFuncs->pfnUtf16ToUtf8(configFile, &configFile1);
|
---|
196 | printf("\tConfig file: %s\n", configFile1);
|
---|
197 |
|
---|
198 | free(configFile1);
|
---|
199 | g_pVBoxFuncs->pfnComUnallocMem(configFile);
|
---|
200 | }
|
---|
201 |
|
---|
202 | {
|
---|
203 | PRUint32 memorySize;
|
---|
204 |
|
---|
205 | machine->vtbl->GetMemorySize(machine, &memorySize);
|
---|
206 | printf("\tMemory size: %uMB\n", memorySize);
|
---|
207 | }
|
---|
208 |
|
---|
209 | {
|
---|
210 | PRUnichar *typeId;
|
---|
211 | PRUnichar *osNameUtf16;
|
---|
212 | char *osName;
|
---|
213 | IGuestOSType *osType = NULL;
|
---|
214 |
|
---|
215 | machine->vtbl->GetOSTypeId(machine, &typeId);
|
---|
216 | virtualBox->vtbl->GetGuestOSType(virtualBox, typeId, &osType);
|
---|
217 | osType->vtbl->GetDescription(osType, &osNameUtf16);
|
---|
218 | g_pVBoxFuncs->pfnUtf16ToUtf8(osNameUtf16,&osName);
|
---|
219 | printf("\tGuest OS: %s\n\n", osName);
|
---|
220 |
|
---|
221 | osType->vtbl->nsisupports.Release((nsISupports *)osType);
|
---|
222 | g_pVBoxFuncs->pfnUtf8Free(osName);
|
---|
223 | g_pVBoxFuncs->pfnComUnallocMem(osNameUtf16);
|
---|
224 | g_pVBoxFuncs->pfnComUnallocMem(typeId);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * Let the user chose a machine to start.
|
---|
231 | */
|
---|
232 |
|
---|
233 | printf("Type Machine# to start (0 - %u) or 'quit' to do nothing: ",
|
---|
234 | (unsigned)(machineCnt - 1));
|
---|
235 | fflush(stdout);
|
---|
236 |
|
---|
237 | if (scanf("%u", &start_id) == 1 && start_id < machineCnt)
|
---|
238 | {
|
---|
239 | IMachine *machine = machines[start_id];
|
---|
240 |
|
---|
241 | if (machine)
|
---|
242 | {
|
---|
243 | PRUnichar *uuidUtf16 = NULL;
|
---|
244 |
|
---|
245 | machine->vtbl->GetId(machine, &uuidUtf16);
|
---|
246 | startVM(virtualBox, session, uuidUtf16);
|
---|
247 |
|
---|
248 | g_pVBoxFuncs->pfnUtf16Free(uuidUtf16);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | /*
|
---|
253 | * Don't forget to release the objects in the array.
|
---|
254 | */
|
---|
255 |
|
---|
256 | for (i = 0; i < machineCnt; ++i)
|
---|
257 | {
|
---|
258 | IMachine *machine = machines[i];
|
---|
259 |
|
---|
260 | if (machine)
|
---|
261 | {
|
---|
262 | machine->vtbl->nsisupports.Release((nsISupports *)machine);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | /* Main - Start the ball rolling. */
|
---|
268 |
|
---|
269 | int main(int argc, char **argv)
|
---|
270 | {
|
---|
271 | IVirtualBox *vbox = NULL;
|
---|
272 | ISession *session = NULL;
|
---|
273 | PRUint32 revision = 0;
|
---|
274 | PRUnichar *versionUtf16 = NULL;
|
---|
275 | PRUnichar *homefolderUtf16 = NULL;
|
---|
276 | nsresult rc; /* Result code of various function (method) calls. */
|
---|
277 |
|
---|
278 | printf("Starting Main\n");
|
---|
279 |
|
---|
280 | /*
|
---|
281 | * VBoxComInitialize does all the necessary startup action and
|
---|
282 | * provides us with pointers to vbox and session handles.
|
---|
283 | * It should be matched by a call to VBoxComUninitialize(vbox)
|
---|
284 | * when done.
|
---|
285 | */
|
---|
286 |
|
---|
287 | if (VBoxCGlueInit() != 0)
|
---|
288 | {
|
---|
289 | fprintf(stderr, "%s: FATAL: VBoxCGlueInit failed: %s\n",
|
---|
290 | argv[0], g_szVBoxErrMsg);
|
---|
291 | return EXIT_FAILURE;
|
---|
292 | }
|
---|
293 |
|
---|
294 | g_pVBoxFuncs->pfnComInitialize(IVIRTUALBOX_IID_STR, &vbox,
|
---|
295 | ISESSION_IID_STR, &session);
|
---|
296 | if (vbox == NULL)
|
---|
297 | {
|
---|
298 | fprintf(stderr, "%s: FATAL: could not get vbox handle\n", argv[0]);
|
---|
299 | return EXIT_FAILURE;
|
---|
300 | }
|
---|
301 | if (session == NULL)
|
---|
302 | {
|
---|
303 | fprintf(stderr, "%s: FATAL: could not get session handle\n", argv[0]);
|
---|
304 | return EXIT_FAILURE;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * Now ask for revision, version and home folder information of
|
---|
309 | * this vbox. Were not using fancy macros here so it
|
---|
310 | * remains easy to see how we access C++'s vtable.
|
---|
311 | */
|
---|
312 |
|
---|
313 | printf("----------------------------------------------------\n");
|
---|
314 |
|
---|
315 | /* 1. Revision */
|
---|
316 |
|
---|
317 | rc = vbox->vtbl->GetRevision(vbox, &revision);
|
---|
318 | if (NS_SUCCEEDED(rc))
|
---|
319 | {
|
---|
320 | printf("\tRevision: %u\n", revision);
|
---|
321 | }
|
---|
322 | else
|
---|
323 | {
|
---|
324 | fprintf(stderr, "%s: GetRevision() returned %08x\n",
|
---|
325 | argv[0], (unsigned)rc);
|
---|
326 | }
|
---|
327 |
|
---|
328 | /* 2. Version */
|
---|
329 |
|
---|
330 | rc = vbox->vtbl->GetVersion(vbox, &versionUtf16);
|
---|
331 | if (NS_SUCCEEDED(rc))
|
---|
332 | {
|
---|
333 | char *version = NULL;
|
---|
334 | g_pVBoxFuncs->pfnUtf16ToUtf8(versionUtf16, &version);
|
---|
335 | printf("\tVersion: %s\n", version);
|
---|
336 | g_pVBoxFuncs->pfnUtf8Free(version);
|
---|
337 | g_pVBoxFuncs->pfnComUnallocMem(versionUtf16);
|
---|
338 | }
|
---|
339 | else
|
---|
340 | {
|
---|
341 | fprintf(stderr, "%s: GetVersion() returned %08x\n",
|
---|
342 | argv[0], (unsigned)rc);
|
---|
343 | }
|
---|
344 |
|
---|
345 | /* 3. Home Folder */
|
---|
346 |
|
---|
347 | rc = vbox->vtbl->GetHomeFolder(vbox, &homefolderUtf16);
|
---|
348 | if (NS_SUCCEEDED(rc))
|
---|
349 | {
|
---|
350 | char *homefolder = NULL;
|
---|
351 | g_pVBoxFuncs->pfnUtf16ToUtf8(homefolderUtf16, &homefolder);
|
---|
352 | printf("\tHomeFolder: %s\n", homefolder);
|
---|
353 | g_pVBoxFuncs->pfnUtf8Free(homefolder);
|
---|
354 | g_pVBoxFuncs->pfnComUnallocMem(homefolderUtf16);
|
---|
355 | }
|
---|
356 | else
|
---|
357 | {
|
---|
358 | fprintf(stderr, "%s: GetHomeFolder() returned %08x\n",
|
---|
359 | argv[0], (unsigned)rc);
|
---|
360 | }
|
---|
361 |
|
---|
362 | listVMs(vbox, session);
|
---|
363 | session->vtbl->UnlockMachine(session);
|
---|
364 |
|
---|
365 | printf("----------------------------------------------------\n");
|
---|
366 |
|
---|
367 | /*
|
---|
368 | * Do as mom told us: always clean up after yourself.
|
---|
369 | */
|
---|
370 |
|
---|
371 | g_pVBoxFuncs->pfnComUninitialize();
|
---|
372 | VBoxCGlueTerm();
|
---|
373 | printf("Finished Main\n");
|
---|
374 |
|
---|
375 | return 0;
|
---|
376 | }
|
---|
377 | /* vim: set ts=4 sw=4 et: */
|
---|