VBoxCAPI.cpp revision 0d625252f8e48f8ec3e2f5a0d30ced35b886fb75
/* $Id$ */
/** @file VBoxCAPI.cpp
* Utility functions to use with the C API binding.
*/
/*
* Copyright (C) 2009-2015 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.
*/
#define LOG_GROUP LOG_GROUP_MAIN
#include "VBoxCAPI.h"
#ifdef VBOX_WITH_XPCOM
# include <nsMemory.h>
# include <nsIServiceManager.h>
# include <nsEventQueueUtils.h>
# include <nsIExceptionService.h>
# include <stdlib.h>
#endif /* VBOX_WITH_XPCOM */
#include <iprt/initterm.h>
#include "VBox/com/NativeEventQueue.h"
using namespace std;
/* The following 2 object references should be eliminated once the legacy
#ifdef VBOX_WITH_XPCOM
/* This object reference should be eliminated once the legacy way of handling
* the event queue (XPCOM specific) is removed. */
#endif /* VBOX_WITH_XPCOM */
static void VBoxComUninitialize(void);
static void VBoxClientUninitialize(void);
static int
{
if (!pwszString)
{
*ppszString = NULL;
return VINF_SUCCESS;
}
}
static int
{
if (!pszString)
{
*ppwszString = NULL;
return VINF_SUCCESS;
}
#ifdef VBOX_WITH_XPCOM
#else /* !VBOX_WITH_XPCOM */
return vrc;
#endif /* !VBOX_WITH_XPCOM */
}
static void
VBoxUtf8Clear(char *pszString)
{
}
static void
{
}
static void
{
#ifdef VBOX_WITH_XPCOM
#else /* !VBOX_WITH_XPCOM */
#endif /* !VBOX_WITH_XPCOM */
}
static void
VBoxUtf8Free(char *pszString)
{
}
static void
{
if (pwsz)
{
#ifdef VBOX_WITH_XPCOM
#else /* !VBOX_WITH_XPCOM */
::SysFreeString(pwsz);
#endif /* !VBOX_WITH_XPCOM */
}
}
static void
VBoxComUnallocMem(void *pv)
{
}
static ULONG
{
switch (vt)
{
case VT_BOOL:
case VT_I1:
case VT_UI1:
return 1;
case VT_I2:
case VT_UI2:
return 2;
case VT_I4:
case VT_UI4:
case VT_HRESULT:
return 4;
case VT_I8:
case VT_UI8:
return 8;
case VT_BSTR:
case VT_DISPATCH:
case VT_UNKNOWN:
return sizeof(void *);
default:
return 0;
}
}
static SAFEARRAY *
{
#ifdef VBOX_WITH_XPCOM
if (!cbElement)
return NULL;
if (!psa)
return psa;
if (cElements)
{
if (!pv)
{
return NULL;
}
}
return psa;
#else /* !VBOX_WITH_XPCOM */
#endif /* !VBOX_WITH_XPCOM */
}
static SAFEARRAY *
{
#ifdef VBOX_WITH_XPCOM
#else /* !VBOX_WITH_XPCOM */
return NULL;
#endif /* !VBOX_WITH_XPCOM */
}
static HRESULT
{
#ifdef VBOX_WITH_XPCOM
if (psa)
{
}
return S_OK;
#else /* !VBOX_WITH_XPCOM */
return SafeArrayDestroy(psa);
#endif /* !VBOX_WITH_XPCOM */
}
static HRESULT
{
return E_POINTER;
if (!cb)
return S_OK;
void *pData;
#ifdef VBOX_WITH_XPCOM
#else /* !VBOX_WITH_XPCOM */
return rc;
#endif /* !VBOX_WITH_XPCOM */
#ifndef VBOX_WITH_XPCOM
#endif /* !VBOX_WITH_XPCOM */
return S_OK;
}
static HRESULT
{
if (!ppv)
return E_POINTER;
if (!cbElement)
{
if (pcb)
*pcb = 0;
return E_INVALIDARG;
}
#ifndef VBOX_WITH_XPCOM
{
if (pcb)
*pcb = 0;
return E_INVALIDARG;
}
#endif /* !VBOX_WITH_XPCOM */
void *pData;
#ifdef VBOX_WITH_XPCOM
#else /* !VBOX_WITH_XPCOM */
{
if (pcb)
*pcb = 0;
return rc;
}
#endif /* !VBOX_WITH_XPCOM */
if (cbTotal)
{
if (!pv)
{
if (pcb)
*pcb = 0;
return E_OUTOFMEMORY;
}
else
}
if (pcb)
#ifndef VBOX_WITH_XPCOM
#endif /* !VBOX_WITH_XPCOM */
return S_OK;
}
static HRESULT
{
{
if (pcObj)
*pcObj = 0;
return rc;
}
if (pcObj)
#ifndef VBOX_WITH_XPCOM
/* Do this only for COM, as there the SAFEARRAY destruction will release
* the contained references automatically. XPCOM doesn't do that, which
* means that copying implicitly transfers ownership. */
{
if (pObj)
}
#endif /* VBOX_WITH_XPCOM */
return S_OK;
}
static HRESULT
VBoxArrayOutFree(void *pv)
{
return S_OK;
}
static void
{
int vrc;
*ppVirtualBox = NULL;
/* convert the string representation of the UUIDs (if provided) to IID */
if (pszVirtualBoxIID && *pszVirtualBoxIID)
{
if (RT_FAILURE(vrc))
return;
}
else
if (pszSessionIID && *pszSessionIID)
{
if (RT_FAILURE(vrc))
return;
}
else
{
return;
}
#ifdef VBOX_WITH_XPCOM
{
return;
}
#endif /* VBOX_WITH_XPCOM */
#ifdef VBOX_WITH_XPCOM
{
return;
}
(void **)&g_VirtualBox);
#else /* !VBOX_WITH_XPCOM */
rc = CoCreateInstance(CLSID_VirtualBox, NULL, CLSCTX_LOCAL_SERVER, virtualBoxIID, (void **)&g_VirtualBox);
#endif /* !VBOX_WITH_XPCOM */
{
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
return;
}
Log(("Cbinding: IVirtualBox object created.\n"));
#ifdef VBOX_WITH_XPCOM
(void **)&g_Session);
#else /* !VBOX_WITH_XPCOM */
#endif /* !VBOX_WITH_XPCOM */
{
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
return;
}
Log(("Cbinding: ISession object created.\n"));
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
}
static void
{
}
static void
VBoxComUninitialize(void)
{
if (g_Session)
{
}
if (g_VirtualBox)
{
g_VirtualBox->Release();
g_VirtualBox = NULL;
}
#ifdef VBOX_WITH_XPCOM
if (g_EventQueue)
{
g_EventQueue->Release();
g_EventQueue = NULL;
}
#endif /* VBOX_WITH_XPCOM */
Log(("Cbinding: Cleaned up the created objects.\n"));
}
#ifdef VBOX_WITH_XPCOM
static void
{
}
#endif /* VBOX_WITH_XPCOM */
static int
{
else
switch (vrc)
{
case VINF_SUCCESS:
return 0;
case VINF_INTERRUPTED:
return 1;
case VERR_INTERRUPTED:
return 2;
case VERR_TIMEOUT:
return 3;
case VERR_INVALID_CONTEXT:
return 4;
default:
return 5;
}
}
static int
{
return 0;
}
static HRESULT
{
*ppException = NULL;
#ifdef VBOX_WITH_XPCOM
return rc;
{
return rc;
}
{
return rc;
}
{
return rc;
}
*ppException = ex;
#else /* !VBOX_WITH_XPCOM */
IErrorInfo *ex;
return rc;
*ppException = ex;
#endif /* !VBOX_WITH_XPCOM */
return rc;
}
static HRESULT
VBoxClearException(void)
{
#ifdef VBOX_WITH_XPCOM
return rc;
{
return rc;
}
{
return rc;
}
#else /* !VBOX_WITH_XPCOM */
#endif /* !VBOX_WITH_XPCOM */
return rc;
}
static HRESULT
{
/* convert the string representation of UUID to IID type */
{
if (RT_FAILURE(vrc))
return E_INVALIDARG;
}
else
{
return rc;
}
#ifdef VBOX_WITH_XPCOM
{
return rc;
}
#endif /* VBOX_WITH_XPCOM */
#ifdef VBOX_WITH_XPCOM
{
return rc;
}
(void **)ppVirtualBoxClient);
#else /* !VBOX_WITH_XPCOM */
rc = CoCreateInstance(CLSID_VirtualBoxClient, NULL, CLSCTX_INPROC_SERVER, virtualBoxClientIID, (void **)ppVirtualBoxClient);
#endif /* !VBOX_WITH_XPCOM */
{
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
return rc;
}
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
Log(("Cbinding: IVirtualBoxClient object created.\n"));
return S_OK;
}
static HRESULT
{
return com::Initialize();
}
static HRESULT
{
}
static void
VBoxClientUninitialize(void)
{
#ifdef VBOX_WITH_XPCOM
if (g_EventQueue)
{
g_EventQueue = NULL;
}
#endif /* VBOX_WITH_XPCOM */
Log(("Cbinding: Cleaned up the created objects.\n"));
}
static unsigned int
VBoxVersion(void)
{
}
static unsigned int
VBoxAPIVersion(void)
{
}
VBoxGetCAPIFunctions(unsigned uVersion)
{
/* This is the first piece of code which knows that IPRT exists, so
* initialize it properly. The limited initialization in VBoxC is not
* sufficient, and causes trouble with com::Initialize() misbehaving. */
RTR3InitDll(0);
/*
* The current interface version.
*/
static const VBOXCAPI s_Functions =
{
sizeof(VBOXCAPI),
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
};
return &s_Functions;
/*
* Legacy interface version 3.0.
*/
static const struct VBOXCAPIV3
{
/** The size of the structure. */
unsigned cb;
/** The structure version. */
unsigned uVersion;
unsigned int (*pfnGetVersion)(void);
unsigned int (*pfnGetAPIVersion)(void);
void (*pfnClientUninitialize)(void);
void (*pfnComInitialize)(const char *pszVirtualBoxIID,
const char *pszSessionIID,
void (*pfnComUninitialize)(void);
void (*pfnComUnallocMem)(void *pv);
void (*pfnUtf8Free)(char *pszString);
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
HRESULT (*pfnClearException)(void);
/** Tail version, same as uVersion. */
unsigned uEndVersion;
} s_Functions_v3_0 =
{
sizeof(s_Functions_v3_0),
0x00030000U,
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
0x00030000U
};
return (PCVBOXCAPI)&s_Functions_v3_0;
/*
* Legacy interface version 2.0.
*/
static const struct VBOXCAPIV2
{
/** The size of the structure. */
unsigned cb;
/** The structure version. */
unsigned uVersion;
unsigned int (*pfnGetVersion)(void);
void (*pfnComInitialize)(const char *pszVirtualBoxIID,
const char *pszSessionIID,
void (*pfnComUninitialize)(void);
void (*pfnComUnallocMem)(void *pv);
void (*pfnUtf8Free)(char *pszString);
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
/** Tail version, same as uVersion. */
unsigned uEndVersion;
} s_Functions_v2_0 =
{
sizeof(s_Functions_v2_0),
0x00020000U,
#ifdef VBOX_WITH_XPCOM
#endif /* VBOX_WITH_XPCOM */
0x00020000U
};
return (PCVBOXCAPI)&s_Functions_v2_0;
/*
* Legacy interface version 1.0.
*/
static const struct VBOXCAPIV1
{
/** The size of the structure. */
unsigned cb;
/** The structure version. */
unsigned uVersion;
unsigned int (*pfnGetVersion)(void);
void (*pfnComUninitialize)(void);
void (*pfnComUnallocMem)(void *pv);
void (*pfnUtf8Free)(char *pszString);
/** Tail version, same as uVersion. */
unsigned uEndVersion;
} s_Functions_v1_0 =
{
sizeof(s_Functions_v1_0),
0x00010000U,
0x00010000U
};
return (PCVBOXCAPI)&s_Functions_v1_0;
/*
* Unsupported interface version.
*/
return NULL;
}
#ifdef VBOX_WITH_XPCOM
VBoxGetXPCOMCFunctions(unsigned uVersion)
{
return VBoxGetCAPIFunctions(uVersion);
}
#endif /* VBOX_WITH_XPCOM */
/* vim: set ts=4 sw=4 et: */