tstVBoxAPIWin.cpp revision 3d9b2bf0cd2b26ada853ec1b9df753d352679a44
/* $Id$ */
/** @file
*
* tstVBoxAPIWin - sample program to illustrate the VirtualBox
* COM API for machine management on Windows.
It only uses standard C/C++ and COM semantics,
* make things even easier to follow, only the
* standard Win32 API has been used. Typically,
* C++ developers would make use of Microsoft's
* ATL to ease development.
*/
/*
* Copyright (C) 2006-2014 Oracle Corporation
*
* 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 (GPL) 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.
*/
/*
* PURPOSE OF THIS SAMPLE PROGRAM
* ------------------------------
*
* This sample program is intended to demonstrate the minimal code necessary
* to use VirtualBox COM API for learning puroses only. The program uses pure
* Win32 API and doesn't have any extra dependencies to let you better
* understand what is going on when a client talks to the VirtualBox core
* using the COM framework.
*
* However, if you want to write a real application, it is highly recommended
* to use our MS COM XPCOM Glue library and helper C++ classes. This way, you
* will get at least the following benefits:
*
* a) better portability: both the MS COM (used on Windows) and XPCOM (used
* everywhere else) VirtualBox client application from the same source code
* (including common smart C++ templates for automatic interface pointer
* reference counter and string data management);
* b) simpler XPCOM initialization and shutdown (only a single method call
* that does everything right).
*
* Currently, there is no separate sample program that uses the VirtualBox MS
* COM XPCOM Glue library. Please refer to the sources of stock VirtualBox
* applications such as the VirtualBox GUI frontend or the VBoxManage command
* line frontend.
*/
#include <stdio.h>
#include "VirtualBox.h"
#define SAFE_RELEASE(x) \
if (x) { \
x->Release(); \
x = NULL; \
}
{
/*
* First we have to get a list of all registered VMs
*/
{
{
{
{
}
}
}
}
return 0;
}
{
/* Try to find a machine that doesn't exist */
{
else
{
else
{
}
}
}
return 0;
}
{
/* Try to start a VM called "WinXP SP2". */
{
else
{
else
{
}
}
}
else
{
do
{
{
break;
}
/* Create the session object. */
NULL, /* no aggregation */
CLSCTX_INPROC_SERVER, /* the object lives in a server process on this machine */
IID_ISession, /* IID of the interface */
(void**)&session);
{
break;
}
/* Start a VM session using the delivered VBox GUI. */
{
break;
}
/* Wait until VM is running. */
printf("Starting VM, please wait ...\n");
/* Get console object. */
/* Bring console window to front. */
machine->ShowConsoleWindow(0);
printf("Press enter to power off VM and close the session...\n");
getchar();
/* Power down the machine. */
/* Wait until VM is powered down. */
printf("Powering off VM, please wait ...\n");
/* Close the session. */
} while (0);
}
return 0;
}
{
do
{
/* Initialize the COM subsystem. */
/* Instantiate the VirtualBox root object. */
NULL, /* no aggregation */
CLSCTX_LOCAL_SERVER, /* the object lives in a server process on this machine */
IID_IVirtualBox, /* IID of the interface */
(void**)&virtualBox);
{
break;
}
/* Enable the following line to get a VM started. */
//testStartVM(virtualBox);
/* Release the VirtualBox object. */
virtualBox->Release();
} while (0);
return 0;
}