[30269] | 1 | /* $Id: tstVBoxAPIWin.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
[11115] | 2 | /** @file
|
---|
| 3 | *
|
---|
| 4 | * tstVBoxAPIWin - sample program to illustrate the VirtualBox
|
---|
| 5 | * COM API for machine management on Windows.
|
---|
| 6 | It only uses standard C/C++ and COM semantics,
|
---|
| 7 | * no additional VBox classes/macros/helpers. To
|
---|
| 8 | * make things even easier to follow, only the
|
---|
| 9 | * standard Win32 API has been used. Typically,
|
---|
| 10 | * C++ developers would make use of Microsoft's
|
---|
| 11 | * ATL to ease development.
|
---|
| 12 | */
|
---|
| 13 |
|
---|
| 14 | /*
|
---|
[93115] | 15 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
[5999] | 16 | *
|
---|
| 17 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 18 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 19 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 20 | * General Public License (GPL) as published by the Free Software
|
---|
| 21 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 22 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 23 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[11115] | 24 | */
|
---|
| 25 |
|
---|
| 26 | /*
|
---|
| 27 | * PURPOSE OF THIS SAMPLE PROGRAM
|
---|
| 28 | * ------------------------------
|
---|
| 29 | *
|
---|
| 30 | * This sample program is intended to demonstrate the minimal code necessary
|
---|
| 31 | * to use VirtualBox COM API for learning puroses only. The program uses pure
|
---|
| 32 | * Win32 API and doesn't have any extra dependencies to let you better
|
---|
| 33 | * understand what is going on when a client talks to the VirtualBox core
|
---|
| 34 | * using the COM framework.
|
---|
| 35 | *
|
---|
| 36 | * However, if you want to write a real application, it is highly recommended
|
---|
| 37 | * to use our MS COM XPCOM Glue library and helper C++ classes. This way, you
|
---|
| 38 | * will get at least the following benefits:
|
---|
| 39 | *
|
---|
| 40 | * a) better portability: both the MS COM (used on Windows) and XPCOM (used
|
---|
| 41 | * everywhere else) VirtualBox client application from the same source code
|
---|
| 42 | * (including common smart C++ templates for automatic interface pointer
|
---|
| 43 | * reference counter and string data management);
|
---|
[33540] | 44 | * b) simpler XPCOM initialization and shutdown (only a single method call
|
---|
[11115] | 45 | * that does everything right).
|
---|
| 46 | *
|
---|
| 47 | * Currently, there is no separate sample program that uses the VirtualBox MS
|
---|
| 48 | * COM XPCOM Glue library. Please refer to the sources of stock VirtualBox
|
---|
| 49 | * applications such as the VirtualBox GUI frontend or the VBoxManage command
|
---|
| 50 | * line frontend.
|
---|
| 51 | */
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | #include <stdio.h>
|
---|
[62768] | 55 | #include <iprt/win/windows.h> /* Avoid -Wall warnings. */
|
---|
[11115] | 56 | #include "VirtualBox.h"
|
---|
| 57 |
|
---|
[19477] | 58 | #define SAFE_RELEASE(x) \
|
---|
| 59 | if (x) { \
|
---|
| 60 | x->Release(); \
|
---|
| 61 | x = NULL; \
|
---|
| 62 | }
|
---|
[11115] | 63 |
|
---|
| 64 | int listVMs(IVirtualBox *virtualBox)
|
---|
| 65 | {
|
---|
| 66 | HRESULT rc;
|
---|
| 67 |
|
---|
| 68 | /*
|
---|
| 69 | * First we have to get a list of all registered VMs
|
---|
| 70 | */
|
---|
| 71 | SAFEARRAY *machinesArray = NULL;
|
---|
| 72 |
|
---|
[17737] | 73 | rc = virtualBox->get_Machines(&machinesArray);
|
---|
[11115] | 74 | if (SUCCEEDED(rc))
|
---|
| 75 | {
|
---|
| 76 | IMachine **machines;
|
---|
[50339] | 77 | rc = SafeArrayAccessData(machinesArray, (void **) &machines);
|
---|
[11115] | 78 | if (SUCCEEDED(rc))
|
---|
| 79 | {
|
---|
| 80 | for (ULONG i = 0; i < machinesArray->rgsabound[0].cElements; ++i)
|
---|
| 81 | {
|
---|
| 82 | BSTR str;
|
---|
| 83 |
|
---|
| 84 | rc = machines[i]->get_Name(&str);
|
---|
| 85 | if (SUCCEEDED(rc))
|
---|
| 86 | {
|
---|
| 87 | printf("Name: %S\n", str);
|
---|
| 88 | SysFreeString(str);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[50339] | 92 | SafeArrayUnaccessData(machinesArray);
|
---|
[11115] | 93 | }
|
---|
| 94 |
|
---|
[50339] | 95 | SafeArrayDestroy(machinesArray);
|
---|
[11115] | 96 | }
|
---|
| 97 |
|
---|
| 98 | return 0;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[19477] | 101 |
|
---|
[11115] | 102 | int testErrorInfo(IVirtualBox *virtualBox)
|
---|
| 103 | {
|
---|
| 104 | HRESULT rc;
|
---|
| 105 |
|
---|
[19477] | 106 | /* Try to find a machine that doesn't exist */
|
---|
[11115] | 107 | IMachine *machine = NULL;
|
---|
| 108 | BSTR machineName = SysAllocString(L"Foobar");
|
---|
| 109 |
|
---|
| 110 | rc = virtualBox->FindMachine(machineName, &machine);
|
---|
| 111 |
|
---|
| 112 | if (FAILED(rc))
|
---|
| 113 | {
|
---|
| 114 | IErrorInfo *errorInfo;
|
---|
| 115 |
|
---|
| 116 | rc = GetErrorInfo(0, &errorInfo);
|
---|
| 117 |
|
---|
| 118 | if (FAILED(rc))
|
---|
[83820] | 119 | printf("Error getting error info! rc=%#lx\n", rc);
|
---|
[11115] | 120 | else
|
---|
| 121 | {
|
---|
| 122 | BSTR errorDescription = NULL;
|
---|
| 123 |
|
---|
| 124 | rc = errorInfo->GetDescription(&errorDescription);
|
---|
| 125 |
|
---|
| 126 | if (FAILED(rc) || !errorDescription)
|
---|
[83820] | 127 | printf("Error getting error description! rc=%#lx\n", rc);
|
---|
[11115] | 128 | else
|
---|
| 129 | {
|
---|
| 130 | printf("Successfully retrieved error description: %S\n", errorDescription);
|
---|
| 131 |
|
---|
| 132 | SysFreeString(errorDescription);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | errorInfo->Release();
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[19477] | 139 | SAFE_RELEASE(machine);
|
---|
| 140 | SysFreeString(machineName);
|
---|
[11115] | 141 |
|
---|
[19477] | 142 | return 0;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | int testStartVM(IVirtualBox *virtualBox)
|
---|
| 147 | {
|
---|
| 148 | HRESULT rc;
|
---|
| 149 |
|
---|
| 150 | /* Try to start a VM called "WinXP SP2". */
|
---|
| 151 | IMachine *machine = NULL;
|
---|
| 152 | BSTR machineName = SysAllocString(L"WinXP SP2");
|
---|
| 153 |
|
---|
| 154 | rc = virtualBox->FindMachine(machineName, &machine);
|
---|
| 155 |
|
---|
| 156 | if (FAILED(rc))
|
---|
| 157 | {
|
---|
| 158 | IErrorInfo *errorInfo;
|
---|
| 159 |
|
---|
| 160 | rc = GetErrorInfo(0, &errorInfo);
|
---|
| 161 |
|
---|
| 162 | if (FAILED(rc))
|
---|
[83820] | 163 | printf("Error getting error info! rc=%#lx\n", rc);
|
---|
[19477] | 164 | else
|
---|
| 165 | {
|
---|
| 166 | BSTR errorDescription = NULL;
|
---|
| 167 |
|
---|
| 168 | rc = errorInfo->GetDescription(&errorDescription);
|
---|
| 169 |
|
---|
| 170 | if (FAILED(rc) || !errorDescription)
|
---|
[83820] | 171 | printf("Error getting error description! rc=%#lx\n", rc);
|
---|
[19477] | 172 | else
|
---|
| 173 | {
|
---|
| 174 | printf("Successfully retrieved error description: %S\n", errorDescription);
|
---|
| 175 |
|
---|
| 176 | SysFreeString(errorDescription);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | SAFE_RELEASE(errorInfo);
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | else
|
---|
| 183 | {
|
---|
| 184 | ISession *session = NULL;
|
---|
| 185 | IConsole *console = NULL;
|
---|
| 186 | IProgress *progress = NULL;
|
---|
| 187 | BSTR sessiontype = SysAllocString(L"gui");
|
---|
| 188 | BSTR guid;
|
---|
| 189 |
|
---|
| 190 | do
|
---|
| 191 | {
|
---|
| 192 | rc = machine->get_Id(&guid); /* Get the GUID of the machine. */
|
---|
| 193 | if (!SUCCEEDED(rc))
|
---|
| 194 | {
|
---|
[83820] | 195 | printf("Error retrieving machine ID! rc=%#lx\n", rc);
|
---|
[19477] | 196 | break;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /* Create the session object. */
|
---|
| 200 | rc = CoCreateInstance(CLSID_Session, /* the VirtualBox base object */
|
---|
| 201 | NULL, /* no aggregation */
|
---|
[60063] | 202 | CLSCTX_INPROC_SERVER, /* the object lives in the current process */
|
---|
[19477] | 203 | IID_ISession, /* IID of the interface */
|
---|
| 204 | (void**)&session);
|
---|
| 205 | if (!SUCCEEDED(rc))
|
---|
| 206 | {
|
---|
[83820] | 207 | printf("Error creating Session instance! rc=%#lx\n", rc);
|
---|
[19477] | 208 | break;
|
---|
| 209 | }
|
---|
[20931] | 210 |
|
---|
[19477] | 211 | /* Start a VM session using the delivered VBox GUI. */
|
---|
[31008] | 212 | rc = machine->LaunchVMProcess(session, sessiontype,
|
---|
| 213 | NULL, &progress);
|
---|
[19477] | 214 | if (!SUCCEEDED(rc))
|
---|
| 215 | {
|
---|
[83820] | 216 | printf("Could not open remote session! rc=%#lx\n", rc);
|
---|
[19477] | 217 | break;
|
---|
| 218 | }
|
---|
[20931] | 219 |
|
---|
[19477] | 220 | /* Wait until VM is running. */
|
---|
[50339] | 221 | printf("Starting VM, please wait ...\n");
|
---|
| 222 | rc = progress->WaitForCompletion(-1);
|
---|
[20931] | 223 |
|
---|
[19477] | 224 | /* Get console object. */
|
---|
| 225 | session->get_Console(&console);
|
---|
| 226 |
|
---|
| 227 | /* Bring console window to front. */
|
---|
| 228 | machine->ShowConsoleWindow(0);
|
---|
| 229 |
|
---|
[50339] | 230 | printf("Press enter to power off VM and close the session...\n");
|
---|
[19477] | 231 | getchar();
|
---|
| 232 |
|
---|
| 233 | /* Power down the machine. */
|
---|
[20931] | 234 | rc = console->PowerDown(&progress);
|
---|
[19477] | 235 |
|
---|
[20931] | 236 | /* Wait until VM is powered down. */
|
---|
[50339] | 237 | printf("Powering off VM, please wait ...\n");
|
---|
| 238 | rc = progress->WaitForCompletion(-1);
|
---|
[20931] | 239 |
|
---|
[19477] | 240 | /* Close the session. */
|
---|
[31070] | 241 | rc = session->UnlockMachine();
|
---|
[20931] | 242 |
|
---|
[19477] | 243 | } while (0);
|
---|
| 244 |
|
---|
| 245 | SAFE_RELEASE(console);
|
---|
| 246 | SAFE_RELEASE(progress);
|
---|
| 247 | SAFE_RELEASE(session);
|
---|
| 248 | SysFreeString(guid);
|
---|
| 249 | SysFreeString(sessiontype);
|
---|
| 250 | SAFE_RELEASE(machine);
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[11115] | 253 | SysFreeString(machineName);
|
---|
| 254 |
|
---|
| 255 | return 0;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
[62768] | 259 | int main()
|
---|
[11115] | 260 | {
|
---|
[62769] | 261 | /* Initialize the COM subsystem. */
|
---|
| 262 | CoInitialize(NULL);
|
---|
| 263 |
|
---|
| 264 | /* Instantiate the VirtualBox root object. */
|
---|
[60063] | 265 | IVirtualBoxClient *virtualBoxClient;
|
---|
[62769] | 266 | HRESULT rc = CoCreateInstance(CLSID_VirtualBoxClient, /* the VirtualBoxClient object */
|
---|
| 267 | NULL, /* no aggregation */
|
---|
| 268 | CLSCTX_INPROC_SERVER, /* the object lives in the current process */
|
---|
| 269 | IID_IVirtualBoxClient, /* IID of the interface */
|
---|
| 270 | (void**)&virtualBoxClient);
|
---|
| 271 | if (SUCCEEDED(rc))
|
---|
[11115] | 272 | {
|
---|
[62769] | 273 | IVirtualBox *virtualBox;
|
---|
| 274 | rc = virtualBoxClient->get_VirtualBox(&virtualBox);
|
---|
[60063] | 275 | if (SUCCEEDED(rc))
|
---|
[11115] | 276 | {
|
---|
[62769] | 277 | listVMs(virtualBox);
|
---|
[11115] | 278 |
|
---|
[62769] | 279 | testErrorInfo(virtualBox);
|
---|
[11115] | 280 |
|
---|
[62769] | 281 | /* Enable the following line to get a VM started. */
|
---|
| 282 | //testStartVM(virtualBox);
|
---|
[11115] | 283 |
|
---|
[62769] | 284 | /* Release the VirtualBox object. */
|
---|
| 285 | virtualBox->Release();
|
---|
| 286 | virtualBoxClient->Release();
|
---|
| 287 | }
|
---|
| 288 | else
|
---|
[83820] | 289 | printf("Error creating VirtualBox instance! rc=%#lx\n", rc);
|
---|
[62769] | 290 | }
|
---|
[19477] | 291 |
|
---|
[11115] | 292 | CoUninitialize();
|
---|
| 293 | return 0;
|
---|
| 294 | }
|
---|
| 295 |
|
---|