tstCAPIGlue.c revision 681bb0bf6e9ab5a590d8afcdf37634e2fc29f595
/* $Revision$ */
/** @file tstCAPIGlue.c
* Demonstrator program to illustrate use of C bindings of Main API.
*
* It has sample code showing how to retrieve all available error information,
* and how to handle active (event delivery through callbacks) or passive
* (event delivery through a polling mechanism) event listeners.
*/
/*
* Copyright (C) 2009-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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "VBoxCAPIGlue.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifndef WIN32
# include <signal.h>
# include <unistd.h>
#endif
/**
* Select between active event listener (defined) and passive event listener
* (undefined). The active event listener case needs much more code, and
* additionally requires a lot more platform dependent code.
*/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Set by Ctrl+C handler. */
static volatile int g_fStop = 0;
#ifdef USE_ACTIVE_EVENT_LISTENER
# ifdef WIN32
/** The COM type information for IEventListener, for implementing IDispatch. */
# endif /* WIN32 */
#endif /* USE_ACTIVE_EVENT_LISTENER */
{
switch (machineState)
{
case MachineState_Null: return "<null>";
case MachineState_PoweredOff: return "PoweredOff";
case MachineState_Saved: return "Saved";
case MachineState_Teleported: return "Teleported";
case MachineState_Aborted: return "Aborted";
case MachineState_Running: return "Running";
case MachineState_Paused: return "Paused";
case MachineState_Stuck: return "Stuck";
case MachineState_Teleporting: return "Teleporting";
case MachineState_LiveSnapshotting: return "LiveSnapshotting";
case MachineState_Starting: return "Starting";
case MachineState_Stopping: return "Stopping";
case MachineState_Saving: return "Saving";
case MachineState_Restoring: return "Restoring";
case MachineState_TeleportingPausedVM: return "TeleportingPausedVM";
case MachineState_TeleportingIn: return "TeleportingIn";
case MachineState_FaultTolerantSyncing: return "FaultTolerantSyncing";
case MachineState_DeletingSnapshotOnline: return "DeletingSnapshotOnline";
case MachineState_DeletingSnapshotPaused: return "DeletingSnapshotPaused";
case MachineState_RestoringSnapshot: return "RestoringSnapshot";
case MachineState_DeletingSnapshot: return "DeletingSnapshot";
case MachineState_SettingUp: return "SettingUp";
default: return "no idea";
}
}
/**
* Ctrl+C handler, terminate event listener.
*
* Remember that most function calls are not allowed in this context (including
* printf!), so make sure that this does as little as possible.
*
* @param iInfo Platform dependent detail info (ignored).
*/
{
(void)iInfo;
g_fStop = 1;
return TRUE;
}
/**
* Sample event processing function, dumping some event information.
* Shared between active and passive event demo, to highlight that this part
* is identical between the two.
*/
{
if (!event)
{
printf("event null\n");
return S_OK;
}
{
return S_OK;
}
switch (evType)
{
printf("OnMousePointerShapeChanged\n");
break;
printf("OnMouseCapabilityChanged\n");
break;
printf("OnMouseCapabilityChanged\n");
break;
{
enum MachineState state;
{
return S_OK;
}
if (!ev)
{
printf("StateChangedEvent reference null\n");
return S_OK;
}
if ( state == MachineState_PoweredOff
|| state == MachineState_Saved
|| state == MachineState_Teleported
|| state == MachineState_Aborted
)
g_fStop = 1;
break;
}
printf("OnAdditionsStateChanged\n");
break;
printf("OnNetworkAdapterChanged\n");
break;
printf("OnSerialPortChanged\n");
break;
printf("OnParallelPortChanged\n");
break;
printf("OnStorageControllerChanged\n");
break;
printf("OnMediumChanged\n");
break;
printf("OnVRDEServerChanged\n");
break;
printf("OnUSBControllerChanged\n");
break;
printf("OnUSBDeviceStateChanged\n");
break;
printf("OnSharedFolderChanged\n");
break;
printf("OnRuntimeError\n");
break;
printf("OnCanShowWindow\n");
break;
printf("OnShowWindow\n");
break;
default:
}
return S_OK;
}
#ifdef USE_ACTIVE_EVENT_LISTENER
struct IEventListenerDemo;
typedef struct IEventListenerDemo IEventListenerDemo;
typedef struct IEventListenerDemoVtbl
{
#ifdef WIN32
HRESULT (*GetIDsOfNames)(IEventListenerDemo *pThis, REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId);
HRESULT (*Invoke)(IEventListenerDemo *pThis, DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
#endif
typedef struct IEventListenerDemo
{
struct IEventListenerDemoVtbl *lpVtbl;
int cRef;
#ifdef WIN32
/* Active event delivery needs a free threaded marshaler, as the default
* proxy marshaling cannot deal correctly with this case. */
#endif
/* Defines for easily calling IEventListenerDemo functions. */
/* IUnknown functions. */
#define IEventListenerDemo_AddRef(This) \
#define IEventListenerDemo_Release(This) \
#ifdef WIN32
/* IDispatch functions. */
#define IEventListenerDemo_Invoke(This,dispIdMember,riid,lcid,wFlags,pDispParams,pVarResult,pExcepInfo,puArgErr) \
( (This)->lpVtbl->Invoke(This,dispIdMember,riid,lcid,wFlags,pDispParams,pVarResult,pExcepInfo,puArgErr) )
#endif
/* IEventListener functions. */
/**
* Event handler function, for active event processing.
*/
{
return EventListenerDemoProcessEvent(event);
}
static HRESULT IEventListenerDemoImpl_QueryInterface(IEventListenerDemo *pThis, const IID *iid, void **resultp)
{
/* match iid */
{
return S_OK;
}
#ifdef WIN32
#endif
return E_NOINTERFACE;
}
{
}
{
HRESULT c;
if (!c)
return c;
}
#ifdef WIN32
{
if (!pctinfo)
return E_POINTER;
*pctinfo = 1;
return S_OK;
}
static HRESULT IEventListenerDemoImpl_GetTypeInfo(IEventListenerDemo *pThis, UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
{
if (!ppTInfo)
return E_POINTER;
return S_OK;
}
static HRESULT IEventListenerDemoImpl_GetIDsOfNames(IEventListenerDemo *pThis, REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
}
static HRESULT IEventListenerDemoImpl_Invoke(IEventListenerDemo *pThis, DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
return ITypeInfo_Invoke(g_pTInfoIEventListener, (IDispatch *)pThis, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
}
{
return rc;
/* No longer need access to the type lib, release it. */
return rc;
}
#endif
#ifdef __GNUC__
typedef struct IEventListenerDemoVtblInt
{
void *typeinfo;
{
0, /* offset_to_top */
NULL, /* typeinfo, not vital */
{
#ifdef WIN32
#endif
}
};
typedef struct IEventListenerDemoVtblInt
{
{
{
#ifdef WIN32
#endif
}
};
#else
#endif
/**
* Register active event listener for the selected VM.
*
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
* @param id identifies the machine to start
*/
{
{
{
static const ULONG interestingEvents[] =
{
};
/* The VirtualBox API expects enum values as VT_I4, which in the
* future can be hopefully relaxed. */
interestingEventsSA = g_pVBoxFuncs->pfnSafeArrayCreateVector(VT_I4, 0, sizeof(interestingEvents) / sizeof(interestingEvents[0]));
g_pVBoxFuncs->pfnSafeArrayCopyInParamHelper(interestingEventsSA, &interestingEvents, sizeof(interestingEvents));
if (consoleListener)
{
#ifdef WIN32
#endif
1 /* active */);
{
/* Just wait here for events, no easy way to do this better
* as there's not much to do after this completes. */
printf("Entering event loop, PowerOff the machine to exit or press Ctrl-C to terminate\n");
#ifdef WIN32
#else
#endif
while (!g_fStop)
{
}
#ifdef WIN32
#else
#endif
}
else
{
printf("Failed to register event listener.\n");
}
#ifdef WIN32
if (consoleListener->pUnkMarshaler)
#endif
}
else
{
printf("Failed while allocating memory for console event listener.\n");
}
}
else
{
printf("Failed to get the event source instance.\n");
}
}
}
#else /* !USE_ACTIVE_EVENT_LISTENER */
/**
* Register passive event listener for the selected VM.
*
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
* @param id identifies the machine to start
*/
static void registerPassiveEventListener(IVirtualBox *virtualBox, ISession *session, BSTR machineId)
{
{
{
static const ULONG interestingEvents[] =
{
};
/* The VirtualBox API expects enum values as VT_I4, which in the
* future can be hopefully relaxed. */
interestingEventsSA = g_pVBoxFuncs->pfnSafeArrayCreateVector(VT_I4, 0, sizeof(interestingEvents) / sizeof(interestingEvents[0]));
g_pVBoxFuncs->pfnSafeArrayCopyInParamHelper(interestingEventsSA, &interestingEvents, sizeof(interestingEvents));
{
0 /* passive */);
{
/* Just wait here for events, no easy way to do this better
* as there's not much to do after this completes. */
printf("Entering event loop, PowerOff the machine to exit or press Ctrl-C to terminate\n");
#ifdef WIN32
#else
#endif
while (!g_fStop)
{
{
g_fStop = 1;
continue;
}
/* handle timeouts, resulting in NULL events */
if (!ev)
continue;
{
g_fStop = 1;
/* finish processing the event */
}
{
g_fStop = 1;
/* continue with event release */
}
if (ev)
{
}
}
#ifdef WIN32
#else
#endif
}
else
{
printf("Failed to register event listener.\n");
}
}
else
{
printf("Failed to create an event listener instance.\n");
}
}
else
{
printf("Failed to get the event source instance.\n");
}
}
}
#endif /* !USE_ACTIVE_EVENT_LISTENER */
/**
* Print detailed error information if available.
* @param pszExecutable string with the executable name
* @param pszErrorMsg string containing the code location specific error message
*/
{
IErrorInfo *ex;
{
if (ei)
{
/* got extended error info, maybe multiple infos */
do
{
}
while (ei);
}
}
}
/**
* Start a VM.
*
* @param argv0 executable name
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
* @param id identifies the machine to start
*/
{
{
return;
}
{
printf("Waiting for the remote session to open...\n");
if (FAILED(resultCode))
{
char *text;
}
else
{
/* Kick off the event listener demo part, which is quite separate.
* Ignore it if you need a more basic sample. */
#ifdef USE_ACTIVE_EVENT_LISTENER
#else /* !USE_ACTIVE_EVENT_LISTENER */
#endif /* !USE_ACTIVE_EVENT_LISTENER */
}
}
else
/* It's important to always release resources. */
}
/**
* List the registered VMs.
*
* @param argv0 executable name
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
*/
{
ULONG machineCnt = 0;
ULONG i;
unsigned start_id;
/*
* Get the list of all registered VMs.
*/
{
return;
}
/*
* Extract interface pointers from machinesSA, and update the reference
* counter of each object, as destroying machinesSA would call Release.
*/
g_pVBoxFuncs->pfnSafeArrayCopyOutIfaceParamHelper((IUnknown ***)&machines, &machineCnt, machinesSA);
if (!machineCnt)
{
printf("\tNo VMs\n");
return;
}
printf("VM List:\n\n");
/*
* Iterate through the collection.
*/
for (i = 0; i < machineCnt; ++i)
{
printf("\tMachine #%u\n", (unsigned)i);
if (!machine)
{
printf("\t(skipped, NULL)\n");
continue;
}
if (isAccessible)
{
char *machineName;
}
else
{
printf("\tName: <inaccessible>\n");
}
{
char *uuidUtf8;
}
if (isAccessible)
{
{
char *configFileUtf8;
}
{
}
{
char *osName;
}
}
}
/*
* Let the user chose a machine to start.
*/
printf("Type Machine# to start (0 - %u) or 'quit' to do nothing: ",
(unsigned)(machineCnt - 1));
{
if (machine)
{
}
}
/*
* Don't forget to release the objects in the array.
*/
for (i = 0; i < machineCnt; ++i)
{
if (machine)
{
}
}
if (machines)
}
/* Main - Start the ball rolling. */
{
printf("Starting main()\n");
if (VBoxCGlueInit() != 0)
{
argv[0], g_szVBoxErrMsg);
return EXIT_FAILURE;
}
{
}
if (!vboxclient)
{
return EXIT_FAILURE;
}
printf("----------------------------------------------------\n");
{
return EXIT_FAILURE;
}
{
return EXIT_FAILURE;
}
#ifdef USE_ACTIVE_EVENT_LISTENER
# ifdef WIN32
{
return EXIT_FAILURE;
}
# endif /* WIN32 */
#endif /* USE_ACTIVE_EVENT_LISTENER */
/*
* Now ask for revision, version and home folder information of
* this vbox. Were not using fancy macros here so it
* remains easy to see how we access C++'s vtable.
*/
/* 1. Revision */
else
/* 2. Version */
{
}
else
/* 3. Home Folder */
{
char *homefolder = NULL;
}
else
printf("----------------------------------------------------\n");
/*
* Do as mom told us: always clean up after yourself.
*/
#ifdef USE_ACTIVE_EVENT_LISTENER
# ifdef WIN32
{
}
# endif /* WIN32 */
#endif /* USE_ACTIVE_EVENT_LISTENER */
if (session)
{
}
if (vbox)
{
}
if (vboxclient)
{
vboxclient = NULL;
}
printf("Finished main()\n");
return 0;
}
/* vim: set ts=4 sw=4 et: */