tstVBoxAPILinux.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/** @file
*
* tstVBoxAPILinux - sample program to illustrate the VirtualBox
* XPCOM API for machine management on Linux.
* It only uses standard C/C++ and XPCOM semantics,
*
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <errno.h>
/*
* Include the XPCOM headers
*/
#include <nsIServiceManager.h>
#include <nsIComponentRegistrar.h>
#include <nsXPCOMGlue.h>
#include <nsMemory.h>
#include <nsString.h>
#include <nsIProgrammingLanguage.h>
#include <ipcIService.h>
#include <ipcCID.h>
#include <ipcIDConnectService.h>
#include <nsIEventQueueService.h>
/*
* Some XPCOM declarations that haven't made it
* into the official headers yet
*/
#define IPC_DCONNECTSERVICE_CONTRACTID \
/*
* VirtualBox XPCOM interface. This header is generated
* from IDL which in turn is generated from a custom XML format.
*/
#include "VirtualBox_XPCOM.h"
/*
* Prototypes
*/
/**
* Display all registered VMs on the screen with some information about each
*
* @param virtualBox VirtualBox instance object.
*/
{
printf("----------------------------------------------------\n");
printf("VM List:\n\n");
/*
* Get the list of all registered VMs
*/
if (NS_SUCCEEDED(rc))
if (NS_SUCCEEDED(rc))
{
/*
* Iterate through the collection
*/
{
{
free((void*)uuidString);
}
}
}
printf("----------------------------------------------------\n\n");
/* don't forget to release the objects... */
if (enumerator)
enumerator->Release();
if (collection)
collection->Release();
}
/**
* Create a sample VM
*
* @param virtualBox VirtualBox instance object.
*/
{
/*
* First create a new VM. It will be unconfigured and not be saved
* in the configuration until we explicitely choose to do so.
*/
/*
* Set some properties
*/
/* alternative to illustrate the use of string classes */
char vmName[] = "A new name";
/*
* Now a more advanced property -- the guest OS type. This is
* an object by itself which has to be found first. Note that we
* use the ID of the guest OS type here which is an internal
* representation (you can find that by configuring the OS type of
* a machine in the GUI and then looking at the <Guest ostype=""/>
* setting in the XML file. It is also possible to get the OS type from
* its description (win2k would be "Windows 2000") by getting the
* guest OS type collection and enumerating it.
*/
char win2k[] = "win2k";
{
}
else
{
}
/*
* Create a virtual harddisk
*/
IVirtualDiskImage *vdi = 0;
char vdiName[] = "TestHardDisk.vdi";
&hardDisk);
if (NS_SUCCEEDED (rc))
{
if (NS_SUCCEEDED (rc))
}
{
printf("Failed creating a hard disk object!\n");
}
else
{
/*
* We have only created an object so far. No on disk representation exists
* because none of its properties has been set so far. Let's continue creating
* a dynamically expanding image.
*/
&progress); // optional progress object
{
printf("Failed creating hard disk image!\n");
}
else
{
/*
* Creating the image is done in the background because it can take quite
* some time (at least fixed size images). We have to wait for its completion.
* Here we wait forever (timeout -1) which is potentially dangerous.
*/
{
printf("Error: could not create hard disk!\n");
}
else
{
/*
* Now we have to register the new hard disk with VirtualBox.
*/
{
printf("Error: could not register hard disk!\n");
}
else
{
/*
* Now that it's registered, we can assign it to the VM. This is done
* by UUID, so query that one fist. The UUID has been assigned automatically
* when we've created the image.
*/
0); // device number on the controller
{
printf("Error: could not attach hard disk!\n");
}
}
}
}
}
if (vdi)
if (hardDisk)
/*
* It's got a hard disk but that one is new and thus not bootable. Make it
* boot from an ISO file. This requires some processing. First the ISO file
* has to be registered and then mounted to the VM's DVD drive and selected
* as the boot device.
*/
char isoFile[] = "/home/achimha/isoimages/winnt4ger.iso";
uuid, /* NULL UUID, i.e. a new one will be created */
&dvdImage);
{
printf("Error: could not open CD image!\n");
}
else
{
/*
* Register it with VBox
*/
{
printf("Error: could not register CD image!\n");
}
else
{
/*
* Now assign it to our VM
*/
{
printf("Error: could not mount ISO image!\n");
}
else
{
/*
* Last step: tell the VM to boot from the CD.
*/
{
printf("Could not set boot device!\n");
}
}
}
}
/*
* Register the VM. Note that this call also saves the VM config
* to disk. It is also possible to save the VM settings but not
* register the VM.
*/
}
// main
///////////////////////////////////////////////////////////////////////////////
{
/*
* This is the standard XPCOM init processing.
* What we do is just follow the required steps to get an instance
* of our main interface, which is IVirtualBox
*/
{
return -1;
}
// register our component
if (!registrar)
{
printf("Error: could not query nsIComponentRegistrar interface!\n");
return -1;
}
// Create the Event Queue for this thread
{
return -1;
}
{
return -1;
}
{
return -1;
}
// get ipc service
{
return -1;
}
// get dconnect service
{
return -1;
}
{
return -1;
}
if (!manager)
{
printf("Error: could not query nsIComponentManager interface!\n");
return -1;
}
/*
* Now XPCOM is ready and we can start to do real work.
* IVirtualBox is the root interface of VirtualBox and will be
* retrieved from the XPCOM component registrar. We use the
* XPCOM provided smart pointer nsCOMPtr for all objects because
* that's very convenient and removes the need deal with reference
* counting and freeing.
*/
{
return -1;
}
printf("VirtualBox object created\n");
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/* this is enough to free the IVirtualBox instance -- smart pointers rule! */
virtualBox = nsnull;
/*
* Process events that might have queued up in the XPCOM event
* queue. If we don't process them, the server might hang.
*/
/*
* Perform the standard XPCOM shutdown procedure
*/
printf("Done!\n");
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//// Helpers
//////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Helper function to convert an nsID into a human readable string
*
* @returns result string, allocated. Has to be freed using free()
* @param guid Pointer to nsID that will be converted.
*/
{
{
}
return res;
}