vbox_MSCOMGlue.c 20.5 KB
Newer Older
1 2 3
/*
 * vbox_MSCOMGlue.c: glue to the MSCOM based VirtualBox API
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2013 Red Hat, Inc.
5
 * Copyright (C) 2010-2011 Matthias Bolte <matthias.bolte@googlemail.com>
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 */

#include <config.h>

25 26 27
#ifdef HAVE_WINSOCK2_H
# include <winsock2.h>
#endif
28 29 30 31 32
#include <windows.h>

#define nsCID CLSID

#include "internal.h"
33
#include "viralloc.h"
34
#include "virlog.h"
35
#include "virerror.h"
E
Eric Blake 已提交
36 37
#include "virstring.h"
#include "virutil.h"
38 39 40 41
#include "vbox_MSCOMGlue.h"

#define VIR_FROM_THIS VIR_FROM_VBOX

42 43
VIR_LOG_INIT("vbox.vbox_MSCOMGlue");

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
#define VBOX_REGKEY_ORACLE "Software\\Oracle\\VirtualBox"
#define VBOX_REGKEY_SUN "Software\\Sun\\xVM VirtualBox"

#define IVIRTUALBOX_IID_STR_v2_2 "779264f4-65ed-48ed-be39-518ca549e296"
#define ISESSION_IID_STR_v2_2 "12F4DCDB-12B2-4ec1-B7CD-DDD9F6C5BF4D"



typedef struct _VBOXXPCOMC_v1 VBOXXPCOMC_v1;
typedef struct _VBOXXPCOMC_v2 VBOXXPCOMC_v2;

struct _VBOXXPCOMC_v1 {
    unsigned cb;
    unsigned uVersion;
    unsigned int (*pfnGetVersion)(void);
    void (*pfnComInitialize)(IVirtualBox **virtualBox, ISession **session);
    void (*pfnComUninitialize)(void);
    void (*pfnComUnallocMem)(void *pv);
    void (*pfnUtf16Free)(PRUnichar *pwszString);
    void (*pfnUtf8Free)(char *pszString);
    int (*pfnUtf16ToUtf8)(const PRUnichar *pwszString, char **ppszString);
    int (*pfnUtf8ToUtf16)(const char *pszString, PRUnichar **ppwszString);
    unsigned uEndVersion;
};

struct _VBOXXPCOMC_v2 {
    unsigned cb;
    unsigned uVersion;
    unsigned int (*pfnGetVersion)(void);
    void (*pfnComInitialize)(const char *pszVirtualBoxIID,
                             IVirtualBox **ppVirtualBox,
                             const char *pszSessionIID,
                             ISession **ppSession);
    void (*pfnComUninitialize)(void);
    void (*pfnComUnallocMem)(void *pv);
    void (*pfnUtf16Free)(PRUnichar *pwszString);
    void (*pfnUtf8Free)(char *pszString);
    int (*pfnUtf16ToUtf8)(const PRUnichar *pwszString, char **ppszString);
    int (*pfnUtf8ToUtf16)(const char *pszString, PRUnichar **ppwszString);
    void (*pfnGetEventQueue)(nsIEventQueue **eventQueue);
    unsigned uEndVersion;
};



PFNVBOXGETXPCOMCFUNCTIONS g_pfnGetFunctions = NULL;

static unsigned long vboxVersion;
static IVirtualBox *vboxVirtualBox;
static ISession *vboxSession;



/*
 * nsISupports dummy implementation
 */

