tstdlOpen.c revision 0a29840869615968f7abf77fb83908f9e660db31
/* $Revsion: $ */
/** @file tstdlOpen.c
* Demonstrator program to illustrate use of C bindings of Main API
* using dynamic linking (dlopen and friends).
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "cbinding.h"
void (*VBoxComUninitializePtr)(void);
void (*VBoxComUnallocMemPtr)(void *ptr);
void (*VBoxUtf8FreePtr)(char *pszString);
const char * (*VBoxGetEnvPtr)(const char *pszVar);
/**
* 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.
*/
{
/* Avoid magic number 39. Yes, sizeof "literal" includes the NUL byte. */
{
}
return res;
}
/**
* List the registered VMs.
*
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
*/
{
PRUint32 machineCnt = 0;
PRUint32 i;
unsigned start_id;
/*
* Get the list of all registered VMs.
*/
{
(unsigned)rc);
return;
}
if (machineCnt == 0)
{
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 *uuidString;
}
if (isAccessible)
{
{
}
{
}
{
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)
{
}
}
}
/**
* Start a VM.
*
* @param virtualBox ptr to IVirtualBox object
* @param session ptr to ISession object
* @param id identifies the machine to start
*/
{
{
return;
}
id,
env,
);
{
}
else
{
printf("Waiting for the remote session to open...\n");
{
}
if (NS_FAILED(resultCode))
{
char *text;
}
else
{
}
}
/* It's important to always release resources. */
}
#ifdef USE_DYNAMIC_GLUE
/** The dlopen handle for VBoxXPCOM. */
void *g_hVBoxXPCOMC = NULL;
/** The last load error. */
char g_szVBoxXPCOMErrMsg[256];
# define SYM_PREFIX ""
# define DYNLIB_NAME "VBoxXPCOM.so"
# define SYM_PREFIX "_"
# define DYNLIB_NAME "VBoxXPCOM.dylib"
#else
# error "Port me"
#endif
/**
* Try load VBoxXPCOMC.so/dylib/dll from the specified location and resolve all
* the symbols we need.
*
* @returns 0 on success, -1 on failure.
* @param pszHome The director where to try load VBoxXPCOMC from. Can be NULL.
* @param pszMsgPrefix Error message prefix. NULL means no error messages.
*/
{
static struct
{
const char *pszSymbol;
void **ppvSym;
} const s_aSyms[] =
{
};
char * pszBuf;
int rc = 0;
/*
* Construct the full name.
*/
if (!pszBuf)
{
if (pszMsgPrefix)
return -1;
}
if (pszHome)
{
cchHome++;
}
/*
* Try load it by that name, setting the VBOX_APP_HOME first (for now).
*/
if (g_hVBoxXPCOMC)
{
while (i-- > 0)
{
if (!pv)
{
if (pszMsgPrefix)
rc = -1;
break;
}
}
}
else
{
rc = -1;
}
return rc;
}
/**
* Tries to locate and load VBoxXPCOMC.so/dylib/dll, resolving all the related
* function pointers.
*
* @returns 0 on success, -1 on failure.
* @param pszMsgPrefix Error message prefix. NULL means no error messages.
*
* @remark This should be considered moved into a separate glue library since
* its its going to be pretty much the same for any user of VBoxXPCOMC
* and it will just cause trouble to have duplicate versions of this
* source code all around the place.
*/
static int tryLoad(const char *pszMsgPrefix)
{
/*
* If the user specifies the location, try only that.
*/
if (pszHome)
/*
* Try the known standard locations.
*/
#if defined(__gnu__linux__) || defined(__linux__)
return 0;
return 0;
return 0;
return 0;
return 0;
#else
# error "port me"
#endif
/*
* Finally try the dynamic linker search path.
*/
return 0;
/* No luck, return failure. */
if (pszMsgPrefix)
return -1;
}
#endif
/* Main - Start the ball rolling. */
{
#ifndef USE_DYNAMIC_GLUE
void *xpcomHandle = NULL;
const char *xpcomdlError;
/*
* Guess where VirtualBox is installed not mentioned in the environment.
* (This will be moved to VBoxComInitialize later.)
*/
{
if (!xpcomHandle)
{
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
}
{
if (!xpcomHandle)
{
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
}
#else
/*
* Initialize the dynamic linking glue.
*/
{
return EXIT_FAILURE;
}
#endif
printf("Starting Main\n");
/*
* VBoxComInitialize does all the necessary startup action and
* provides us with pointers to vbox and session handles.
* It should be matched by a call to VBoxComUninitialize(vbox)
* when done.
*/
#ifndef USE_DYNAMIC_GLUE
dlerror(); /* Clear any existing error */
return EXIT_FAILURE;
}
#endif
{
return EXIT_FAILURE;
}
{
return EXIT_FAILURE;
}
#ifndef USE_DYNAMIC_GLUE
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
return EXIT_FAILURE;
}
#endif
/*
* 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.
*/
printf("----------------------------------------------------\n");
/* 1. Revision */
if (NS_SUCCEEDED(rc))
{
}
else
{
}
/* 2. Version */
if (NS_SUCCEEDED(rc))
{
}
else
{
}
/* 3. Home Folder */
if (NS_SUCCEEDED(rc))
{
char *homefolder = NULL;
}
else
{
}
printf("----------------------------------------------------\n");
/*
* Do as mom told us: always clean up after yourself.
*/
#ifndef USE_DYNAMIC_GLUE
return EXIT_FAILURE;
}
#endif
#ifndef USE_DYNAMIC_GLUE
#endif
printf("Finished Main\n");
return 0;
}
/* vim: set ts=4 sw=4 et: */