[55401] | 1 | /* $Id: tstVBoxAPIXPCOM.cpp 103356 2024-02-14 14:19:45Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
| 3 | *
|
---|
[50339] | 4 | * tstVBoxAPIXPCOM - sample program to illustrate the VirtualBox
|
---|
| 5 | * XPCOM API for machine management.
|
---|
[1] | 6 | * It only uses standard C/C++ and XPCOM semantics,
|
---|
| 7 | * no additional VBox classes/macros/helpers.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | /*
|
---|
[98103] | 11 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 12 | *
|
---|
[96407] | 13 | * This file is part of VirtualBox base platform packages, as
|
---|
| 14 | * available from https://www.virtualbox.org.
|
---|
| 15 | *
|
---|
| 16 | * This program is free software; you can redistribute it and/or
|
---|
| 17 | * modify it under the terms of the GNU General Public License
|
---|
| 18 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 19 | * License.
|
---|
| 20 | *
|
---|
| 21 | * This program is distributed in the hope that it will be useful, but
|
---|
| 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 24 | * General Public License for more details.
|
---|
| 25 | *
|
---|
| 26 | * You should have received a copy of the GNU General Public License
|
---|
| 27 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 28 | *
|
---|
| 29 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[1] | 30 | */
|
---|
| 31 |
|
---|
[5501] | 32 | /*
|
---|
| 33 | * PURPOSE OF THIS SAMPLE PROGRAM
|
---|
| 34 | * ------------------------------
|
---|
| 35 | *
|
---|
| 36 | * This sample program is intended to demonstrate the minimal code necessary
|
---|
| 37 | * to use VirtualBox XPCOM API for learning puroses only. The program uses
|
---|
| 38 | * pure XPCOM and doesn't have any extra dependencies to let you better
|
---|
| 39 | * understand what is going on when a client talks to the VirtualBox core
|
---|
| 40 | * using the XPCOM framework.
|
---|
| 41 | *
|
---|
| 42 | * However, if you want to write a real application, it is highly recommended
|
---|
| 43 | * to use our MS COM XPCOM Glue library and helper C++ classes. This way, you
|
---|
| 44 | * will get at least the following benefits:
|
---|
| 45 | *
|
---|
| 46 | * a) better portability: both the MS COM (used on Windows) and XPCOM (used
|
---|
| 47 | * everywhere else) VirtualBox client application from the same source code
|
---|
| 48 | * (including common smart C++ templates for automatic interface pointer
|
---|
| 49 | * reference counter and string data management);
|
---|
[33540] | 50 | * b) simpler XPCOM initialization and shutdown (only a single method call
|
---|
[5501] | 51 | * that does everything right).
|
---|
| 52 | *
|
---|
| 53 | * Currently, there is no separate sample program that uses the VirtualBox MS
|
---|
| 54 | * COM XPCOM Glue library. Please refer to the sources of stock VirtualBox
|
---|
| 55 | * applications such as the VirtualBox GUI frontend or the VBoxManage command
|
---|
| 56 | * line frontend.
|
---|
| 57 | *
|
---|
| 58 | *
|
---|
| 59 | * RUNNING THIS SAMPLE PROGRAM
|
---|
| 60 | * ---------------------------
|
---|
| 61 | *
|
---|
| 62 | * This sample program needs to know where the VirtualBox core files reside
|
---|
| 63 | * and where to search for VirtualBox shared libraries. Therefore, you need to
|
---|
| 64 | * use the following (or similar) command to execute it:
|
---|
| 65 | *
|
---|
[50339] | 66 | * $ env VBOX_XPCOM_HOME=../../.. LD_LIBRARY_PATH=../../.. ./tstVBoxAPIXPCOM
|
---|
[5501] | 67 | *
|
---|
| 68 | * The above command assumes that VBoxRT.so, VBoxXPCOM.so and others reside in
|
---|
| 69 | * the directory ../../..
|
---|
| 70 | */
|
---|
| 71 |
|
---|
| 72 |
|
---|
[1] | 73 | #include <stdio.h>
|
---|
| 74 | #include <stdlib.h>
|
---|
| 75 | #include <iconv.h>
|
---|
| 76 |
|
---|
| 77 | /*
|
---|
| 78 | * Include the XPCOM headers
|
---|
| 79 | */
|
---|
| 80 | #include <nsMemory.h>
|
---|
| 81 | #include <nsString.h>
|
---|
[4246] | 82 | #include <nsIServiceManager.h>
|
---|
| 83 | #include <nsEventQueueUtils.h>
|
---|
[1] | 84 |
|
---|
[4246] | 85 | #include <nsIExceptionService.h>
|
---|
[1] | 86 |
|
---|
| 87 | /*
|
---|
| 88 | * VirtualBox XPCOM interface. This header is generated
|
---|
| 89 | * from IDL which in turn is generated from a custom XML format.
|
---|
| 90 | */
|
---|
| 91 | #include "VirtualBox_XPCOM.h"
|
---|
| 92 |
|
---|
| 93 | /*
|
---|
| 94 | * Prototypes
|
---|
| 95 | */
|
---|
[5501] | 96 |
|
---|
[1] | 97 | char *nsIDToString(nsID *guid);
|
---|
[4246] | 98 | void printErrorInfo();
|
---|
[1] | 99 |
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * Display all registered VMs on the screen with some information about each
|
---|
| 103 | *
|
---|
| 104 | * @param virtualBox VirtualBox instance object.
|
---|
| 105 | */
|
---|
[99775] | 106 | static void listVMs(IVirtualBox *virtualBox)
|
---|
[1] | 107 | {
|
---|
| 108 | nsresult rc;
|
---|
| 109 |
|
---|
[50339] | 110 | printf("----------------------------------------------------\n");
|
---|
| 111 | printf("VM List:\n\n");
|
---|
[1] | 112 |
|
---|
| 113 | /*
|
---|
| 114 | * Get the list of all registered VMs
|
---|
| 115 | */
|
---|
[13580] | 116 | IMachine **machines = NULL;
|
---|
[86402] | 117 | PRUint32 cMachines = 0;
|
---|
[13580] | 118 |
|
---|
[86402] | 119 | rc = virtualBox->GetMachines(&cMachines, &machines);
|
---|
[1] | 120 | if (NS_SUCCEEDED(rc))
|
---|
| 121 | {
|
---|
| 122 | /*
|
---|
| 123 | * Iterate through the collection
|
---|
| 124 | */
|
---|
[86402] | 125 | for (PRUint32 i = 0; i < cMachines; ++ i)
|
---|
[1] | 126 | {
|
---|
[26186] | 127 | IMachine *machine = machines[i];
|
---|
[13580] | 128 | if (machine)
|
---|
[1] | 129 | {
|
---|
[5501] | 130 | PRBool isAccessible = PR_FALSE;
|
---|
[26186] | 131 | machine->GetAccessible(&isAccessible);
|
---|
[1] | 132 |
|
---|
[5501] | 133 | if (isAccessible)
|
---|
| 134 | {
|
---|
| 135 | nsXPIDLString machineName;
|
---|
| 136 | machine->GetName(getter_Copies(machineName));
|
---|
| 137 | char *machineNameAscii = ToNewCString(machineName);
|
---|
[50339] | 138 | printf("\tName: %s\n", machineNameAscii);
|
---|
[5501] | 139 | free(machineNameAscii);
|
---|
| 140 | }
|
---|
| 141 | else
|
---|
| 142 | {
|
---|
[50339] | 143 | printf("\tName: <inaccessible>\n");
|
---|
[5501] | 144 | }
|
---|
| 145 |
|
---|
[19242] | 146 | nsXPIDLString iid;
|
---|
| 147 | machine->GetId(getter_Copies(iid));
|
---|
| 148 | const char *uuidString = ToNewCString(iid);
|
---|
[50339] | 149 | printf("\tUUID: %s\n", uuidString);
|
---|
[1] | 150 | free((void*)uuidString);
|
---|
| 151 |
|
---|
[5501] | 152 | if (isAccessible)
|
---|
| 153 | {
|
---|
| 154 | nsXPIDLString configFile;
|
---|
| 155 | machine->GetSettingsFilePath(getter_Copies(configFile));
|
---|
| 156 | char *configFileAscii = ToNewCString(configFile);
|
---|
[50339] | 157 | printf("\tConfig file: %s\n", configFileAscii);
|
---|
[5501] | 158 | free(configFileAscii);
|
---|
[1] | 159 |
|
---|
[5501] | 160 | PRUint32 memorySize;
|
---|
| 161 | machine->GetMemorySize(&memorySize);
|
---|
[50339] | 162 | printf("\tMemory size: %uMB\n", memorySize);
|
---|
[1] | 163 |
|
---|
[5501] | 164 | nsXPIDLString typeId;
|
---|
| 165 | machine->GetOSTypeId(getter_Copies(typeId));
|
---|
| 166 | IGuestOSType *osType = nsnull;
|
---|
[49452] | 167 | virtualBox->GetGuestOSType(typeId.get(), &osType);
|
---|
[5501] | 168 | nsXPIDLString osName;
|
---|
| 169 | osType->GetDescription(getter_Copies(osName));
|
---|
| 170 | char *osNameAscii = ToNewCString(osName);
|
---|
[50339] | 171 | printf("\tGuest OS: %s\n\n", osNameAscii);
|
---|
[5501] | 172 | free(osNameAscii);
|
---|
| 173 | osType->Release();
|
---|
| 174 | }
|
---|
[1] | 175 |
|
---|
[13580] | 176 | /* don't forget to release the objects in the array... */
|
---|
[1] | 177 | machine->Release();
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
[86402] | 180 | nsMemory::Free(machines);
|
---|
[1] | 181 | }
|
---|
[50339] | 182 | printf("----------------------------------------------------\n\n");
|
---|
[1] | 183 | }
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * Create a sample VM
|
---|
| 187 | *
|
---|
| 188 | * @param virtualBox VirtualBox instance object.
|
---|
| 189 | */
|
---|
[99775] | 190 | static void createVM(IVirtualBox *virtualBox)
|
---|
[1] | 191 | {
|
---|
| 192 | nsresult rc;
|
---|
| 193 | /*
|
---|
[4246] | 194 | * First create a unnamed new VM. It will be unconfigured and not be saved
|
---|
[1] | 195 | * in the configuration until we explicitely choose to do so.
|
---|
| 196 | */
|
---|
[33451] | 197 | nsCOMPtr<IMachine> machine;
|
---|
[33458] | 198 | rc = virtualBox->CreateMachine(NULL, /* settings file */
|
---|
[33451] | 199 | NS_LITERAL_STRING("A brand new name").get(),
|
---|
[101035] | 200 | PlatformArchitecture_x86,
|
---|
[42131] | 201 | 0, nsnull, /* groups (safearray)*/
|
---|
[33458] | 202 | nsnull, /* ostype */
|
---|
[43041] | 203 | nsnull, /* create flags */
|
---|
[94660] | 204 | nsnull, /* cipher */
|
---|
| 205 | nsnull, /* password id */
|
---|
| 206 | nsnull, /* password */
|
---|
[33451] | 207 | getter_AddRefs(machine));
|
---|
[4246] | 208 | if (NS_FAILED(rc))
|
---|
| 209 | {
|
---|
[50339] | 210 | printf("Error: could not create machine! rc=%#x\n", rc);
|
---|
[4246] | 211 | return;
|
---|
| 212 | }
|
---|
[1] | 213 |
|
---|
| 214 | /*
|
---|
| 215 | * Set some properties
|
---|
| 216 | */
|
---|
| 217 | /* alternative to illustrate the use of string classes */
|
---|
[4246] | 218 | rc = machine->SetName(NS_ConvertUTF8toUTF16("A new name").get());
|
---|
[1] | 219 | rc = machine->SetMemorySize(128);
|
---|
| 220 |
|
---|
| 221 | /*
|
---|
| 222 | * Now a more advanced property -- the guest OS type. This is
|
---|
| 223 | * an object by itself which has to be found first. Note that we
|
---|
| 224 | * use the ID of the guest OS type here which is an internal
|
---|
| 225 | * representation (you can find that by configuring the OS type of
|
---|
| 226 | * a machine in the GUI and then looking at the <Guest ostype=""/>
|
---|
| 227 | * setting in the XML file. It is also possible to get the OS type from
|
---|
| 228 | * its description (win2k would be "Windows 2000") by getting the
|
---|
| 229 | * guest OS type collection and enumerating it.
|
---|
| 230 | */
|
---|
[33451] | 231 | nsCOMPtr<IGuestOSType> osType;
|
---|
[101381] | 232 | rc = virtualBox->GetGuestOSType(NS_LITERAL_STRING(GUEST_OS_ID_STR_X86("Windows2000")).get(),
|
---|
[4246] | 233 | getter_AddRefs(osType));
|
---|
| 234 | if (NS_FAILED(rc))
|
---|
[1] | 235 | {
|
---|
[50339] | 236 | printf("Error: could not find guest OS type! rc=%#x\n", rc);
|
---|
[1] | 237 | }
|
---|
| 238 | else
|
---|
| 239 | {
|
---|
[101381] | 240 | machine->SetOSTypeId(NS_LITERAL_STRING(GUEST_OS_ID_STR_X86("Windows2000")).get());
|
---|
[1] | 241 | }
|
---|
| 242 |
|
---|
| 243 | /*
|
---|
[4246] | 244 | * Register the VM. Note that this call also saves the VM config
|
---|
| 245 | * to disk. It is also possible to save the VM settings but not
|
---|
| 246 | * register the VM.
|
---|
| 247 | *
|
---|
| 248 | * Also note that due to current VirtualBox limitations, the machine
|
---|
| 249 | * must be registered *before* we can attach hard disks to it.
|
---|
| 250 | */
|
---|
| 251 | rc = virtualBox->RegisterMachine(machine);
|
---|
| 252 | if (NS_FAILED(rc))
|
---|
| 253 | {
|
---|
[50339] | 254 | printf("Error: could not register machine! rc=%#x\n", rc);
|
---|
[4246] | 255 | printErrorInfo();
|
---|
| 256 | return;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[47813] | 259 | nsCOMPtr<IMachine> origMachine = machine;
|
---|
| 260 |
|
---|
[4246] | 261 | /*
|
---|
| 262 | * In order to manipulate the registered machine, we must open a session
|
---|
| 263 | * for that machine. Do it now.
|
---|
| 264 | */
|
---|
| 265 | nsCOMPtr<ISession> session;
|
---|
[47813] | 266 | nsCOMPtr<IMachine> sessionMachine;
|
---|
[4246] | 267 | {
|
---|
| 268 | nsCOMPtr<nsIComponentManager> manager;
|
---|
[49452] | 269 | rc = NS_GetComponentManager(getter_AddRefs(manager));
|
---|
[4246] | 270 | if (NS_FAILED(rc))
|
---|
| 271 | {
|
---|
[50339] | 272 | printf("Error: could not get component manager! rc=%#x\n", rc);
|
---|
[4246] | 273 | return;
|
---|
| 274 | }
|
---|
[49452] | 275 | rc = manager->CreateInstanceByContractID(NS_SESSION_CONTRACTID,
|
---|
| 276 | nsnull,
|
---|
| 277 | NS_GET_IID(ISession),
|
---|
| 278 | getter_AddRefs(session));
|
---|
[4246] | 279 | if (NS_FAILED(rc))
|
---|
| 280 | {
|
---|
[50339] | 281 | printf("Error, could not instantiate session object! rc=%#x\n", rc);
|
---|
[4246] | 282 | return;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[33604] | 285 | rc = machine->LockMachine(session, LockType_Write);
|
---|
[4246] | 286 | if (NS_FAILED(rc))
|
---|
| 287 | {
|
---|
[50339] | 288 | printf("Error, could not lock the machine for the session! rc=%#x\n", rc);
|
---|
[4246] | 289 | return;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /*
|
---|
| 293 | * After the machine is registered, the initial machine object becomes
|
---|
| 294 | * immutable. In order to get a mutable machine object, we must query
|
---|
| 295 | * it from the opened session object.
|
---|
| 296 | */
|
---|
[47813] | 297 | rc = session->GetMachine(getter_AddRefs(sessionMachine));
|
---|
[4246] | 298 | if (NS_FAILED(rc))
|
---|
| 299 | {
|
---|
[50339] | 300 | printf("Error, could not get machine session! rc=%#x\n", rc);
|
---|
[4246] | 301 | return;
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | /*
|
---|
[1] | 306 | * Create a virtual harddisk
|
---|
| 307 | */
|
---|
[23223] | 308 | nsCOMPtr<IMedium> hardDisk = 0;
|
---|
[54438] | 309 | rc = virtualBox->CreateMedium(NS_LITERAL_STRING("VDI").get(),
|
---|
| 310 | NS_LITERAL_STRING("/tmp/TestHardDisk.vdi").get(),
|
---|
| 311 | AccessMode_ReadWrite, DeviceType_HardDisk,
|
---|
| 312 | getter_AddRefs(hardDisk));
|
---|
[4246] | 313 | if (NS_FAILED(rc))
|
---|
[1] | 314 | {
|
---|
[50339] | 315 | printf("Failed creating a hard disk object! rc=%#x\n", rc);
|
---|
[1] | 316 | }
|
---|
| 317 | else
|
---|
| 318 | {
|
---|
| 319 | /*
|
---|
| 320 | * We have only created an object so far. No on disk representation exists
|
---|
| 321 | * because none of its properties has been set so far. Let's continue creating
|
---|
| 322 | * a dynamically expanding image.
|
---|
| 323 | */
|
---|
[49452] | 324 | nsCOMPtr<IProgress> progress;
|
---|
[50339] | 325 | MediumVariant_T mediumVariants[] =
|
---|
| 326 | { MediumVariant_Standard };
|
---|
| 327 | rc = hardDisk->CreateBaseStorage(100 * 1024 * 1024, // size in bytes
|
---|
| 328 | sizeof(mediumVariants) / sizeof(mediumVariants[0]), mediumVariants,
|
---|
[17970] | 329 | getter_AddRefs(progress)); // optional progress object
|
---|
[4246] | 330 | if (NS_FAILED(rc))
|
---|
[1] | 331 | {
|
---|
[50339] | 332 | printf("Failed creating hard disk image! rc=%#x\n", rc);
|
---|
[1] | 333 | }
|
---|
| 334 | else
|
---|
| 335 | {
|
---|
| 336 | /*
|
---|
| 337 | * Creating the image is done in the background because it can take quite
|
---|
| 338 | * some time (at least fixed size images). We have to wait for its completion.
|
---|
| 339 | * Here we wait forever (timeout -1) which is potentially dangerous.
|
---|
| 340 | */
|
---|
| 341 | rc = progress->WaitForCompletion(-1);
|
---|
[20220] | 342 | PRInt32 resultCode;
|
---|
[1] | 343 | progress->GetResultCode(&resultCode);
|
---|
[4246] | 344 | if (NS_FAILED(rc) || NS_FAILED(resultCode))
|
---|
[1] | 345 | {
|
---|
[50339] | 346 | printf("Error: could not create hard disk! rc=%#x\n",
|
---|
[4246] | 347 | NS_FAILED(rc) ? rc : resultCode);
|
---|
[1] | 348 | }
|
---|
| 349 | else
|
---|
| 350 | {
|
---|
| 351 | /*
|
---|
[31615] | 352 | * Now that it's created, we can assign it to the VM.
|
---|
[1] | 353 | */
|
---|
[47813] | 354 | rc = sessionMachine->AttachDevice(
|
---|
| 355 | NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
|
---|
[23223] | 356 | 0, // channel number on the controller
|
---|
| 357 | 0, // device number on the controller
|
---|
| 358 | DeviceType_HardDisk,
|
---|
[31615] | 359 | hardDisk);
|
---|
[1] | 360 | if (NS_FAILED(rc))
|
---|
| 361 | {
|
---|
[50339] | 362 | printf("Error: could not attach hard disk! rc=%#x\n", rc);
|
---|
[1] | 363 | }
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /*
|
---|
| 369 | * It's got a hard disk but that one is new and thus not bootable. Make it
|
---|
| 370 | * boot from an ISO file. This requires some processing. First the ISO file
|
---|
| 371 | * has to be registered and then mounted to the VM's DVD drive and selected
|
---|
| 372 | * as the boot device.
|
---|
| 373 | */
|
---|
[23223] | 374 | nsCOMPtr<IMedium> dvdImage;
|
---|
[31568] | 375 | rc = virtualBox->OpenMedium(NS_LITERAL_STRING("/home/vbox/isos/winnt4ger.iso").get(),
|
---|
| 376 | DeviceType_DVD,
|
---|
| 377 | AccessMode_ReadOnly,
|
---|
[37525] | 378 | false /* fForceNewUuid */,
|
---|
[31568] | 379 | getter_AddRefs(dvdImage));
|
---|
[4246] | 380 | if (NS_FAILED(rc))
|
---|
[50339] | 381 | printf("Error: could not open CD image! rc=%#x\n", rc);
|
---|
[1] | 382 | else
|
---|
| 383 | {
|
---|
| 384 | /*
|
---|
[13580] | 385 | * Now assign it to our VM
|
---|
[1] | 386 | */
|
---|
[47813] | 387 | rc = sessionMachine->MountMedium(
|
---|
| 388 | NS_LITERAL_STRING("IDE Controller").get(), // controller identifier
|
---|
[23223] | 389 | 2, // channel number on the controller
|
---|
| 390 | 0, // device number on the controller
|
---|
[33140] | 391 | dvdImage,
|
---|
[24499] | 392 | PR_FALSE); // aForce
|
---|
[4246] | 393 | if (NS_FAILED(rc))
|
---|
[1] | 394 | {
|
---|
[50339] | 395 | printf("Error: could not mount ISO image! rc=%#x\n", rc);
|
---|
[1] | 396 | }
|
---|
| 397 | else
|
---|
| 398 | {
|
---|
| 399 | /*
|
---|
[13580] | 400 | * Last step: tell the VM to boot from the CD.
|
---|
[1] | 401 | */
|
---|
[49452] | 402 | rc = sessionMachine->SetBootOrder(1, DeviceType::DVD);
|
---|
[1] | 403 | if (NS_FAILED(rc))
|
---|
| 404 | {
|
---|
[50339] | 405 | printf("Could not set boot device! rc=%#x\n", rc);
|
---|
[1] | 406 | }
|
---|
| 407 | }
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 | /*
|
---|
[4246] | 411 | * Save all changes we've just made.
|
---|
[1] | 412 | */
|
---|
[47813] | 413 | rc = sessionMachine->SaveSettings();
|
---|
[4246] | 414 | if (NS_FAILED(rc))
|
---|
[50339] | 415 | printf("Could not save machine settings! rc=%#x\n", rc);
|
---|
[1] | 416 |
|
---|
[4246] | 417 | /*
|
---|
| 418 | * It is always important to close the open session when it becomes not
|
---|
| 419 | * necessary any more.
|
---|
| 420 | */
|
---|
[31070] | 421 | session->UnlockMachine();
|
---|
[47813] | 422 |
|
---|
[50339] | 423 | IMedium **aMedia;
|
---|
| 424 | PRUint32 cMedia;
|
---|
[47813] | 425 | rc = machine->Unregister((CleanupMode_T)CleanupMode_DetachAllReturnHardDisksOnly,
|
---|
[50339] | 426 | &cMedia, &aMedia);
|
---|
[47813] | 427 | if (NS_FAILED(rc))
|
---|
[50339] | 428 | printf("Unregistering the machine failed! rc=%#x\n", rc);
|
---|
[47813] | 429 | else
|
---|
| 430 | {
|
---|
[50339] | 431 | nsCOMPtr<IProgress> pProgress;
|
---|
| 432 | rc = machine->DeleteConfig(cMedia, aMedia, getter_AddRefs(pProgress));
|
---|
[47813] | 433 | if (NS_FAILED(rc))
|
---|
[50339] | 434 | printf("Deleting of machine failed! rc=%#x\n", rc);
|
---|
[47813] | 435 | else
|
---|
| 436 | {
|
---|
| 437 | rc = pProgress->WaitForCompletion(-1);
|
---|
| 438 | PRInt32 resultCode;
|
---|
| 439 | pProgress->GetResultCode(&resultCode);
|
---|
| 440 | if (NS_FAILED(rc) || NS_FAILED(resultCode))
|
---|
[50339] | 441 | printf("Failed to delete the machine! rc=%#x\n",
|
---|
| 442 | NS_FAILED(rc) ? rc : resultCode);
|
---|
[47813] | 443 | }
|
---|
[86617] | 444 |
|
---|
| 445 | /* Release the media array: */
|
---|
| 446 | for (PRUint32 i = 0; i < cMedia; i++)
|
---|
| 447 | if (aMedia[i])
|
---|
| 448 | aMedia[i]->Release();
|
---|
| 449 | nsMemory::Free(aMedia);
|
---|
[47813] | 450 | }
|
---|
[1] | 451 | }
|
---|
| 452 |
|
---|
| 453 | // main
|
---|
| 454 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 455 |
|
---|
[86402] | 456 | int main(int argc, char **argv)
|
---|
[1] | 457 | {
|
---|
| 458 | /*
|
---|
[4246] | 459 | * Check that PRUnichar is equal in size to what compiler composes L""
|
---|
| 460 | * strings from; otherwise NS_LITERAL_STRING macros won't work correctly
|
---|
| 461 | * and we will get a meaningless SIGSEGV. This, of course, must be checked
|
---|
| 462 | * at compile time in xpcom/string/nsTDependentString.h, but XPCOM lacks
|
---|
| 463 | * compile-time assert macros and I'm not going to add them now.
|
---|
[1] | 464 | */
|
---|
[4246] | 465 | if (sizeof(PRUnichar) != sizeof(wchar_t))
|
---|
[1] | 466 | {
|
---|
[50339] | 467 | printf("Error: sizeof(PRUnichar) {%lu} != sizeof(wchar_t) {%lu}!\n"
|
---|
[4246] | 468 | "Probably, you forgot the -fshort-wchar compiler option.\n",
|
---|
[13386] | 469 | (unsigned long) sizeof(PRUnichar),
|
---|
| 470 | (unsigned long) sizeof(wchar_t));
|
---|
[1] | 471 | return -1;
|
---|
| 472 | }
|
---|
| 473 |
|
---|
[86402] | 474 | #if 1 /* Please ignore this! It is very very crude. */
|
---|
[86616] | 475 | # ifdef RTPATH_APP_PRIVATE_ARCH
|
---|
| 476 | if (!getenv("VBOX_XPCOM_HOME"))
|
---|
| 477 | setenv("VBOX_XPCOM_HOME", RTPATH_APP_PRIVATE_ARCH, 1);
|
---|
| 478 | # else
|
---|
[86402] | 479 | char szTmp[8192];
|
---|
| 480 | if (!getenv("VBOX_XPCOM_HOME"))
|
---|
| 481 | {
|
---|
| 482 | strcpy(szTmp, argv[0]);
|
---|
| 483 | *strrchr(szTmp, '/') = '\0';
|
---|
| 484 | strcat(szTmp, "/..");
|
---|
| 485 | fprintf(stderr, "tstVBoxAPIXPCOM: VBOX_XPCOM_HOME is not set, using '%s' instead\n", szTmp);
|
---|
| 486 | setenv("VBOX_XPCOM_HOME", szTmp, 1);
|
---|
| 487 | }
|
---|
[86616] | 488 | # endif
|
---|
[86402] | 489 | #endif
|
---|
| 490 | (void)argc; (void)argv;
|
---|
| 491 |
|
---|
[4246] | 492 | nsresult rc;
|
---|
[1] | 493 |
|
---|
[4246] | 494 | /*
|
---|
| 495 | * This is the standard XPCOM init procedure.
|
---|
| 496 | * What we do is just follow the required steps to get an instance
|
---|
| 497 | * of our main interface, which is IVirtualBox.
|
---|
[48164] | 498 | *
|
---|
[4246] | 499 | * Note that we scope all nsCOMPtr variables in order to have all XPCOM
|
---|
| 500 | * objects automatically released before we call NS_ShutdownXPCOM at the
|
---|
| 501 | * end. This is an XPCOM requirement.
|
---|
| 502 | */
|
---|
[1] | 503 | {
|
---|
[4246] | 504 | nsCOMPtr<nsIServiceManager> serviceManager;
|
---|
| 505 | rc = NS_InitXPCOM2(getter_AddRefs(serviceManager), nsnull, nsnull);
|
---|
| 506 | if (NS_FAILED(rc))
|
---|
| 507 | {
|
---|
[50339] | 508 | printf("Error: XPCOM could not be initialized! rc=%#x\n", rc);
|
---|
[4246] | 509 | return -1;
|
---|
| 510 | }
|
---|
[1] | 511 |
|
---|
[4246] | 512 | #if 0
|
---|
| 513 | /*
|
---|
| 514 | * Register our components. This step is only necessary if this executable
|
---|
| 515 | * implements XPCOM components itself which is not the case for this
|
---|
| 516 | * simple example.
|
---|
| 517 | */
|
---|
| 518 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(serviceManager);
|
---|
| 519 | if (!registrar)
|
---|
| 520 | {
|
---|
[50339] | 521 | printf("Error: could not query nsIComponentRegistrar interface!\n");
|
---|
[4246] | 522 | return -1;
|
---|
| 523 | }
|
---|
| 524 | registrar->AutoRegister(nsnull);
|
---|
| 525 | #endif
|
---|
[1] | 526 |
|
---|
[4246] | 527 | /*
|
---|
| 528 | * Make sure the main event queue is created. This event queue is
|
---|
| 529 | * responsible for dispatching incoming XPCOM IPC messages. The main
|
---|
| 530 | * thread should run this event queue's loop during lengthy non-XPCOM
|
---|
| 531 | * operations to ensure messages from the VirtualBox server and other
|
---|
| 532 | * XPCOM IPC clients are processed. This use case doesn't perform such
|
---|
| 533 | * operations so it doesn't run the event loop.
|
---|
| 534 | */
|
---|
| 535 | nsCOMPtr<nsIEventQueue> eventQ;
|
---|
[49452] | 536 | rc = NS_GetMainEventQ(getter_AddRefs(eventQ));
|
---|
[4246] | 537 | if (NS_FAILED(rc))
|
---|
| 538 | {
|
---|
[50339] | 539 | printf("Error: could not get main event queue! rc=%#x\n", rc);
|
---|
[4246] | 540 | return -1;
|
---|
| 541 | }
|
---|
[1] | 542 |
|
---|
[4246] | 543 | /*
|
---|
| 544 | * Now XPCOM is ready and we can start to do real work.
|
---|
| 545 | * IVirtualBox is the root interface of VirtualBox and will be
|
---|
| 546 | * retrieved from the XPCOM component manager. We use the
|
---|
| 547 | * XPCOM provided smart pointer nsCOMPtr for all objects because
|
---|
| 548 | * that's very convenient and removes the need deal with reference
|
---|
| 549 | * counting and freeing.
|
---|
| 550 | */
|
---|
| 551 | nsCOMPtr<nsIComponentManager> manager;
|
---|
[49452] | 552 | rc = NS_GetComponentManager(getter_AddRefs(manager));
|
---|
[4246] | 553 | if (NS_FAILED(rc))
|
---|
| 554 | {
|
---|
[50339] | 555 | printf("Error: could not get component manager! rc=%#x\n", rc);
|
---|
[4246] | 556 | return -1;
|
---|
| 557 | }
|
---|
[1] | 558 |
|
---|
[4246] | 559 | nsCOMPtr<IVirtualBox> virtualBox;
|
---|
[49452] | 560 | rc = manager->CreateInstanceByContractID(NS_VIRTUALBOX_CONTRACTID,
|
---|
| 561 | nsnull,
|
---|
| 562 | NS_GET_IID(IVirtualBox),
|
---|
| 563 | getter_AddRefs(virtualBox));
|
---|
[4246] | 564 | if (NS_FAILED(rc))
|
---|
| 565 | {
|
---|
[50339] | 566 | printf("Error, could not instantiate VirtualBox object! rc=%#x\n", rc);
|
---|
[4246] | 567 | return -1;
|
---|
| 568 | }
|
---|
[50339] | 569 | printf("VirtualBox object created\n");
|
---|
[1] | 570 |
|
---|
[4246] | 571 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 572 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 573 | ////////////////////////////////////////////////////////////////////////////////
|
---|
[1] | 574 |
|
---|
| 575 |
|
---|
[4246] | 576 | listVMs(virtualBox);
|
---|
[1] | 577 |
|
---|
[4246] | 578 | createVM(virtualBox);
|
---|
[1] | 579 |
|
---|
| 580 |
|
---|
[4246] | 581 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 582 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 583 | ////////////////////////////////////////////////////////////////////////////////
|
---|
[1] | 584 |
|
---|
[4246] | 585 | /* this is enough to free the IVirtualBox instance -- smart pointers rule! */
|
---|
| 586 | virtualBox = nsnull;
|
---|
[1] | 587 |
|
---|
[4246] | 588 | /*
|
---|
| 589 | * Process events that might have queued up in the XPCOM event
|
---|
| 590 | * queue. If we don't process them, the server might hang.
|
---|
| 591 | */
|
---|
| 592 | eventQ->ProcessPendingEvents();
|
---|
| 593 | }
|
---|
[1] | 594 |
|
---|
| 595 | /*
|
---|
[4246] | 596 | * Perform the standard XPCOM shutdown procedure.
|
---|
[1] | 597 | */
|
---|
| 598 | NS_ShutdownXPCOM(nsnull);
|
---|
[50339] | 599 | printf("Done!\n");
|
---|
[1] | 600 | return 0;
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 |
|
---|
| 604 | //////////////////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 605 | //// Helpers
|
---|
| 606 | //////////////////////////////////////////////////////////////////////////////////////////////////////
|
---|
| 607 |
|
---|
| 608 | /**
|
---|
| 609 | * Helper function to convert an nsID into a human readable string
|
---|
| 610 | *
|
---|
| 611 | * @returns result string, allocated. Has to be freed using free()
|
---|
| 612 | * @param guid Pointer to nsID that will be converted.
|
---|
| 613 | */
|
---|
| 614 | char *nsIDToString(nsID *guid)
|
---|
| 615 | {
|
---|
[103356] | 616 | char *res = (char *)malloc(39);
|
---|
[1] | 617 | if (res != NULL)
|
---|
| 618 | {
|
---|
[50339] | 619 | snprintf(res, 39, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
|
---|
[1] | 620 | guid->m0, (PRUint32)guid->m1, (PRUint32)guid->m2,
|
---|
| 621 | (PRUint32)guid->m3[0], (PRUint32)guid->m3[1], (PRUint32)guid->m3[2],
|
---|
| 622 | (PRUint32)guid->m3[3], (PRUint32)guid->m3[4], (PRUint32)guid->m3[5],
|
---|
| 623 | (PRUint32)guid->m3[6], (PRUint32)guid->m3[7]);
|
---|
| 624 | }
|
---|
| 625 | return res;
|
---|
| 626 | }
|
---|
[4246] | 627 |
|
---|
| 628 | /**
|
---|
| 629 | * Helper function to print XPCOM exception information set on the current
|
---|
| 630 | * thread after a failed XPCOM method call. This function will also print
|
---|
| 631 | * extended VirtualBox error info if it is available.
|
---|
| 632 | */
|
---|
| 633 | void printErrorInfo()
|
---|
| 634 | {
|
---|
[103356] | 635 | nsresult rc = NS_ERROR_UNEXPECTED;
|
---|
[49452] | 636 | nsCOMPtr<nsIExceptionService> es;
|
---|
| 637 | es = do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
|
---|
[21878] | 638 | if (NS_SUCCEEDED(rc))
|
---|
[4246] | 639 | {
|
---|
[49452] | 640 | nsCOMPtr<nsIExceptionManager> em;
|
---|
| 641 | rc = es->GetCurrentExceptionManager(getter_AddRefs(em));
|
---|
[21878] | 642 | if (NS_SUCCEEDED(rc))
|
---|
[4246] | 643 | {
|
---|
| 644 | nsCOMPtr<nsIException> ex;
|
---|
[49452] | 645 | rc = em->GetCurrentException(getter_AddRefs(ex));
|
---|
[21878] | 646 | if (NS_SUCCEEDED(rc) && ex)
|
---|
[4246] | 647 | {
|
---|
[49452] | 648 | nsCOMPtr<IVirtualBoxErrorInfo> info;
|
---|
[4246] | 649 | info = do_QueryInterface(ex, &rc);
|
---|
| 650 | if (NS_SUCCEEDED(rc) && info)
|
---|
| 651 | {
|
---|
| 652 | /* got extended error info */
|
---|
[50339] | 653 | printf("Extended error info (IVirtualBoxErrorInfo):\n");
|
---|
[20267] | 654 | PRInt32 resultCode = NS_OK;
|
---|
[49452] | 655 | info->GetResultCode(&resultCode);
|
---|
[50339] | 656 | printf(" resultCode=%08X\n", resultCode);
|
---|
[4246] | 657 | nsXPIDLString component;
|
---|
[49452] | 658 | info->GetComponent(getter_Copies(component));
|
---|
[50339] | 659 | printf(" component=%s\n", NS_ConvertUTF16toUTF8(component).get());
|
---|
[4246] | 660 | nsXPIDLString text;
|
---|
[49452] | 661 | info->GetText(getter_Copies(text));
|
---|
[50339] | 662 | printf(" text=%s\n", NS_ConvertUTF16toUTF8(text).get());
|
---|
[4246] | 663 | }
|
---|
| 664 | else
|
---|
| 665 | {
|
---|
| 666 | /* got basic error info */
|
---|
[50339] | 667 | printf("Basic error info (nsIException):\n");
|
---|
[4246] | 668 | nsresult resultCode = NS_OK;
|
---|
[49452] | 669 | ex->GetResult(&resultCode);
|
---|
[50339] | 670 | printf(" resultCode=%08X\n", resultCode);
|
---|
[4246] | 671 | nsXPIDLCString message;
|
---|
[49452] | 672 | ex->GetMessage(getter_Copies(message));
|
---|
[50339] | 673 | printf(" message=%s\n", message.get());
|
---|
[4246] | 674 | }
|
---|
| 675 |
|
---|
| 676 | /* reset the exception to NULL to indicate we've processed it */
|
---|
[49452] | 677 | em->SetCurrentException(NULL);
|
---|
[4246] | 678 |
|
---|
| 679 | rc = NS_OK;
|
---|
| 680 | }
|
---|
| 681 | }
|
---|
| 682 | }
|
---|
| 683 | }
|
---|