static nsresult __stdcall
vboxSupports_QueryInterface(nsISupports *pThis ATTRIBUTE_UNUSED,
                            const nsID *iid ATTRIBUTE_UNUSED,
                            void **resultp ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxSupports_AddRef(nsISupports *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxSupports_Release(nsISupports *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxSupports_GetTypeInfoCount(nsISupports *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxSupports_GetTypeInfo(nsISupports *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxSupports_GetIDsOfNames(nsISupports *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxSupports_Invoke(nsISupports *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}



/*
 * nsIEventTarget dummy implementation
 */

static nsresult __stdcall
vboxEventTarget_PostEvent(nsIEventTarget *pThis ATTRIBUTE_UNUSED,
                          PLEvent *aEvent ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventTarget_IsOnCurrentThread(nsIEventTarget *pThis ATTRIBUTE_UNUSED,
                                  PRBool *_retval ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}



/*
 * nsIEventQueue dummy implementation
 */

static nsresult __stdcall
vboxEventQueue_InitEvent(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                         PLEvent *aEvent ATTRIBUTE_UNUSED,
                         void *owner ATTRIBUTE_UNUSED,
                         PLHandleEventProc handler ATTRIBUTE_UNUSED,
                         PLDestroyEventProc destructor ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_PostSynchronousEvent(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                                    PLEvent *aEvent ATTRIBUTE_UNUSED,
                                    void **aResult ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_PendingEvents(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                             PRBool *_retval ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_ProcessPendingEvents(nsIEventQueue *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_EventLoop(nsIEventQueue *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_EventAvailable(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                              PRBool *aResult ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_GetEvent(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                        PLEvent **_retval ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_HandleEvent(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                           PLEvent *aEvent ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_WaitForEvent(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                            PLEvent **_retval ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static PRInt32 __stdcall
vboxEventQueue_GetEventQueueSelectFD(nsIEventQueue *pThis ATTRIBUTE_UNUSED)
{
    return -1;
}

static nsresult __stdcall
vboxEventQueue_Init(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                    PRBool aNative ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_InitFromPRThread(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                                PRThread *thread ATTRIBUTE_UNUSED,
                                PRBool aNative ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_InitFromPLQueue(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                               PLEventQueue *aQueue ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_EnterMonitor(nsIEventQueue *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_ExitMonitor(nsIEventQueue *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_RevokeEvents(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                            void *owner ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_GetPLEventQueue(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                               PLEventQueue **_retval ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_IsQueueNative(nsIEventQueue *pThis ATTRIBUTE_UNUSED,
                             PRBool *_retval ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static nsresult __stdcall
vboxEventQueue_StopAcceptingEvents(nsIEventQueue *pThis ATTRIBUTE_UNUSED)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

static struct nsIEventQueue_vtbl vboxEventQueueVtbl = {
    {
        {
            vboxSupports_QueryInterface,
            vboxSupports_AddRef,
            vboxSupports_Release,

            vboxSupports_GetTypeInfoCount,
            vboxSupports_GetTypeInfo,
            vboxSupports_GetIDsOfNames,
            vboxSupports_Invoke
        },

        vboxEventTarget_PostEvent,
        vboxEventTarget_IsOnCurrentThread
    },

    vboxEventQueue_InitEvent,
    vboxEventQueue_PostSynchronousEvent,
    vboxEventQueue_PendingEvents,
    vboxEventQueue_ProcessPendingEvents,
    vboxEventQueue_EventLoop,
    vboxEventQueue_EventAvailable,
    vboxEventQueue_GetEvent,
    vboxEventQueue_HandleEvent,
    vboxEventQueue_WaitForEvent,
    vboxEventQueue_GetEventQueueSelectFD,
    vboxEventQueue_Init,
    vboxEventQueue_InitFromPRThread,
    vboxEventQueue_InitFromPLQueue,
    vboxEventQueue_EnterMonitor,
    vboxEventQueue_ExitMonitor,
    vboxEventQueue_RevokeEvents,
    vboxEventQueue_GetPLEventQueue,
    vboxEventQueue_IsQueueNative,
    vboxEventQueue_StopAcceptingEvents,
};

static nsIEventQueue vboxEventQueue = {
    &vboxEventQueueVtbl
};



347 348
static char *
vboxLookupRegistryValue(HKEY key, const char *keyName, const char *valueName)
349 350 351 352 353 354
{
    LONG status;
    DWORD type;
    DWORD length;
    char *value = NULL;

355
    status = RegQueryValueEx(key, valueName, NULL, &type, NULL, &length);
356 357

    if (status != ERROR_SUCCESS) {
358 359
        VIR_ERROR(_("Could not query registry value '%s\\%s'"),
                  keyName, valueName);
360 361 362 363
        goto cleanup;
    }

    if (type != REG_SZ) {
364 365
        VIR_ERROR(_("Registry value '%s\\%s' has unexpected type"),
                  keyName, valueName);
366 367 368 369
        goto cleanup;
    }

    if (length < 2) {
370 371
        VIR_ERROR(_("Registry value '%s\\%s' is too short"),
                  keyName, valueName);
372 373 374 375
        goto cleanup;
    }

    /* +1 for the null-terminator if it's missing */
376
    if (VIR_ALLOC_N(value, length + 1) < 0)
377 378
        goto cleanup;

379
    status = RegQueryValueEx(key, valueName, NULL, NULL, (LPBYTE)value, &length);
380 381

    if (status != ERROR_SUCCESS) {
382 383 384
        VIR_FREE(value);
        VIR_ERROR(_("Could not query registry value '%s\\%s'"),
                  keyName, valueName);
385 386 387
        goto cleanup;
    }

388
    if (value[length - 1] != '\0')
389 390
        value[length] = '\0';

391
 cleanup:
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    return value;
}

static int
vboxLookupVersionInRegistry(void)
{
    int result = -1;
    const char *keyName = VBOX_REGKEY_ORACLE;
    LONG status;
    HKEY key;
    char *value = NULL;

    status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, KEY_READ, &key);

    if (status != ERROR_SUCCESS) {
        keyName = VBOX_REGKEY_SUN;
        status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, KEY_READ, &key);

        if (status != ERROR_SUCCESS) {
            /* Both keys aren't there, or we cannot open them. In general this
             * indicates that VirtualBox is not installed, so we just silently
             * fail here making vboxRegister() register the dummy driver. */
            return -1;
        }
    }

    /* The registry key layout changed around version 4.0.8. Before the version
     * number was in the Version key, now the Version key can contain %VER% and
     * the actual version number is in the VersionExt key then. */
    value = vboxLookupRegistryValue(key, keyName, "Version");

423
    if (value == NULL)
424 425 426 427 428 429
        goto cleanup;

    if (STREQ(value, "%VER%")) {
        VIR_FREE(value);
        value = vboxLookupRegistryValue(key, keyName, "VersionExt");

430
        if (value == NULL)
431 432 433
            goto cleanup;
    }

434
    if (virParseVersionString(value, &vboxVersion, false) < 0) {
435 436 437 438 439 440
        VIR_ERROR(_("Could not parse version number from '%s'"), value);
        goto cleanup;
    }

    result = 0;

441
 cleanup:
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    VIR_FREE(value);
    RegCloseKey(key);

    return result;
}

static unsigned int
vboxGetVersion(void)
{
    return vboxVersion;
}

static void
vboxComUnallocMem(void *pv)
{
    SysFreeString(pv);
}

static void
vboxUtf16Free(PRUnichar *pwszString)
{
    SysFreeString(pwszString);
}

static void
vboxUtf8Free(char *pszString)
{
    VIR_FREE(pszString);
}

static int
vboxUtf16ToUtf8(const PRUnichar *pwszString, char **ppszString)
{
    int length = WideCharToMultiByte(CP_UTF8, 0, pwszString, -1, NULL, 0,
                                     NULL, NULL);

478
    if (length < 1)
479 480
        return -1;

481
    if (VIR_ALLOC_N(*ppszString, length) < 0)
482 483 484 485 486 487 488 489 490 491 492
        return -1;

    return WideCharToMultiByte(CP_UTF8, 0, pwszString, -1, *ppszString,
                               length, NULL, NULL);
}

static int
vboxUtf8ToUtf16(const char *pszString, PRUnichar **ppwszString)
{
    int length = MultiByteToWideChar(CP_UTF8, 0, pszString, -1, NULL, 0);

493
    if (length < 1)
494 495 496 497
        return -1;

    *ppwszString = SysAllocStringLen(NULL, length);

498
    if (*ppwszString == NULL)
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
        return -1;

    return MultiByteToWideChar(CP_UTF8, 0, pszString, -1, *ppwszString, length);
}

static void
vboxGetEventQueue(nsIEventQueue **eventQueue)
{
    *eventQueue = &vboxEventQueue;
}

static void
vboxComInitialize_v2(const char *pszVirtualBoxIID, IVirtualBox **ppVirtualBox,
                     const char *pszSessionIID, ISession **ppSession)
{
    int result = -1;
    HRESULT hrc;
    IID virtualBoxIID;
    IID sessionIID;
    char *mbsVirtualBoxIID = NULL;
    char *mbsSessionIID = NULL;
    PRUnichar *wcsVirtualBoxIID = NULL;
    PRUnichar *wcsSessionIID = NULL;

    *ppVirtualBox = NULL;
    *ppSession = NULL;

    CoInitialize(NULL);

    if (virAsprintf(&mbsVirtualBoxIID, "{%s}", pszVirtualBoxIID) < 0 ||
529
        virAsprintf(&mbsSessionIID, "{%s}", pszSessionIID) < 0)
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
        goto cleanup;

    if (vboxUtf8ToUtf16(mbsVirtualBoxIID, &wcsVirtualBoxIID) < 0 ||
        vboxUtf8ToUtf16(mbsSessionIID, &wcsSessionIID) < 0) {
        goto cleanup;
    }

    hrc = IIDFromString(wcsVirtualBoxIID, &virtualBoxIID);

    if (FAILED(hrc)) {
        VIR_ERROR(_("Could not parse IID from '%s', rc = 0x%08x"),
                  pszVirtualBoxIID, (unsigned int)hrc);
        goto cleanup;
    }

    hrc = IIDFromString(wcsSessionIID, &sessionIID);

    if (FAILED(hrc)) {
        VIR_ERROR(_("Could not parse IID from '%s', rc = 0x%08x"),
                  pszVirtualBoxIID, (unsigned int)hrc);
        goto cleanup;
    }

    hrc = CoCreateInstance(&CLSID_VirtualBox, NULL, CLSCTX_LOCAL_SERVER,
                           &virtualBoxIID, (void**)&vboxVirtualBox);

    if (FAILED(hrc)) {
        VIR_ERROR(_("Could not create VirtualBox instance, rc = 0x%08x"),
                  (unsigned int)hrc);
        goto cleanup;
    }

    hrc = CoCreateInstance(&CLSID_Session, NULL, CLSCTX_INPROC_SERVER,
                           &sessionIID, (void**)&vboxSession);

    if (FAILED(hrc)) {
        VIR_ERROR(_("Could not create Session instance, rc = 0x%08x"),
                  (unsigned int)hrc);
        goto cleanup;
    }

    *ppVirtualBox = vboxVirtualBox;
    *ppSession = vboxSession;

    result = 0;

576
 cleanup:
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
    if (result < 0) {
        if (vboxVirtualBox != NULL) {
            vboxVirtualBox->vtbl->nsisupports.Release((nsISupports *)vboxVirtualBox);
            vboxVirtualBox = NULL;
        }

        if (vboxSession != NULL) {
            vboxSession->vtbl->nsisupports.Release((nsISupports *)vboxSession);
            vboxSession = NULL;
        }
    }

    vboxUtf16Free(wcsVirtualBoxIID);
    vboxUtf16Free(wcsSessionIID);
}

static void
vboxComInitialize_v1(IVirtualBox **ppVirtualBox, ISession **ppSession)
{
    vboxComInitialize_v2(IVIRTUALBOX_IID_STR_v2_2, ppVirtualBox,
                         ISESSION_IID_STR_v2_2, ppSession);
}

static void
vboxComUninitialize(void)
{
    if (vboxVirtualBox != NULL) {
        vboxVirtualBox->vtbl->nsisupports.Release((nsISupports *)vboxVirtualBox);
        vboxVirtualBox = NULL;
    }

    if (vboxSession != NULL) {
        vboxSession->vtbl->nsisupports.Release((nsISupports *)vboxSession);
        vboxSession = NULL;
    }

    CoUninitialize();
}



static VBOXXPCOMC_v1 vboxXPCOMC_v1 = {
619
    sizeof(VBOXXPCOMC_v1),      /* cb */
620 621 622 623 624 625 626 627 628 629 630 631 632
    0x00010000U,                /* uVersion */
    vboxGetVersion,             /* pfnGetVersion */
    vboxComInitialize_v1,       /* pfnComInitialize */
    vboxComUninitialize,        /* pfnComUninitialize */
    vboxComUnallocMem,          /* pfnComUnallocMem */
    vboxUtf16Free,              /* pfnUtf16Free */
    vboxUtf8Free,               /* pfnUtf8Free */
    vboxUtf16ToUtf8,            /* pfnUtf16ToUtf8 */
    vboxUtf8ToUtf16,            /* pfnUtf8ToUtf16 */
    0x00010000U                 /* uEndVersion */
};

static VBOXXPCOMC_v2 vboxXPCOMC_v2 = {
633
    sizeof(VBOXXPCOMC_v2),      /* cb */
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    0x00020000U,                /* uVersion */
    vboxGetVersion,             /* pfnGetVersion */
    vboxComInitialize_v2,       /* pfnComInitialize */
    vboxComUninitialize,        /* pfnComUninitialize */
    vboxComUnallocMem,          /* pfnComUnallocMem */
    vboxUtf16Free,              /* pfnUtf16Free */
    vboxUtf8Free,               /* pfnUtf8Free */
    vboxUtf16ToUtf8,            /* pfnUtf16ToUtf8 */
    vboxUtf8ToUtf16,            /* pfnUtf8ToUtf16 */
    vboxGetEventQueue,          /* pfnGetEventQueue */
    0x00020000U                 /* uEndVersion */
};

static PCVBOXXPCOM
vboxGetFunctions(unsigned int version)
{
    if (version == 0x00010000U) {
        return (PCVBOXXPCOM)&vboxXPCOMC_v1;
    } else if (version == 0x00020000U) {
        return (PCVBOXXPCOM)&vboxXPCOMC_v2;
    } else {
        return NULL;
    }
}



int
VBoxCGlueInit(unsigned int *version)
{
664
    if (vboxLookupVersionInRegistry() < 0)
665 666 667 668 669 670 671 672 673 674 675 676
        return -1;

    *version = vboxGetVersion();
    g_pfnGetFunctions = vboxGetFunctions;

    return 0;
}

void
VBoxCGlueTerm(void)
{
}
677 678 679 680 681 682 683 684 685 686 687 688 689 690



/*
 * In MSCOM an array is represented by a SAFEARRAY pointer. To access the items
 * in the array the SafeArrayAccessData function is used to lock the array and
 * get its contents. When the items aren't needed anymore the
 * SafeArrayUnaccessData function is used to unlock the array. The pointer
 * retuned by SafeArrayAccessData function becomes invalid. Finally the
 * SafeArrayDestroy function is called to destroy the array, it also releases
 * or frees all items in the array according to their type.
 */

typedef HRESULT __stdcall (*SafeArrayGetter)(void *self, SAFEARRAY **array);
691 692
typedef HRESULT __stdcall (*SafeArrayGetterWithPtrArg)(void *self, void *arg, SAFEARRAY **array);
typedef HRESULT __stdcall (*SafeArrayGetterWithUintArg)(void *self, PRUint32 arg, SAFEARRAY **array);
693

694 695
static nsresult
vboxArrayGetHelper(vboxArray *array, HRESULT hrc, SAFEARRAY *safeArray)
696 697 698 699 700 701 702
{
    void **items = NULL;

    array->items = NULL;
    array->count = 0;
    array->handle = NULL;

703
    if (FAILED(hrc))
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
        return hrc;

    hrc = SafeArrayAccessData(safeArray, (void **)&items);

    if (FAILED(hrc)) {
        SafeArrayDestroy(safeArray);
        return hrc;
    }

    array->items = items;
    array->count = safeArray->rgsabound[0].cElements;
    array->handle = safeArray;

    return hrc;
}

/*
721 722
 * Call the getter with self as first argument and fill the array with the
 * returned items.
723 724
 */
nsresult
725
vboxArrayGet(vboxArray *array, void *self, void *getter)
726 727 728 729
{
    HRESULT hrc;
    SAFEARRAY *safeArray = NULL;

730
    hrc = ((SafeArrayGetter)getter)(self, &safeArray);
731

732 733
    return vboxArrayGetHelper(array, hrc, safeArray);
}
734

735 736 737 738 739 740 741 742 743
/*
 * Call the getter with self as first argument and arg as second argument
 * and fill the array with the returned items.
 */
nsresult
vboxArrayGetWithPtrArg(vboxArray *array, void *self, void *getter, void *arg)
{
    HRESULT hrc;
    SAFEARRAY *safeArray = NULL;
744

745
    hrc = ((SafeArrayGetterWithPtrArg)getter)(self, arg, &safeArray);
746

747 748
    return vboxArrayGetHelper(array, hrc, safeArray);
}
749

750 751 752 753 754 755 756 757 758
/*
 * Call the getter with self as first argument and arg as second argument
 * and fill the array with the returned items.
 */
nsresult
vboxArrayGetWithUintArg(vboxArray *array, void *self, void *getter, PRUint32 arg)
{
    HRESULT hrc;
    SAFEARRAY *safeArray = NULL;
759

760 761 762
    hrc = ((SafeArrayGetterWithUintArg)getter)(self, arg, &safeArray);

    return vboxArrayGetHelper(array, hrc, safeArray);
763 764 765 766 767 768 769 770 771 772 773 774
}

/*
 * Release all items in the array and reset it.
 *
 * SafeArrayDestroy is aware of the item's type and calls release or free
 * for each item according to its type. Therefore, vboxArrayUnalloc and
 * vboxArrayRelease are the same for MSCOM.
 */
void
vboxArrayRelease(vboxArray *array)
{
775
    if (array->handle == NULL)
776 777 778 779 780 781 782 783 784
        return;

    SafeArrayUnaccessData(array->handle);
    SafeArrayDestroy(array->handle);

    array->items = NULL;
    array->count = 0;
    array->handle = NULL;
}