VirtualBox

source: vbox/trunk/src/VBox/Main/src-all/win/VBoxProxyStub.c@ 63259

Last change on this file since 63259 was 63259, checked in by vboxsync, 9 years ago

Main: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 87.9 KB
Line 
1/* $Id: VBoxProxyStub.c 63259 2016-08-10 12:37:42Z vboxsync $ */
2/** @file
3 * VBoxProxyStub - Proxy Stub and Typelib, COM DLL exports and DLL init/term.
4 *
5 * @remarks This is a C file and not C++ because rpcproxy.h isn't C++ clean,
6 * at least not in SDK v7.1.
7 */
8
9/*
10 * Copyright (C) 2006-2016 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.215389.xyz. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 */
20
21
22/*********************************************************************************************************************************
23* Header Files *
24*********************************************************************************************************************************/
25#define LOG_GROUP LOG_GROUP_MAIN
26#define PROXY_DELEGATION /* see generated dlldata.c */
27#include <iprt/nt/nt-and-windows.h>
28#include <rpcproxy.h>
29#include <iprt/win/shlwapi.h>
30#include <stdio.h>
31
32#include "VirtualBox.h"
33#include <VBox/cdefs.h> /* for VBOX_STRICT */
34#include <VBox/log.h>
35#include <iprt/alloca.h>
36#include <iprt/assert.h>
37#include <iprt/ctype.h>
38#include <iprt/initterm.h>
39#include <iprt/path.h>
40#include <iprt/string.h>
41#include <iprt/uuid.h>
42
43
44/*********************************************************************************************************************************
45* Defined Constants And Macros *
46*********************************************************************************************************************************/
47#ifdef DEBUG_bird
48# define VBSP_LOG_ENABLED
49#endif
50
51#ifdef VBSP_LOG_ENABLED
52# define VBSP_LOG_VALUE_CHANGE(a) RTAssertMsg2 a
53#else
54# define VBSP_LOG_VALUE_CHANGE(a) do { } while (0)
55#endif
56
57#ifdef VBSP_LOG_ENABLED
58# define VBSP_LOG_SET_VALUE(a) RTAssertMsg2 a
59#else
60# define VBSP_LOG_SET_VALUE(a) do { } while (0)
61#endif
62
63#ifdef VBSP_LOG_ENABLED
64# define VBSP_LOG_NEW_KEY(a) RTAssertMsg2 a
65#else
66# define VBSP_LOG_NEW_KEY(a) do { } while (0)
67#endif
68
69#ifdef VBSP_LOG_ENABLED
70# define VBSP_LOG_DEL_KEY(a) RTAssertMsg2 a
71#else
72# define VBSP_LOG_DEL_KEY(a) do { } while (0)
73#endif
74
75/**
76 * Selects the proxy stub DLL based on 32-on-64-bit and host OS version.
77 *
78 * The legacy DLL covers 64-bit pre Windows 7 versions of Windows. W2K3-amd64
79 * has trouble parsing the result when MIDL /target NT51 or higher. Vista and
80 * windows server 2008 seems to have trouble with newer IDL compilers.
81 */
82#if ARCH_BITS == 64 || defined(VBOX_IN_32_ON_64_MAIN_API)
83# define VBPS_PROXY_STUB_FILE(a_fIs32On64) ( (a_fIs32On64) ? "x86\\VBoxProxyStub-x86.dll" : VBPS_PROXY_STUB_FILE_SUB() )
84#else
85# define VBPS_PROXY_STUB_FILE(a_fIs32On64) VBPS_PROXY_STUB_FILE_SUB()
86#endif
87#define VBPS_PROXY_STUB_FILE_SUB() \
88 ( RT_MAKE_U64(((PKUSER_SHARED_DATA)MM_SHARED_USER_DATA_VA)->NtMinorVersion, \
89 ((PKUSER_SHARED_DATA)MM_SHARED_USER_DATA_VA)->NtMajorVersion) >= RT_MAKE_U64(1/*Lo*/,6/*Hi*/) \
90 ? "VBoxProxyStub.dll" : "VBoxProxyStubLegacy.dll" )
91
92/** For use with AssertLogRel except a_Expr1 from assertions but not LogRel. */
93#ifdef RT_STRICT
94# define VBPS_LOGREL_NO_ASSERT(a_Expr) (a_Expr)
95#else
96# define VBPS_LOGREL_NO_ASSERT(a_Expr) false
97#endif
98
99
100/*********************************************************************************************************************************
101* Global Variables *
102*********************************************************************************************************************************/
103/** For NdrXxx. */
104CStdPSFactoryBuffer g_ProxyStubFactory = /* see generated dlldata.c */
105{
106 NULL,
107 0,
108 NULL,
109 0
110};
111/** Reference to VirtualBox_p.c structure. */
112EXTERN_PROXY_FILE(VirtualBox) /* see generated dlldata.c */
113/** For NdrXxx and for returning. */
114static const ProxyFileInfo *g_apProxyFiles[] =
115{
116 REFERENCE_PROXY_FILE(VirtualBox),
117 NULL /* terminator */
118};
119/** The class ID for this proxy stub factory (see Makefile). */
120static const CLSID g_ProxyClsId = PROXY_CLSID_IS;
121/** The instance handle of this DLL. For use in registration routines. */
122static HINSTANCE g_hDllSelf;
123
124
125/** Type library GUIDs to clean up manually.
126 * Must be upper case! */
127static PCRTUTF16 const g_apwszTypeLibIds[] =
128{
129 L"{46137EEC-703B-4FE5-AFD4-7C9BBBBA0259}",
130 L"{D7569351-1750-46F0-936E-BD127D5BC264}",
131};
132
133/** Type library version to clean up manually. */
134static PCRTUTF16 const g_apwszTypelibVersions[] =
135{
136 L"1.0",
137 L"1.3",
138};
139
140/** Proxy stub class IDs we wish to clean up manually.
141 * Must be upper case! */
142static PCRTUTF16 const g_apwszProxyStubClsIds[] =
143{
144 L"{0BB3B78C-1807-4249-5BA5-EA42D66AF0BF}",
145 L"{327E3C00-EE61-462F-AED3-0DFF6CBF9904}",
146};
147
148
149/**
150 * DLL main function.
151 *
152 * @returns TRUE (/ FALSE).
153 * @param hInstance The DLL handle.
154 * @param dwReason The rason for the call (DLL_XXX).
155 * @param lpReserved Reserved.
156 */
157BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
158{
159 switch (dwReason)
160 {
161 case DLL_PROCESS_ATTACH:
162 /* Save the DLL handle so we can get the path to this DLL during
163 registration and updating. */
164 g_hDllSelf = hInstance;
165
166 /* We don't need callbacks for thread creation and destruction. */
167 DisableThreadLibraryCalls(hInstance);
168
169 /* Init IPRT. */
170 RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
171
172#ifdef VBOX_STRICT
173 {
174 /*
175 * Check that no interface has more than 256 methods in the stub vtable.
176 */
177 const ProxyFileInfo **ppProxyFile = &g_apProxyFiles[0];
178 const ProxyFileInfo *pProxyFile;
179 while ((pProxyFile = *ppProxyFile++) != NULL)
180 {
181 const PCInterfaceStubVtblList * const papStubVtbls = pProxyFile->pStubVtblList;
182 const char * const *papszNames = pProxyFile->pNamesArray;
183 unsigned iIf = pProxyFile->TableSize;
184 AssertStmt(iIf < 1024, iIf = 0);
185 Assert(pProxyFile->TableVersion == 2);
186
187 while (iIf-- > 0)
188 AssertMsg(papStubVtbls[iIf]->header.DispatchTableCount <= 256,
189 ("%s: DispatchTableCount=%d\n", papszNames[iIf], papStubVtbls[iIf]->header.DispatchTableCount));
190 }
191 }
192#endif
193 break;
194
195 case DLL_PROCESS_DETACH:
196 break;
197 }
198
199 NOREF(lpReserved);
200 return TRUE;
201}
202
203
204/**
205 * RPC entry point returning info about the proxy.
206 */
207void RPC_ENTRY GetProxyDllInfo(const ProxyFileInfo ***ppapInfo, const CLSID **ppClsid)
208{
209 *ppapInfo = &g_apProxyFiles[0];
210 *ppClsid = &g_ProxyClsId;
211}
212
213
214/**
215 * Instantiate the proxy stub class object.
216 *
217 * @returns COM status code
218 * @param rclsid Reference to the ID of the call to instantiate (our
219 * g_ProxyClsId).
220 * @param riid The interface ID to return (IID_IPSFactoryBuffer).
221 * @param ppv Where to return the interface pointer on success.
222 */
223HRESULT STDAPICALLTYPE DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
224{
225 HRESULT hrc;
226 Assert(memcmp(rclsid, &g_ProxyClsId, sizeof(g_ProxyClsId)) == 0);
227
228 hrc = NdrDllGetClassObject(rclsid, riid, ppv, /* see DLLGETCLASSOBJECTROUTINE in RpcProxy.h */
229 g_apProxyFiles, &g_ProxyClsId, &g_ProxyStubFactory);
230
231 /*
232 * This may fail if the IDL compiler generates code that is incompatible
233 * with older windows releases. Like for instance 64-bit W2K8 SP1 not
234 * liking the output of MIDL 7.00.0555 (from the v7.1 SDK), despite
235 * /target being set to NT51.
236 */
237 AssertLogRelMsg(hrc == S_OK, ("%Rhrc\n", hrc));
238 return hrc;
239}
240
241
242/**
243 * Checks whether the DLL can be unloaded or not.
244 *
245 * @returns S_OK if it can be unloaded, S_FALSE if not.
246 */
247HRESULT STDAPICALLTYPE DllCanUnloadNow(void)
248{
249 return NdrDllCanUnloadNow(&g_ProxyStubFactory); /* see DLLCANUNLOADNOW in RpcProxy.h */
250}
251
252
253
254/**
255 * Release call that could be referenced by VirtualBox_p.c via
256 * CStdStubBuffer_METHODS.
257 *
258 * @returns New reference count.
259 * @param pThis Buffer to release.
260 */
261ULONG STDMETHODCALLTYPE CStdStubBuffer_Release(IRpcStubBuffer *pThis) /* see CSTDSTUBBUFFERRELEASE in RpcProxy.h */
262{
263 return NdrCStdStubBuffer_Release(pThis, (IPSFactoryBuffer *)&g_ProxyStubFactory);
264}
265
266
267/**
268 * Release call referenced by VirtualBox_p.c via
269 * CStdStubBuffer_DELEGATING_METHODS.
270 *
271 * @returns New reference count.
272 * @param pThis Buffer to release.
273 */
274ULONG WINAPI CStdStubBuffer2_Release(IRpcStubBuffer *pThis) /* see CSTDSTUBBUFFER2RELEASE in RpcProxy.h */
275{
276 return NdrCStdStubBuffer2_Release(pThis, (IPSFactoryBuffer *)&g_ProxyStubFactory);
277}
278
279
280/**
281 * Pure virtual method implementation referenced by VirtualBox_p.c
282 *
283 * @returns New reference count.
284 * @param pThis Buffer to release.
285 */
286void __cdecl _purecall(void) /* see DLLDUMMYPURECALL in RpcProxy.h */
287{
288 AssertFailed();
289}
290
291
292#ifdef VBSP_LOG_ENABLED
293# include <iprt/asm.h>
294
295/** For logging full key names. */
296static PCRTUTF16 vbpsDebugKeyToWSZ(HKEY hKey)
297{
298 static union
299 {
300 KEY_NAME_INFORMATION NameInfo;
301 WCHAR awchPadding[260];
302 } s_aBufs[4];
303 static uint32_t volatile iNext = 0;
304 uint32_t i = ASMAtomicIncU32(&iNext) % RT_ELEMENTS(s_aBufs);
305 ULONG cbRet = 0;
306 NTSTATUS rcNt;
307
308 memset(&s_aBufs[i], 0, sizeof(s_aBufs[i]));
309 rcNt = NtQueryKey(hKey, KeyNameInformation, &s_aBufs[i], sizeof(s_aBufs[i]) - sizeof(WCHAR), &cbRet);
310 if (!NT_SUCCESS(rcNt))
311 s_aBufs[i].NameInfo.NameLength = 0;
312 s_aBufs[i].NameInfo.Name[s_aBufs[i].NameInfo.NameLength] = '\0';
313 return s_aBufs[i].NameInfo.Name;
314}
315#endif
316
317/**
318 * Registry modifier state.
319 */
320typedef struct VBPSREGSTATE
321{
322 /** Where the classes and stuff are to be registered. */
323 HKEY hkeyClassesRootDst;
324 /** The handle to the CLSID key under hkeyClassesRootDst. */
325 HKEY hkeyClsidRootDst;
326 /** The handle to the Interface key under hkeyClassesRootDst. */
327 HKEY hkeyInterfaceRootDst;
328
329 /** Alternative locations where data needs to be deleted, but never updated. */
330 struct
331 {
332 /** The classes root key handle. */
333 HKEY hkeyClasses;
334 /** The classes/CLSID key handle. */
335 HKEY hkeyClsid;
336 /** The classes/Interface key handle. */
337 HKEY hkeyInterface;
338 } aAltDeletes[3];
339 /** Alternative delete locations. */
340 uint32_t cAltDeletes;
341
342 /** The current total result. */
343 LSTATUS rc;
344
345 /** KEY_WOW64_32KEY, KEY_WOW64_64KEY or 0 (for default). Allows doing all
346 * almost the work from one process (at least W7+ due to aliases). */
347 DWORD fSamWow;
348 /** Desired key access when only deleting. */
349 DWORD fSamDelete;
350 /** Desired key access when only doing updates. */
351 DWORD fSamUpdate;
352 /** Desired key access when both deleting and updating. */
353 DWORD fSamBoth;
354 /** Whether to delete registrations first. */
355 bool fDelete;
356 /** Whether to update registry value and keys. */
357 bool fUpdate;
358
359} VBPSREGSTATE;
360
361
362/**
363 * Initializes a registry modification job state.
364 *
365 * Always call vbpsRegTerm!
366 *
367 * @returns Windows error code (ERROR_SUCCESS on success).
368 * @param pState The state to init.
369 * @param hkeyRoot The registry root tree constant.
370 * @param pszSubRoot The path to the where the classes are registered,
371 * NULL if @a hkeyRoot.
372 * @param hkeyAltRoot The registry root tree constant for the alternative
373 * registrations (remove only).
374 * @param pszAltSubRoot The path to where classes could also be registered,
375 * but shouldn't be in our setup.
376 * @param fDelete Whether to delete registrations first.
377 * @param fUpdate Whether to update registrations.
378 * @param fSamWow KEY_WOW64_32KEY or 0.
379 */
380static LSTATUS vbpsRegInit(VBPSREGSTATE *pState, HKEY hkeyRoot, const char *pszSubRoot, bool fDelete, bool fUpdate, DWORD fSamWow)
381{
382 LSTATUS rc;
383 unsigned i = 0;
384
385 /*
386 * Initialize the whole structure first so we can safely call vbpsRegTerm on failure.
387 */
388 pState->hkeyClassesRootDst = NULL;
389 pState->hkeyClsidRootDst = NULL;
390 pState->hkeyInterfaceRootDst = NULL;
391 for (i = 0; i < RT_ELEMENTS(pState->aAltDeletes); i++)
392 {
393 pState->aAltDeletes[i].hkeyClasses = NULL;
394 pState->aAltDeletes[i].hkeyClsid = NULL;
395 pState->aAltDeletes[i].hkeyInterface = NULL;
396 }
397 pState->cAltDeletes = 0;
398 pState->rc = ERROR_SUCCESS;
399 pState->fDelete = fDelete;
400 pState->fUpdate = fUpdate;
401 pState->fSamWow = fSamWow;
402 pState->fSamDelete = 0;
403 if (fDelete)
404 pState->fSamDelete = pState->fSamWow | DELETE | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE
405 | STANDARD_RIGHTS_READ | STANDARD_RIGHTS_WRITE;
406 pState->fSamUpdate = 0;
407 if (fUpdate)
408 pState->fSamUpdate = pState->fSamWow | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY
409 | STANDARD_RIGHTS_READ | STANDARD_RIGHTS_WRITE;
410 pState->fSamBoth = pState->fSamDelete | pState->fSamUpdate;
411
412 /*
413 * Open the root keys.
414 */
415 rc = RegOpenKeyExA(hkeyRoot, pszSubRoot, 0 /*fOptions*/, pState->fSamBoth, &pState->hkeyClassesRootDst);
416 if (rc == ERROR_SUCCESS)
417 {
418 rc = RegCreateKeyExW(pState->hkeyClassesRootDst, L"CLSID", 0 /*Reserved*/, NULL /*pszClass*/, 0 /*fOptions*/,
419 pState->fSamBoth, NULL /*pSecAttr*/, &pState->hkeyClsidRootDst, NULL /*pdwDisposition*/);
420 if (rc == ERROR_SUCCESS)
421 return ERROR_SUCCESS;
422
423 /* Ignore access denied errors as these may easily happen for
424 non-admin users. Just give up when this happens */
425 AssertLogRelMsgReturn(rc == ERROR_ACCESS_DENIED, ("%u\n", rc), pState->rc = rc);
426 }
427 else
428 AssertLogRelMsgReturn(rc == ERROR_ACCESS_DENIED, ("%u\n", rc), pState->rc = rc);
429 return pState->rc = rc;
430}
431
432
433/**
434 * Terminates the state, closing all open keys.
435 *
436 * @param pState The state to clean up.
437 */
438static void vbpsRegTerm(VBPSREGSTATE *pState)
439{
440 LSTATUS rc;
441 if (pState->hkeyClassesRootDst)
442 {
443 rc = RegCloseKey(pState->hkeyClassesRootDst);
444 Assert(rc == ERROR_SUCCESS);
445 pState->hkeyClassesRootDst = NULL;
446 }
447 if (pState->hkeyClsidRootDst)
448 {
449 rc = RegCloseKey(pState->hkeyClsidRootDst);
450 Assert(rc == ERROR_SUCCESS);
451 pState->hkeyClsidRootDst = NULL;
452 }
453 if (pState->hkeyInterfaceRootDst)
454 {
455 rc = RegCloseKey(pState->hkeyInterfaceRootDst);
456 Assert(rc == ERROR_SUCCESS);
457 pState->hkeyInterfaceRootDst = NULL;
458 }
459
460 while (pState->cAltDeletes > 0 && pState->cAltDeletes <= RT_ELEMENTS(pState->aAltDeletes))
461 {
462 unsigned i = --pState->cAltDeletes;
463 if (pState->aAltDeletes[i].hkeyClasses)
464 {
465 rc = RegCloseKey(pState->aAltDeletes[i].hkeyClasses);
466 Assert(rc == ERROR_SUCCESS);
467 pState->aAltDeletes[i].hkeyClasses = NULL;
468 }
469 if (pState->aAltDeletes[i].hkeyClsid)
470 {
471 rc = RegCloseKey(pState->aAltDeletes[i].hkeyClsid);
472 Assert(rc == ERROR_SUCCESS);
473 pState->aAltDeletes[i].hkeyClsid = NULL;
474 }
475 if (pState->aAltDeletes[i].hkeyInterface)
476 {
477 rc = RegCloseKey(pState->aAltDeletes[i].hkeyInterface);
478 Assert(rc == ERROR_SUCCESS);
479 pState->aAltDeletes[i].hkeyInterface = NULL;
480 }
481 }
482}
483
484
485/**
486 * Add an alternative registry classes tree from which to remove keys.
487 *
488 * @returns ERROR_SUCCESS if we successfully opened the destination root, other
489 * wise windows error code (remebered).
490 * @param pState The registry modifier state.
491 * @param hkeyAltRoot The root of the alternate registry classes
492 * location.
493 * @param pszAltSubRoot The path to the 'classes' sub-key, or NULL if
494 * hkeyAltRoot is it.
495 */
496static LSTATUS vbpsRegAddAltDelete(VBPSREGSTATE *pState, HKEY hkeyAltRoot, const char *pszAltSubRoot)
497{
498 unsigned i;
499 LSTATUS rc;
500
501 /* Ignore call if not in delete mode. */
502 if (!pState->fDelete)
503 return ERROR_SUCCESS;
504
505 /* Check that there is space in the state. */
506 i = pState->cAltDeletes;
507 AssertReturn(i < RT_ELEMENTS(pState->aAltDeletes), pState->rc = ERROR_TOO_MANY_NAMES);
508
509
510 /* Open the root. */
511 rc = RegOpenKeyExA(hkeyAltRoot, pszAltSubRoot, 0 /*fOptions*/, pState->fSamDelete,
512 &pState->aAltDeletes[i].hkeyClasses);
513 if (rc == ERROR_SUCCESS)
514 {
515 /* Try open the CLSID subkey, it's fine if it doesn't exists. */
516 rc = RegOpenKeyExW(pState->aAltDeletes[i].hkeyClasses, L"CLSID", 0 /*fOptions*/, pState->fSamDelete,
517 &pState->aAltDeletes[i].hkeyClsid);
518 if (rc == ERROR_SUCCESS || rc == ERROR_FILE_NOT_FOUND)
519 {
520 if (rc == ERROR_FILE_NOT_FOUND)
521 pState->aAltDeletes[i].hkeyClsid = NULL;
522 pState->cAltDeletes = i + 1;
523 return ERROR_SUCCESS;
524 }
525 AssertLogRelMsgFailed(("%u\n", rc));
526 RegCloseKey(pState->aAltDeletes[i].hkeyClasses);
527 }
528 /* No need to add non-existing alternative roots, nothing to delete in the void. */
529 else if (rc == ERROR_FILE_NOT_FOUND)
530 rc = ERROR_SUCCESS;
531 else
532 {
533 AssertLogRelMsgFailed(("%u (%#x %s)\n", rc));
534 pState->rc = rc;
535 }
536
537 pState->aAltDeletes[i].hkeyClasses = NULL;
538 pState->aAltDeletes[i].hkeyClsid = NULL;
539 return rc;
540}
541
542
543/**
544 * Open the 'Interface' keys under the current classes roots.
545 *
546 * We don't do this during vbpsRegInit as it's only needed for updating.
547 *
548 * @returns ERROR_SUCCESS if we successfully opened the destination root, other
549 * wise windows error code (remebered).
550 * @param pState The registry modifier state.
551 */
552static LSTATUS vbpsRegOpenInterfaceKeys(VBPSREGSTATE *pState)
553{
554 unsigned i;
555 LSTATUS rc;
556
557 /*
558 * Under the root destination.
559 */
560 if (pState->hkeyInterfaceRootDst == NULL)
561 {
562 if (pState->fSamUpdate)
563 rc = RegCreateKeyExW(pState->hkeyClassesRootDst, L"Interface", 0 /*Reserved*/, NULL /*pszClass*/, 0 /*fOptions*/,
564 pState->fSamBoth, NULL /*pSecAttr*/, &pState->hkeyInterfaceRootDst, NULL /*pdwDisposition*/);
565 else
566 rc = RegOpenKeyExW(pState->hkeyClassesRootDst, L"Interface", 0 /*fOptions*/, pState->fSamBoth,
567 &pState->hkeyClsidRootDst);
568 AssertLogRelMsgReturnStmt(rc == ERROR_SUCCESS, ("%u\n", rc), pState->hkeyInterfaceRootDst = NULL, pState->rc = rc);
569 }
570
571 /*
572 * Under the alternative delete locations.
573 */
574 i = pState->cAltDeletes;
575 while (i-- > 0)
576 if (pState->aAltDeletes[i].hkeyInterface == NULL)
577 {
578 rc = RegOpenKeyExW(pState->aAltDeletes[i].hkeyClasses, L"Interface", 0 /*fOptions*/, pState->fSamDelete,
579 &pState->aAltDeletes[i].hkeyInterface);
580 if (rc != ERROR_SUCCESS)
581 {
582 AssertMsgStmt(rc == ERROR_FILE_NOT_FOUND || ERROR_ACCESS_DENIED, ("%u\n", rc), pState->rc = rc);
583 pState->aAltDeletes[i].hkeyInterface = NULL;
584 }
585 }
586
587 return ERROR_SUCCESS;
588}
589
590
591/** The destination buffer size required by vbpsFormatUuidInCurly. */
592#define CURLY_UUID_STR_BUF_SIZE 40
593
594/**
595 * Formats a UUID to a string, inside curly braces.
596 *
597 * @returns @a pszString
598 * @param pszString Output buffer of size CURLY_UUID_STR_BUF_SIZE.
599 * @param pUuidIn The UUID to format.
600 */
601static const char *vbpsFormatUuidInCurly(char pszString[CURLY_UUID_STR_BUF_SIZE], const CLSID *pUuidIn)
602{
603 static const char s_achDigits[17] = "0123456789abcdef";
604 PCRTUUID pUuid = (PCRTUUID)pUuidIn;
605 uint32_t u32TimeLow;
606 unsigned u;
607
608 pszString[ 0] = '{';
609 u32TimeLow = RT_H2LE_U32(pUuid->Gen.u32TimeLow);
610 pszString[ 1] = s_achDigits[(u32TimeLow >> 28)/*& 0xf*/];
611 pszString[ 2] = s_achDigits[(u32TimeLow >> 24) & 0xf];
612 pszString[ 3] = s_achDigits[(u32TimeLow >> 20) & 0xf];
613 pszString[ 4] = s_achDigits[(u32TimeLow >> 16) & 0xf];
614 pszString[ 5] = s_achDigits[(u32TimeLow >> 12) & 0xf];
615 pszString[ 6] = s_achDigits[(u32TimeLow >> 8) & 0xf];
616 pszString[ 7] = s_achDigits[(u32TimeLow >> 4) & 0xf];
617 pszString[ 8] = s_achDigits[(u32TimeLow/*>>0*/)& 0xf];
618 pszString[ 9] = '-';
619 u = RT_H2LE_U16(pUuid->Gen.u16TimeMid);
620 pszString[10] = s_achDigits[(u >> 12)/*& 0xf*/];
621 pszString[11] = s_achDigits[(u >> 8) & 0xf];
622 pszString[12] = s_achDigits[(u >> 4) & 0xf];
623 pszString[13] = s_achDigits[(u/*>>0*/)& 0xf];
624 pszString[14] = '-';
625 u = RT_H2LE_U16(pUuid->Gen.u16TimeHiAndVersion);
626 pszString[15] = s_achDigits[(u >> 12)/*& 0xf*/];
627 pszString[16] = s_achDigits[(u >> 8) & 0xf];
628 pszString[17] = s_achDigits[(u >> 4) & 0xf];
629 pszString[18] = s_achDigits[(u/*>>0*/)& 0xf];
630 pszString[19] = '-';
631 pszString[20] = s_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved >> 4];
632 pszString[21] = s_achDigits[pUuid->Gen.u8ClockSeqHiAndReserved & 0xf];
633 pszString[22] = s_achDigits[pUuid->Gen.u8ClockSeqLow >> 4];
634 pszString[23] = s_achDigits[pUuid->Gen.u8ClockSeqLow & 0xf];
635 pszString[24] = '-';
636 pszString[25] = s_achDigits[pUuid->Gen.au8Node[0] >> 4];
637 pszString[26] = s_achDigits[pUuid->Gen.au8Node[0] & 0xf];
638 pszString[27] = s_achDigits[pUuid->Gen.au8Node[1] >> 4];
639 pszString[28] = s_achDigits[pUuid->Gen.au8Node[1] & 0xf];
640 pszString[29] = s_achDigits[pUuid->Gen.au8Node[2] >> 4];
641 pszString[30] = s_achDigits[pUuid->Gen.au8Node[2] & 0xf];
642 pszString[31] = s_achDigits[pUuid->Gen.au8Node[3] >> 4];
643 pszString[32] = s_achDigits[pUuid->Gen.au8Node[3] & 0xf];
644 pszString[33] = s_achDigits[pUuid->Gen.au8Node[4] >> 4];
645 pszString[34] = s_achDigits[pUuid->Gen.au8Node[4] & 0xf];
646 pszString[35] = s_achDigits[pUuid->Gen.au8Node[5] >> 4];
647 pszString[36] = s_achDigits[pUuid->Gen.au8Node[5] & 0xf];
648 pszString[37] = '}';
649 pszString[38] = '\0';
650
651 return pszString;
652
653}
654
655
656/**
657 * Sets a registry string value, wide char variant.
658 *
659 * @returns See RegSetValueExA (errors are remembered in the state).
660 * @param pState The registry modifier state.
661 * @param hkey The key to add the value to.
662 * @param pwszValueNm The value name. NULL for setting the default.
663 * @param pwszValue The value string.
664 * @param uLine The line we're called from.
665 */
666static LSTATUS vbpsSetRegValueWW(VBPSREGSTATE *pState, HKEY hkey, PCRTUTF16 pwszValueNm, PCRTUTF16 pwszValue, unsigned uLine)
667{
668 DWORD const cbValue = (DWORD)((RTUtf16Len(pwszValue) + 1) * sizeof(RTUTF16));
669 LSTATUS rc;
670 Assert(pState->fUpdate);
671
672 /*
673 * If we're not deleting the key prior to updating, we're in gentle update
674 * mode where we will query if the existing value matches the incoming one.
675 */
676 if (!pState->fDelete)
677 {
678 DWORD cbExistingData = cbValue + 128;
679 PRTUTF16 pwszExistingData = (PRTUTF16)alloca(cbExistingData);
680 DWORD dwExistingType;
681 rc = RegQueryValueExW(hkey, pwszValueNm, 0 /*Reserved*/, &dwExistingType, (BYTE *)pwszExistingData, &cbExistingData);
682 if (rc == ERROR_SUCCESS)
683 {
684 if ( dwExistingType == REG_SZ
685 && cbExistingData == cbValue)
686 {
687 if (memcmp(pwszValue, pwszExistingData, cbValue) == 0)
688 return ERROR_SUCCESS;
689 }
690 VBSP_LOG_VALUE_CHANGE(("vbpsSetRegValueWW: Value difference: dwExistingType=%d cbExistingData=%#x cbValue=%#x\n"
691 " hkey=%#x %ls; value name=%ls\n"
692 "existing: %.*Rhxs (%.*ls)\n"
693 " new: %.*Rhxs (%ls)\n",
694 dwExistingType, cbExistingData, cbValue,
695 hkey, vbpsDebugKeyToWSZ(hkey), pwszValueNm ? pwszValueNm : L"(default)",
696 cbExistingData, pwszExistingData, cbExistingData / sizeof(RTUTF16), pwszExistingData,
697 cbValue, pwszValue, pwszValue));
698 }
699 else
700 Assert(rc == ERROR_FILE_NOT_FOUND || rc == ERROR_MORE_DATA);
701 }
702
703 /*
704 * Set the value.
705 */
706 rc = RegSetValueExW(hkey, pwszValueNm, 0 /*Reserved*/, REG_SZ, (const BYTE *)pwszValue, cbValue);
707 if (rc == ERROR_SUCCESS)
708 {
709 VBSP_LOG_SET_VALUE(("vbpsSetRegValueWW: %ls/%ls=%ls (at %d)\n",
710 vbpsDebugKeyToWSZ(hkey), pwszValueNm ? pwszValueNm : L"(Default)", pwszValue, uLine));
711 return ERROR_SUCCESS;
712 }
713
714 AssertLogRelMsg(VBPS_LOGREL_NO_ASSERT(rc == ERROR_ACCESS_DENIED),
715 ("%d: '%ls'='%ls' -> %u\n", uLine, pwszValueNm, pwszValue, rc));
716 pState->rc = rc;
717 return rc;
718}
719
720
721/**
722 * Sets a registry string value.
723 *
724 * @returns See RegSetValueExA (errors are remembered in the state).
725 * @param pState The registry modifier state.
726 * @param hkey The key to add the value to.
727 * @param pszValueNm The value name. NULL for setting the default.
728 * @param pszValue The value string.
729 * @param uLine The line we're called from.
730 */
731static LSTATUS vbpsSetRegValueAA(VBPSREGSTATE *pState, HKEY hkey, const char *pszValueNm, const char *pszValue, unsigned uLine)
732{
733 DWORD const cbValue = (DWORD)strlen(pszValue) + 1;
734 LSTATUS rc;
735 Assert(pState->fUpdate);
736
737 /*
738 * If we're not deleting the key prior to updating, we're in gentle update
739 * mode where we will query if the existing value matches the incoming one.
740 */
741 if (!pState->fDelete)
742 {
743 DWORD cbExistingData = cbValue + 128;
744 char *pszExistingData = alloca(cbExistingData);
745 DWORD dwExistingType;
746 rc = RegQueryValueExA(hkey, pszValueNm, 0 /*Reserved*/, &dwExistingType, (PBYTE)pszExistingData, &cbExistingData);
747 if (rc == ERROR_SUCCESS)
748 {
749 if ( dwExistingType == REG_SZ
750 && cbExistingData == cbValue)
751 {
752 if (memcmp(pszValue, pszExistingData, cbValue) == 0)
753 return ERROR_SUCCESS;
754 if (memicmp(pszValue, pszExistingData, cbValue) == 0)
755 return ERROR_SUCCESS;
756 }
757 VBSP_LOG_VALUE_CHANGE(("vbpsSetRegValueAA: Value difference: dwExistingType=%d cbExistingData=%#x cbValue=%#x\n"
758 " hkey=%#x %ls; value name=%s\n"
759 "existing: %.*Rhxs (%.*s)\n"
760 " new: %.*Rhxs (%s)\n",
761 dwExistingType, cbExistingData, cbValue,
762 hkey, vbpsDebugKeyToWSZ(hkey), pszValueNm ? pszValueNm : "(default)",
763 cbExistingData, pszExistingData, cbExistingData, pszExistingData,
764 cbValue, pszValue, pszValue));
765 }
766 else
767 Assert(rc == ERROR_FILE_NOT_FOUND || rc == ERROR_MORE_DATA);
768 }
769
770 /*
771 * Set the value.
772 */
773 rc = RegSetValueExA(hkey, pszValueNm, 0 /*Reserved*/, REG_SZ, (PBYTE)pszValue, cbValue);
774 if (rc == ERROR_SUCCESS)
775 {
776 VBSP_LOG_SET_VALUE(("vbpsSetRegValueAA: %ls/%s=%s (at %d)\n",
777 vbpsDebugKeyToWSZ(hkey), pszValueNm ? pszValueNm : "(Default)", pszValue, uLine));
778 return ERROR_SUCCESS;
779 }
780
781 AssertLogRelMsg(VBPS_LOGREL_NO_ASSERT(rc == ERROR_ACCESS_DENIED),
782 ("%d: '%s'='%s' -> %u\n", uLine, pszValueNm, pszValue, rc));
783 pState->rc = rc;
784 return rc;
785}
786
787
788/**
789 * Closes a registry key.
790 *
791 * @returns See RegCloseKey (errors are remembered in the state).
792 * @param pState The registry modifier state.
793 * @param hkey The key to close.
794 * @param uLine The line we're called from.
795 */
796static LSTATUS vbpsCloseKey(VBPSREGSTATE *pState, HKEY hkey, unsigned uLine)
797{
798 LSTATUS rc = RegCloseKey(hkey);
799 if (rc == ERROR_SUCCESS)
800 return ERROR_SUCCESS;
801
802 AssertLogRelMsgFailed(("%d: close key -> %u\n", uLine, rc));
803 pState->rc = rc;
804 return rc;
805}
806
807
808/**
809 * Creates a registry key.
810 *
811 * @returns See RegCreateKeyA and RegSetValueExA (errors are remembered in the
812 * state).
813 * @param pState The registry modifier state.
814 * @param hkeyParent The parent key.
815 * @param pszKey The new key under @a hkeyParent.
816 * @param phkey Where to return the handle to the new key.
817 * @param uLine The line we're called from.
818 */
819static LSTATUS vbpsCreateRegKeyA(VBPSREGSTATE *pState, HKEY hkeyParent, const char *pszKey, PHKEY phkey, unsigned uLine)
820{
821 /*
822 * This will open if it exists and create if new, which is exactly what we want.
823 */
824 HKEY hNewKey;
825 DWORD dwDisposition = 0;
826 LSTATUS rc = RegCreateKeyExA(hkeyParent, pszKey, 0 /*Reserved*/, NULL /*pszClass*/, 0 /*fOptions*/,
827 pState->fSamBoth, NULL /*pSecAttr*/, &hNewKey, &dwDisposition);
828 if (rc == ERROR_SUCCESS)
829 {
830 *phkey = hNewKey;
831 if (dwDisposition == REG_CREATED_NEW_KEY)
832 VBSP_LOG_NEW_KEY(("vbpsCreateRegKeyA: %ls/%s (at %d)\n", vbpsDebugKeyToWSZ(hkeyParent), pszKey, uLine));
833 }
834 else
835 {
836 AssertLogRelMsg(VBPS_LOGREL_NO_ASSERT(rc == ERROR_ACCESS_DENIED),
837 ("%d: create key '%s' -> %u\n", uLine, pszKey, rc));
838 pState->rc = rc;
839 *phkey = NULL;
840 }
841 return rc;
842}
843
844
845/**
846 * Creates a registry key with a default string value.
847 *
848 * @returns See RegCreateKeyA and RegSetValueExA (errors are remembered in the
849 * state).
850 * @param pState The registry modifier state.
851 * @param hkeyParent The parent key.
852 * @param pszKey The new key under @a hkeyParent.
853 * @param pszValue The value string.
854 * @param uLine The line we're called from.
855 */
856static LSTATUS vbpsCreateRegKeyWithDefaultValueAA(VBPSREGSTATE *pState, HKEY hkeyParent, const char *pszKey,
857 const char *pszValue, unsigned uLine)
858{
859 HKEY hNewKey;
860 LSTATUS rc = vbpsCreateRegKeyA(pState, hkeyParent, pszKey, &hNewKey, uLine);
861 if (rc == ERROR_SUCCESS)
862 {
863 rc = vbpsSetRegValueAA(pState, hNewKey, NULL /*pszValueNm*/, pszValue, uLine);
864 vbpsCloseKey(pState, hNewKey, uLine);
865 }
866 else
867 {
868 AssertLogRelMsg(VBPS_LOGREL_NO_ASSERT(rc == ERROR_ACCESS_DENIED),
869 ("%d: create key '%s'(/Default='%s') -> %u\n", uLine, pszKey, pszValue, rc));
870 pState->rc = rc;
871 }
872 return rc;
873}
874
875
876/**
877 * Creates a registry key with a default wide string value.
878 *
879 * @returns See RegCreateKeyA and RegSetValueExA (errors are remembered in the
880 * state).
881 * @param pState The registry modifier state.
882 * @param hkeyParent The parent key.
883 * @param pszKey The new key under @a hkeyParent.
884 * @param pwszValue The value string.
885 * @param uLine The line we're called from.
886 */
887static LSTATUS vbpsCreateRegKeyWithDefaultValueAW(VBPSREGSTATE *pState, HKEY hkeyParent, const char *pszKey,
888 PCRTUTF16 pwszValue, unsigned uLine)
889{
890 HKEY hNewKey;
891 LSTATUS rc = vbpsCreateRegKeyA(pState, hkeyParent, pszKey, &hNewKey, uLine);
892 if (rc == ERROR_SUCCESS)
893 {
894 rc = vbpsSetRegValueWW(pState, hNewKey, NULL /*pwszValueNm*/, pwszValue, uLine);
895 vbpsCloseKey(pState, hNewKey, uLine);
896 }
897 else
898 {
899 AssertLogRelMsg(VBPS_LOGREL_NO_ASSERT(rc == ERROR_ACCESS_DENIED),
900 ("%d: create key '%s'(/Default='%ls') -> %u\n", uLine, pszKey, pwszValue, rc));
901 pState->rc = rc;
902 }
903 return rc;
904}
905
906
907/**
908 * Creates a registry key with a default string value, return the key.
909 *
910 * @returns See RegCreateKeyA and RegSetValueExA (errors are remembered in the
911 * state).
912 * @param pState The registry modifier state.
913 * @param hkeyParent The parent key.
914 * @param pszKey The new key under @a hkeyParent.
915 * @param pszValue The value string.
916 * @param phkey Where to return the handle to the new key.
917 * @param uLine The line we're called from.
918 */
919static LSTATUS vbpsCreateRegKeyWithDefaultValueAAEx(VBPSREGSTATE *pState, HKEY hkeyParent, const char *pszKey,
920 const char *pszValue, PHKEY phkey, unsigned uLine)
921{
922 HKEY hNewKey;
923 LSTATUS rc = vbpsCreateRegKeyA(pState, hkeyParent, pszKey, &hNewKey, uLine);
924 if (rc == ERROR_SUCCESS)
925 {
926 rc = vbpsSetRegValueAA(pState, hNewKey, NULL /*pszValueNm*/, pszValue, uLine);
927 *phkey = hNewKey;
928 }
929 else
930 {
931 AssertLogRelMsg(VBPS_LOGREL_NO_ASSERT(rc == ERROR_ACCESS_DENIED),
932 ("%d: create key '%s'(/Default='%s') -> %u\n", uLine, pszKey, pszValue, rc));
933 pState->rc = rc;
934 *phkey = NULL;
935 }
936 return rc;
937}
938
939
940/**
941 * Recursively deletes a registry key.
942 *
943 * @returns See SHDeleteKeyA (errors are remembered in the state).
944 * @param pState The registry modifier state.
945 * @param hkeyParent The parent key.
946 * @param pszKey The key under @a hkeyParent that should be
947 * deleted.
948 * @param uLine The line we're called from.
949 */
950static LSTATUS vbpsDeleteKeyRecursiveA(VBPSREGSTATE *pState, HKEY hkeyParent, const char *pszKey, unsigned uLine)
951{
952 LSTATUS rc;
953
954 Assert(pState->fDelete);
955 Assert(pszKey);
956 AssertReturn(*pszKey != '\0', pState->rc = ERROR_INVALID_PARAMETER);
957
958#ifdef VBSP_LOG_ENABLED
959 {
960 HKEY hkeyLog;
961 rc = RegOpenKeyExA(hkeyParent, pszKey, 0 /*fOptions*/, pState->fSamDelete, &hkeyLog);
962 if (rc != ERROR_FILE_NOT_FOUND)
963 VBSP_LOG_DEL_KEY(("vbpsDeleteKeyRecursiveA: %ls/%s (at %d)\n", vbpsDebugKeyToWSZ(hkeyParent), pszKey, uLine));
964 if (rc == ERROR_SUCCESS)
965 RegCloseKey(hkeyLog);
966 }
967#endif
968
969 rc = SHDeleteKeyA(hkeyParent, pszKey);
970 if (rc == ERROR_SUCCESS || rc == ERROR_FILE_NOT_FOUND)
971 return ERROR_SUCCESS;
972
973 AssertLogRelMsg(VBPS_LOGREL_NO_ASSERT(rc == ERROR_ACCESS_DENIED),
974 ("%d: delete key '%s' -> %u\n", uLine, pszKey, rc));
975 pState->rc = rc;
976 return rc;
977}
978
979
980/**
981 * Recursively deletes a registry key, wide char version.
982 *
983 * @returns See SHDeleteKeyW (errors are remembered in the state).
984 * @param pState The registry modifier state.
985 * @param hkeyParent The parent key.
986 * @param pwszKey The key under @a hkeyParent that should be
987 * deleted.
988 * @param uLine The line we're called from.
989 */
990static LSTATUS vbpsDeleteKeyRecursiveW(VBPSREGSTATE *pState, HKEY hkeyParent, PCRTUTF16 pwszKey, unsigned uLine)
991{
992 LSTATUS rc;
993
994 Assert(pState->fDelete);
995 Assert(pwszKey);
996 AssertReturn(*pwszKey != '\0', pState->rc = ERROR_INVALID_PARAMETER);
997
998#ifdef VBSP_LOG_ENABLED
999 {
1000 HKEY hkeyLog;
1001 rc = RegOpenKeyExW(hkeyParent, pwszKey, 0 /*fOptions*/, pState->fSamDelete, &hkeyLog);
1002 if (rc != ERROR_FILE_NOT_FOUND)
1003 VBSP_LOG_DEL_KEY(("vbpsDeleteKeyRecursiveW: %ls/%ls (at %d)\n", vbpsDebugKeyToWSZ(hkeyParent), pwszKey, uLine));
1004 if (rc == ERROR_SUCCESS)
1005 RegCloseKey(hkeyLog);
1006 }
1007#endif
1008
1009 rc = SHDeleteKeyW(hkeyParent, pwszKey);
1010 if (rc == ERROR_SUCCESS || rc == ERROR_FILE_NOT_FOUND)
1011 return ERROR_SUCCESS;
1012
1013 AssertLogRelMsg(VBPS_LOGREL_NO_ASSERT(rc == ERROR_ACCESS_DENIED),
1014 ("%d: delete key '%ls' -> %u\n", uLine, pwszKey, rc));
1015 pState->rc = rc;
1016 return rc;
1017}
1018
1019
1020/**
1021 * Register an application id.
1022 *
1023 * @returns Windows error code (errors are rememberd in the state).
1024 * @param pState The registry modifier state.
1025 * @param pszAppId The application UUID string.
1026 * @param pszDescription The description string.
1027 */
1028LSTATUS VbpsRegisterAppId(VBPSREGSTATE *pState, const char *pszAppId, const char *pszDescription)
1029{
1030 LSTATUS rc;
1031 HKEY hkeyAppIds;
1032 Assert(*pszAppId == '{');
1033
1034 /*
1035 * Delete.
1036 */
1037 if (pState->fDelete)
1038 {
1039 unsigned i = pState->cAltDeletes;
1040 while (i-- > 0)
1041 {
1042 rc = RegOpenKeyExW(pState->aAltDeletes[i].hkeyClasses, L"AppID", 0 /*fOptions*/, pState->fSamDelete, &hkeyAppIds);
1043 AssertLogRelMsgStmt(rc == ERROR_SUCCESS || rc == ERROR_FILE_NOT_FOUND, ("%u\n", rc), pState->rc = rc);
1044 if (rc == ERROR_SUCCESS)
1045 {
1046 vbpsDeleteKeyRecursiveA(pState, hkeyAppIds, pszAppId, __LINE__);
1047 vbpsCloseKey(pState, hkeyAppIds, __LINE__);
1048 }
1049 }
1050 }
1051
1052 if (pState->fUpdate)
1053 rc = RegCreateKeyExW(pState->hkeyClassesRootDst, L"AppID", 0 /*Reserved*/, NULL /*pszClass*/, 0 /*fOptions*/,
1054 pState->fSamBoth, NULL /*pSecAttr*/, &hkeyAppIds, NULL /*pdwDisposition*/);
1055
1056 else
1057 {
1058 rc = RegOpenKeyExW(pState->hkeyClassesRootDst, L"AppID", 0 /*fOptions*/, pState->fSamBoth, &hkeyAppIds);
1059 if (rc == ERROR_FILE_NOT_FOUND)
1060 return ERROR_SUCCESS;
1061 }
1062 AssertLogRelMsgReturn(rc == ERROR_SUCCESS, ("%u\n", rc), pState->rc = rc);
1063
1064 if (pState->fDelete)
1065 vbpsDeleteKeyRecursiveA(pState, hkeyAppIds, pszAppId, __LINE__);
1066
1067 /*
1068 * Update.
1069 */
1070 if (pState->fUpdate)
1071 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyAppIds, pszAppId, pszDescription, __LINE__);
1072
1073 vbpsCloseKey(pState, hkeyAppIds, __LINE__);
1074
1075 return pState->rc;
1076}
1077
1078
1079/**
1080 * Register an class name.
1081 *
1082 * @returns Windows error code (errors are rememberd in the state).
1083 * @param pState The registry modifier state.
1084 * @param pszClassName The name of the class.
1085 * @param pszDescription The description string
1086 * @param pClsId The UUID for the class.
1087 * @param pszCurVerSuffIfRootName This is the current version suffix to
1088 * append to @a pszClassName when
1089 * registering the version idependent name.
1090 */
1091LSTATUS VbpsRegisterClassName(VBPSREGSTATE *pState, const char *pszClassName, const char *pszDescription,
1092 const CLSID *pClsId, const char *pszCurVerSuffIfRootName)
1093{
1094 LSTATUS rc;
1095
1096 /*
1097 * Delete.
1098 */
1099 if (pState->fDelete)
1100 {
1101 unsigned i = pState->cAltDeletes;
1102 while (i-- > 0)
1103 vbpsDeleteKeyRecursiveA(pState, pState->aAltDeletes[i].hkeyClasses, pszClassName, __LINE__);
1104 vbpsDeleteKeyRecursiveA(pState, pState->hkeyClassesRootDst, pszClassName, __LINE__);
1105 }
1106
1107 /*
1108 * Update.
1109 */
1110 if (pState->fUpdate)
1111 {
1112 /* pszClassName/Default = description. */
1113 HKEY hkeyClass;
1114 rc = vbpsCreateRegKeyWithDefaultValueAAEx(pState, pState->hkeyClassesRootDst, pszClassName, pszDescription,
1115 &hkeyClass, __LINE__);
1116 if (rc == ERROR_SUCCESS)
1117 {
1118 char szClsId[CURLY_UUID_STR_BUF_SIZE];
1119
1120 /* CLSID/Default = pClsId. */
1121 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyClass, "CLSID", vbpsFormatUuidInCurly(szClsId, pClsId), __LINE__);
1122
1123 /* CurVer/Default = pszClassName+Suffix. */
1124 if (pszCurVerSuffIfRootName != NULL)
1125 {
1126 char szCurClassNameVer[128];
1127 rc = RTStrCopy(szCurClassNameVer, sizeof(szCurClassNameVer), pszClassName);
1128 if (RT_SUCCESS(rc))
1129 rc = RTStrCat(szCurClassNameVer, sizeof(szCurClassNameVer), pszCurVerSuffIfRootName);
1130 AssertStmt(RT_SUCCESS(rc), pState->rc = rc = ERROR_INVALID_DATA);
1131 if (rc == ERROR_SUCCESS)
1132 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyClass, "CurVer", szCurClassNameVer, __LINE__);
1133 }
1134
1135 vbpsCloseKey(pState, hkeyClass, __LINE__);
1136 }
1137 }
1138
1139 return pState->rc;
1140}
1141
1142
1143/**
1144 * Registers a class ID.
1145 *
1146 * @returns Windows error code (errors are rememberd in the state).
1147 * @param pState The registry modifier state.
1148 * @param pClsId The UUID for the class.
1149 * @param pszDescription The description string.
1150 * @param pszAppId The application ID.
1151 * @param pszClassName The version idependent class name.
1152 * @param pszCurClassNameVerSuffix The suffix to add to @a pszClassName for
1153 * the current version.
1154 * @param pTypeLibId The UUID for the typelib this class
1155 * belongs to.
1156 * @param pszServerType The server type (InprocServer32 or
1157 * LocalServer32).
1158 * @param pwszVBoxDir The VirtualBox install directory
1159 * (unicode), trailing slash.
1160 * @param pszServerSubPath What to append to @a pwszVBoxDir to
1161 * construct the server module name.
1162 * @param pszThreadingModel The threading model for inproc servers,
1163 * NULL for local servers.
1164 */
1165LSTATUS VbpsRegisterClassId(VBPSREGSTATE *pState, const CLSID *pClsId, const char *pszDescription, const char *pszAppId,
1166 const char *pszClassName, const char *pszCurClassNameVerSuffix, const CLSID *pTypeLibId,
1167 const char *pszServerType, PCRTUTF16 pwszVBoxDir, const char *pszServerSubPath,
1168 const char *pszThreadingModel)
1169{
1170 LSTATUS rc;
1171 char szClsId[CURLY_UUID_STR_BUF_SIZE];
1172 RT_NOREF(pszAppId);
1173
1174 Assert(!pszAppId || *pszAppId == '{');
1175 Assert((pwszVBoxDir == NULL && !pState->fUpdate) || pwszVBoxDir[RTUtf16Len(pwszVBoxDir) - 1] == '\\');
1176
1177 /*
1178 * We need this, whatever we end up having to do.
1179 */
1180 vbpsFormatUuidInCurly(szClsId, pClsId);
1181
1182 /*
1183 * Delete.
1184 */
1185 if (pState->fDelete)
1186 {
1187 unsigned i = pState->cAltDeletes;
1188 while (i-- > 0)
1189 if (pState->aAltDeletes[i].hkeyClsid != NULL)
1190 vbpsDeleteKeyRecursiveA(pState, pState->aAltDeletes[i].hkeyClsid, szClsId, __LINE__);
1191 vbpsDeleteKeyRecursiveA(pState, pState->hkeyClsidRootDst, szClsId, __LINE__);
1192 }
1193
1194 /*
1195 * Update.
1196 */
1197 if (pState->fUpdate)
1198 {
1199 HKEY hkeyClass;
1200 rc = vbpsCreateRegKeyWithDefaultValueAAEx(pState, pState->hkeyClsidRootDst, szClsId, pszDescription,
1201 &hkeyClass, __LINE__);
1202 if (rc == ERROR_SUCCESS)
1203 {
1204 HKEY hkeyServerType;
1205 char szCurClassNameVer[128];
1206
1207 /* pszServerType/Default = module. */
1208 rc = vbpsCreateRegKeyA(pState, hkeyClass, pszServerType, &hkeyServerType, __LINE__);
1209 if (rc == ERROR_SUCCESS)
1210 {
1211 RTUTF16 wszModule[MAX_PATH * 2];
1212 PRTUTF16 pwszCur = wszModule;
1213 bool fQuoteIt = strcmp(pszServerType, "LocalServer32") == 0;
1214 if (fQuoteIt)
1215 *pwszCur++ = '"';
1216
1217 rc = RTUtf16Copy(pwszCur, MAX_PATH, pwszVBoxDir); AssertRC(rc);
1218 pwszCur += RTUtf16Len(pwszCur);
1219 rc = RTUtf16CopyAscii(pwszCur, MAX_PATH - 3, pszServerSubPath); AssertRC(rc);
1220 pwszCur += RTUtf16Len(pwszCur);
1221
1222 if (fQuoteIt)
1223 *pwszCur++ = '"';
1224 *pwszCur++ = '\0'; /* included, so ++. */
1225
1226 vbpsSetRegValueWW(pState, hkeyServerType, NULL /*pszValueNm*/, wszModule, __LINE__);
1227
1228 /* pszServerType/ThreadingModel = pszThreading Model. */
1229 if (pszThreadingModel)
1230 vbpsSetRegValueAA(pState, hkeyServerType, "ThreadingModel", pszThreadingModel, __LINE__);
1231
1232 vbpsCloseKey(pState, hkeyServerType, __LINE__);
1233 }
1234
1235 /* ProgId/Default = pszClassName + pszCurClassNameVerSuffix. */
1236 if (pszClassName)
1237 {
1238 rc = RTStrCopy(szCurClassNameVer, sizeof(szCurClassNameVer), pszClassName);
1239 if (RT_SUCCESS(rc))
1240 rc = RTStrCat(szCurClassNameVer, sizeof(szCurClassNameVer), pszCurClassNameVerSuffix);
1241 AssertStmt(RT_SUCCESS(rc), pState->rc = rc = ERROR_INVALID_DATA);
1242 if (rc == ERROR_SUCCESS)
1243 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyClass, "ProgId", szCurClassNameVer, __LINE__);
1244
1245 /* VersionIndependentProgID/Default = pszClassName. */
1246 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyClass, "VersionIndependentProgID", pszClassName, __LINE__);
1247 }
1248
1249 /* TypeLib/Default = pTypeLibId. */
1250 if (pTypeLibId)
1251 {
1252 char szTypeLibId[CURLY_UUID_STR_BUF_SIZE];
1253 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyClass, "TypeLib",
1254 vbpsFormatUuidInCurly(szTypeLibId, pTypeLibId), __LINE__);
1255 }
1256
1257 vbpsCloseKey(pState, hkeyClass, __LINE__);
1258 }
1259 }
1260
1261 return pState->rc;
1262}
1263
1264
1265/**
1266 * Register modules and classes from the VirtualBox.xidl file.
1267 *
1268 * @returns COM status code.
1269 * @param pwszVBoxDir The VirtualBox application directory.
1270 * @param fIs32On64 Set if this is the 32-bit on 64-bit component.
1271 *
1272 * @todo convert to XSLT.
1273 */
1274void RegisterXidlModulesAndClassesGenerated(VBPSREGSTATE *pState, PCRTUTF16 pwszVBoxDir, bool fIs32On64)
1275{
1276 const char *pszAppId = "{819B4D85-9CEE-493C-B6FC-64FFE759B3C9}";
1277 const char *pszInprocDll = !fIs32On64 ? "VBoxC.dll" : "x86\\VBoxClient-x86.dll";
1278
1279 VbpsRegisterAppId(pState, pszAppId, "VirtualBox Application");
1280
1281 /* VBoxSVC */
1282 VbpsRegisterClassName(pState, "VirtualBox.VirtualBox.1", "VirtualBox Class", &CLSID_VirtualBox, NULL);
1283 VbpsRegisterClassName(pState, "VirtualBox.VirtualBox", "VirtualBox Class", &CLSID_VirtualBox, ".1");
1284 VbpsRegisterClassId(pState, &CLSID_VirtualBox, "VirtualBox Class", pszAppId, "VirtualBox.VirtualBox", ".1",
1285 &LIBID_VirtualBox, "LocalServer32", pwszVBoxDir, "VBoxSVC.exe", NULL /*N/A*/);
1286 /* VBoxC */
1287 VbpsRegisterClassName(pState, "VirtualBox.Session.1", "Session Class", &CLSID_Session, NULL);
1288 VbpsRegisterClassName(pState, "VirtualBox.Session", "Session Class", &CLSID_Session, ".1");
1289 VbpsRegisterClassId(pState, &CLSID_Session, "Session Class", pszAppId, "VirtualBox.Session", ".1",
1290 &LIBID_VirtualBox, "InprocServer32", pwszVBoxDir, pszInprocDll, "Free");
1291
1292 VbpsRegisterClassName(pState, "VirtualBox.VirtualBoxClient.1", "VirtualBoxClient Class", &CLSID_VirtualBoxClient, NULL);
1293 VbpsRegisterClassName(pState, "VirtualBox.VirtualBoxClient", "VirtualBoxClient Class", &CLSID_VirtualBoxClient, ".1");
1294 VbpsRegisterClassId(pState, &CLSID_VirtualBoxClient, "VirtualBoxClient Class", pszAppId,
1295 "VirtualBox.VirtualBoxClient", ".1",
1296 &LIBID_VirtualBox, "InprocServer32", pwszVBoxDir, pszInprocDll, "Free");
1297}
1298
1299
1300/**
1301 * Updates the VBox type lib registration.
1302 *
1303 * This is only used when updating COM registrations during com::Initialize.
1304 * For normal registration and unregistrations we use the RegisterTypeLib and
1305 * UnRegisterTypeLib APIs.
1306 *
1307 * @param pState The registry modifier state.
1308 * @param pwszVBoxDir The VirtualBox install directory (unicode),
1309 * trailing slash.
1310 * @param fIs32On64 Set if we're registering the 32-bit proxy stub
1311 * on a 64-bit system.
1312 */
1313static void vbpsUpdateTypeLibRegistration(VBPSREGSTATE *pState, PCRTUTF16 pwszVBoxDir, bool fIs32On64)
1314{
1315 const char * const pszTypeLibDll = VBPS_PROXY_STUB_FILE(fIs32On64);
1316#if ARCH_BITS == 32 && !defined(VBOX_IN_32_ON_64_MAIN_API)
1317 const char * const pszWinXx = "win32";
1318#else
1319 const char * const pszWinXx = !fIs32On64 ? "win64" : "win32";
1320#endif
1321 const char * const pszDescription = "VirtualBox Type Library";
1322
1323 char szTypeLibId[CURLY_UUID_STR_BUF_SIZE];
1324 HKEY hkeyTypeLibs;
1325 HKEY hkeyTypeLibId;
1326 LSTATUS rc;
1327
1328 Assert(pState->fUpdate && !pState->fDelete);
1329
1330 /*
1331 * Type library registration (w/o interfaces).
1332 */
1333
1334 /* Open Classes/TypeLib/. */
1335 rc = vbpsCreateRegKeyA(pState, pState->hkeyClassesRootDst, "TypeLib", &hkeyTypeLibs, __LINE__);
1336 if (rc != ERROR_SUCCESS)
1337 return;
1338
1339 /* Create TypeLib/{UUID}. */
1340 rc = vbpsCreateRegKeyA(pState, hkeyTypeLibs, vbpsFormatUuidInCurly(szTypeLibId, &LIBID_VirtualBox), &hkeyTypeLibId, __LINE__);
1341 if (rc == ERROR_SUCCESS)
1342 {
1343 /* {UUID}/Major.Minor/Default = pszDescription. */
1344 HKEY hkeyMajMin;
1345 char szMajMin[64];
1346 sprintf(szMajMin, "%u.%u", kTypeLibraryMajorVersion, kTypeLibraryMinorVersion);
1347 rc = vbpsCreateRegKeyWithDefaultValueAAEx(pState, hkeyTypeLibId, szMajMin, pszDescription, &hkeyMajMin, __LINE__);
1348 if (rc == ERROR_SUCCESS)
1349 {
1350 RTUTF16 wszBuf[MAX_PATH * 2];
1351
1352 /* {UUID}/Major.Minor/0. */
1353 HKEY hkey0;
1354 rc = vbpsCreateRegKeyA(pState, hkeyMajMin, "0", &hkey0, __LINE__);
1355 if (rc == ERROR_SUCCESS)
1356 {
1357 /* {UUID}/Major.Minor/0/winXX/Default = VBoxProxyStub. */
1358 rc = RTUtf16Copy(wszBuf, MAX_PATH, pwszVBoxDir); AssertRC(rc);
1359 rc = RTUtf16CatAscii(wszBuf, MAX_PATH * 2, pszTypeLibDll); AssertRC(rc);
1360
1361 vbpsCreateRegKeyWithDefaultValueAW(pState, hkey0, pszWinXx, wszBuf, __LINE__);
1362 vbpsCloseKey(pState, hkey0, __LINE__);
1363 }
1364
1365 /* {UUID}/Major.Minor/FLAGS */
1366 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyMajMin, "FLAGS", "0", __LINE__);
1367
1368 /* {UUID}/Major.Minor/HELPDIR */
1369 rc = RTUtf16Copy(wszBuf, MAX_PATH, pwszVBoxDir); AssertRC(rc);
1370#if 0 /* MSI: trailing slash; regsvr32/comregister: strip unnecessary trailing slash. Go with MSI to avoid user issues. */
1371 {
1372 size_t off = RTUtf16Len(wszBuf);
1373 while (off > 2 && wszBuf[off - 2] != ':' && RTPATH_IS_SLASH(wszBuf[off - 1]))
1374 off--;
1375 wszBuf[off] = '\0';
1376 }
1377#endif
1378 vbpsCreateRegKeyWithDefaultValueAW(pState, hkeyMajMin, "HELPDIR", wszBuf, __LINE__);
1379
1380 vbpsCloseKey(pState, hkeyMajMin, __LINE__);
1381 }
1382 vbpsCloseKey(pState, hkeyTypeLibId, __LINE__);
1383 }
1384 vbpsCloseKey(pState, hkeyTypeLibs, __LINE__);
1385}
1386
1387
1388/**
1389 * Update the VBox proxy stub registration.
1390 *
1391 * This is only used when updating COM registrations during com::Initialize.
1392 * For normal registration and unregistrations we use the NdrDllRegisterProxy
1393 * and NdrDllUnregisterProxy.
1394 *
1395 * @param pState The registry modifier state.
1396 * @param pwszVBoxDir The VirtualBox install directory (unicode),
1397 * trailing slash.
1398 * @param fIs32On64 Set if we're registering the 32-bit proxy stub
1399 * on a 64-bit system.
1400 */
1401static void vbpsUpdateProxyStubRegistration(VBPSREGSTATE *pState, PCRTUTF16 pwszVBoxDir, bool fIs32On64)
1402{
1403 /*
1404 * Register the proxy stub factory class ID.
1405 * It's simple compared to the VBox classes, thus all the NULL parameters.
1406 */
1407 const char *pszPsDll = VBPS_PROXY_STUB_FILE(fIs32On64);
1408 Assert(pState->fUpdate && !pState->fDelete);
1409 VbpsRegisterClassId(pState, &g_ProxyClsId, "PSFactoryBuffer", NULL /*pszAppId*/,
1410 NULL /*pszClassName*/, NULL /*pszCurClassNameVerSuffix*/, NULL /*pTypeLibId*/,
1411 "InprocServer32", pwszVBoxDir, pszPsDll, "Both");
1412}
1413
1414
1415/**
1416 * Updates the VBox interface registrations.
1417 *
1418 * This is only used when updating COM registrations during com::Initialize.
1419 * For normal registration and unregistrations we use the NdrDllRegisterProxy
1420 * and NdrDllUnregisterProxy.
1421 *
1422 * @param pState The registry modifier state.
1423 * @param pwszVBoxDir The VirtualBox install directory (unicode),
1424 * trailing slash.
1425 * @param fIs32On64 Set if we're registering the 32-bit proxy stub
1426 * on a 64-bit system.
1427 */
1428static void vbpsUpdateInterfaceRegistrations(VBPSREGSTATE *pState)
1429{
1430 const ProxyFileInfo **ppProxyFile = &g_apProxyFiles[0];
1431 const ProxyFileInfo *pProxyFile;
1432 LSTATUS rc;
1433 char szProxyClsId[CURLY_UUID_STR_BUF_SIZE];
1434 char szTypeLibId[CURLY_UUID_STR_BUF_SIZE];
1435 char szTypeLibVersion[64];
1436
1437 vbpsFormatUuidInCurly(szProxyClsId, &g_ProxyClsId);
1438 vbpsFormatUuidInCurly(szTypeLibId, &LIBID_VirtualBox);
1439 sprintf(szTypeLibVersion, "%u.%u", kTypeLibraryMajorVersion, kTypeLibraryMinorVersion);
1440
1441 Assert(pState->fUpdate && !pState->fDelete);
1442 rc = vbpsRegOpenInterfaceKeys(pState);
1443 if (rc != ERROR_SUCCESS)
1444 return;
1445
1446 /*
1447 * We walk the proxy file list (even if we only have one).
1448 */
1449 while ((pProxyFile = *ppProxyFile++) != NULL)
1450 {
1451 const PCInterfaceStubVtblList * const papStubVtbls = pProxyFile->pStubVtblList;
1452 const char * const *papszNames = pProxyFile->pNamesArray;
1453 unsigned iIf = pProxyFile->TableSize;
1454 AssertStmt(iIf < 1024, iIf = 0);
1455 Assert(pProxyFile->TableVersion == 2);
1456
1457 /*
1458 * Walk the interfaces in that file, picking data from the various tables.
1459 */
1460 while (iIf-- > 0)
1461 {
1462 char szIfId[CURLY_UUID_STR_BUF_SIZE];
1463 const char * const pszIfNm = papszNames[iIf];
1464 size_t const cchIfNm = RT_VALID_PTR(pszIfNm) ? strlen(pszIfNm) : 0;
1465 char szMethods[32];
1466 uint32_t const cMethods = papStubVtbls[iIf]->header.DispatchTableCount;
1467 HKEY hkeyIfId;
1468
1469 AssertReturnVoidStmt(cchIfNm >= 3 && cchIfNm <= 72, pState->rc = ERROR_INVALID_DATA);
1470
1471 AssertReturnVoidStmt(cMethods >= 3 && cMethods < 1024, pState->rc = ERROR_INVALID_DATA);
1472 sprintf(szMethods, "%u", cMethods);
1473
1474 rc = vbpsCreateRegKeyWithDefaultValueAAEx(pState, pState->hkeyInterfaceRootDst,
1475 vbpsFormatUuidInCurly(szIfId, papStubVtbls[iIf]->header.piid),
1476 pszIfNm, &hkeyIfId, __LINE__);
1477 if (rc == ERROR_SUCCESS)
1478 {
1479 HKEY hkeyTypeLib;
1480 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyIfId, "ProxyStubClsid32", szProxyClsId, __LINE__);
1481 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyIfId, "NumMethods", szMethods, __LINE__);
1482
1483 /* The MSI seems to still be putting TypeLib keys here. So, let's do that too. */
1484 rc = vbpsCreateRegKeyWithDefaultValueAAEx(pState, hkeyIfId, "TypeLib", szTypeLibId, &hkeyTypeLib, __LINE__);
1485 if (rc == ERROR_SUCCESS)
1486 {
1487 vbpsSetRegValueAA(pState, hkeyTypeLib, "Version", szTypeLibVersion, __LINE__);
1488 vbpsCloseKey(pState, hkeyTypeLib, __LINE__);
1489 }
1490
1491 vbpsCloseKey(pState, hkeyIfId, __LINE__);
1492 }
1493 }
1494 }
1495}
1496
1497
1498static bool vbpsIsUpToDate(VBPSREGSTATE *pState)
1499{
1500 /** @todo read some registry key and */
1501 NOREF(pState);
1502 return false;
1503}
1504
1505static bool vbpsMarkUpToDate(VBPSREGSTATE *pState)
1506{
1507 /** @todo write the key vbpsIsUpToDate uses, if pState indicates success. */
1508 NOREF(pState);
1509 return false;
1510}
1511
1512
1513
1514/**
1515 * Strips the stub dll name and any x86 subdir off the full DLL path to get a
1516 * path to the VirtualBox application directory.
1517 *
1518 * @param pwszDllPath The path to strip, returns will end with a slash.
1519 */
1520static void vbpsDllPathToVBoxDir(PRTUTF16 pwszDllPath)
1521{
1522 RTUTF16 wc;
1523 size_t off = RTUtf16Len(pwszDllPath);
1524 while ( off > 0
1525 && ( (wc = pwszDllPath[off - 1]) >= 127U
1526 || !RTPATH_IS_SEP((unsigned char)wc)))
1527 off--;
1528
1529#ifdef VBOX_IN_32_ON_64_MAIN_API
1530 /*
1531 * The -x86 variant is in a x86 subdirectory, drop it.
1532 */
1533 while ( off > 0
1534 && ( (wc = pwszDllPath[off - 1]) < 127U
1535 && RTPATH_IS_SEP((unsigned char)wc)))
1536 off--;
1537 while ( off > 0
1538 && ( (wc = pwszDllPath[off - 1]) >= 127U
1539 || !RTPATH_IS_SEP((unsigned char)wc)))
1540 off--;
1541#endif
1542 pwszDllPath[off] = '\0';
1543}
1544
1545
1546/**
1547 * Wrapper around RegisterXidlModulesAndClassesGenerated for the convenience of
1548 * the standard registration entry points.
1549 *
1550 * @returns COM status code.
1551 * @param pwszVBoxDir The VirtualBox install directory (unicode),
1552 * trailing slash.
1553 * @param fDelete Whether to delete registration keys and values.
1554 * @param fUpdate Whether to update registration keys and values.
1555 */
1556HRESULT RegisterXidlModulesAndClasses(PRTUTF16 pwszVBoxDir, bool fDelete, bool fUpdate)
1557{
1558#ifdef VBOX_IN_32_ON_64_MAIN_API
1559 bool const fIs32On64 = true;
1560#else
1561 bool const fIs32On64 = false;
1562#endif
1563 VBPSREGSTATE State;
1564 LSTATUS rc;
1565
1566 /*
1567 * Do registration for the current execution mode of the DLL.
1568 */
1569 rc = vbpsRegInit(&State, HKEY_CLASSES_ROOT, NULL /* Alt: HKEY_LOCAL_MACHINE, "Software\\Classes", */, fDelete, fUpdate, 0);
1570 if (rc == ERROR_SUCCESS)
1571 {
1572 if (!fUpdate)
1573 {
1574 /* When only unregistering, really purge everything twice or trice. :-) */
1575 vbpsRegAddAltDelete(&State, HKEY_LOCAL_MACHINE, "Software\\Classes");
1576 vbpsRegAddAltDelete(&State, HKEY_CURRENT_USER, "Software\\Classes");
1577 vbpsRegAddAltDelete(&State, HKEY_CLASSES_ROOT, NULL);
1578 }
1579
1580 RegisterXidlModulesAndClassesGenerated(&State, pwszVBoxDir, fIs32On64);
1581 rc = State.rc;
1582 }
1583 vbpsRegTerm(&State);
1584
1585 /*
1586 * Translate error code? Return.
1587 */
1588 if (rc == ERROR_SUCCESS)
1589 return S_OK;
1590 return E_FAIL;
1591}
1592
1593
1594/**
1595 * Checks if the string matches any of our type library versions.
1596 *
1597 * @returns true on match, false on mismatch.
1598 * @param pwszTypeLibVersion The type library version string.
1599 */
1600DECLINLINE(bool) vbpsIsTypeLibVersionToRemove(PCRTUTF16 pwszTypeLibVersion)
1601{
1602 AssertCompile(RT_ELEMENTS(g_apwszTypelibVersions) == 2);
1603
1604 /* ASSUMES: 1.x version strings and that the input buffer is at least 3 wchars long. */
1605 if ( g_apwszTypelibVersions[0][3] == pwszTypeLibVersion[3]
1606 && RTUtf16Cmp(g_apwszTypelibVersions[0], pwszTypeLibVersion) == 0)
1607 return true;
1608 if ( g_apwszTypelibVersions[1][3] == pwszTypeLibVersion[3]
1609 && RTUtf16Cmp(g_apwszTypelibVersions[1], pwszTypeLibVersion) == 0)
1610 return true;
1611
1612 return false;
1613}
1614
1615
1616/**
1617 * Quick check whether the given string looks like a UUID in braces.
1618 *
1619 * This does not check the whole string, just do a quick sweep.
1620 *
1621 * @returns true if possible UUID, false if definitely not.
1622 * @param pwszUuid Alleged UUID in braces.
1623 */
1624DECLINLINE(bool) vbpsIsUuidInBracesQuickW(PCRTUTF16 pwszUuid)
1625{
1626 return pwszUuid[ 0] == '{'
1627 && pwszUuid[ 9] == '-'
1628 && pwszUuid[14] == '-'
1629 && pwszUuid[19] == '-'
1630 && pwszUuid[24] == '-'
1631 && pwszUuid[37] == '}'
1632 && pwszUuid[38] == '\0'
1633 && RT_C_IS_XDIGIT(pwszUuid[1]);
1634}
1635
1636
1637/**
1638 * Compares two UUIDs (in braces).
1639 *
1640 * @returns true on match, false if no match.
1641 * @param pwszUuid1 The first UUID.
1642 * @param pwszUuid2 The second UUID.
1643 */
1644static bool vbpsCompareUuidW(PCRTUTF16 pwszUuid1, PCRTUTF16 pwszUuid2)
1645{
1646#define COMPARE_EXACT_RET(a_wch1, a_wch2) \
1647 if ((a_wch1) == (a_wch2)) { } else return false
1648
1649#define COMPARE_XDIGITS_RET(a_wch1, a_wch2) \
1650 if ((a_wch1) == (a_wch2)) { } \
1651 else if (RT_C_TO_UPPER(a_wch1) != RT_C_TO_UPPER(a_wch2) || (a_wch1) >= 127U || (a_wch2) >= 127U) \
1652 return false
1653 COMPARE_EXACT_RET( pwszUuid1[ 0], pwszUuid2[ 0]); /* { */
1654 COMPARE_XDIGITS_RET(pwszUuid1[ 1], pwszUuid2[ 1]); /* 5 */
1655 COMPARE_XDIGITS_RET(pwszUuid1[ 2], pwszUuid2[ 2]); /* e */
1656 COMPARE_XDIGITS_RET(pwszUuid1[ 3], pwszUuid2[ 3]); /* 5 */
1657 COMPARE_XDIGITS_RET(pwszUuid1[ 4], pwszUuid2[ 4]); /* e */
1658 COMPARE_XDIGITS_RET(pwszUuid1[ 5], pwszUuid2[ 5]); /* 3 */
1659 COMPARE_XDIGITS_RET(pwszUuid1[ 6], pwszUuid2[ 6]); /* 6 */
1660 COMPARE_XDIGITS_RET(pwszUuid1[ 7], pwszUuid2[ 7]); /* 4 */
1661 COMPARE_XDIGITS_RET(pwszUuid1[ 8], pwszUuid2[ 8]); /* 0 */
1662 COMPARE_EXACT_RET( pwszUuid1[ 9], pwszUuid2[ 9]); /* - */
1663 COMPARE_XDIGITS_RET(pwszUuid1[10], pwszUuid2[10]); /* 7 */
1664 COMPARE_XDIGITS_RET(pwszUuid1[11], pwszUuid2[11]); /* 4 */
1665 COMPARE_XDIGITS_RET(pwszUuid1[12], pwszUuid2[12]); /* f */
1666 COMPARE_XDIGITS_RET(pwszUuid1[13], pwszUuid2[13]); /* 3 */
1667 COMPARE_EXACT_RET( pwszUuid1[14], pwszUuid2[14]); /* - */
1668 COMPARE_XDIGITS_RET(pwszUuid1[15], pwszUuid2[15]); /* 4 */
1669 COMPARE_XDIGITS_RET(pwszUuid1[16], pwszUuid2[16]); /* 6 */
1670 COMPARE_XDIGITS_RET(pwszUuid1[17], pwszUuid2[17]); /* 8 */
1671 COMPARE_XDIGITS_RET(pwszUuid1[18], pwszUuid2[18]); /* 9 */
1672 COMPARE_EXACT_RET( pwszUuid1[19], pwszUuid2[19]); /* - */
1673 COMPARE_XDIGITS_RET(pwszUuid1[20], pwszUuid2[20]); /* 9 */
1674 COMPARE_XDIGITS_RET(pwszUuid1[21], pwszUuid2[21]); /* 7 */
1675 COMPARE_XDIGITS_RET(pwszUuid1[22], pwszUuid2[22]); /* 9 */
1676 COMPARE_XDIGITS_RET(pwszUuid1[23], pwszUuid2[23]); /* f */
1677 COMPARE_EXACT_RET( pwszUuid1[24], pwszUuid2[24]); /* - */
1678 COMPARE_XDIGITS_RET(pwszUuid1[25], pwszUuid2[25]); /* 6 */
1679 COMPARE_XDIGITS_RET(pwszUuid1[26], pwszUuid2[26]); /* b */
1680 COMPARE_XDIGITS_RET(pwszUuid1[27], pwszUuid2[27]); /* 1 */
1681 COMPARE_XDIGITS_RET(pwszUuid1[28], pwszUuid2[28]); /* b */
1682 COMPARE_XDIGITS_RET(pwszUuid1[29], pwszUuid2[29]); /* 8 */
1683 COMPARE_XDIGITS_RET(pwszUuid1[30], pwszUuid2[30]); /* d */
1684 COMPARE_XDIGITS_RET(pwszUuid1[31], pwszUuid2[31]); /* 7 */
1685 COMPARE_XDIGITS_RET(pwszUuid1[32], pwszUuid2[32]); /* 6 */
1686 COMPARE_XDIGITS_RET(pwszUuid1[33], pwszUuid2[33]); /* 0 */
1687 COMPARE_XDIGITS_RET(pwszUuid1[34], pwszUuid2[34]); /* 9 */
1688 COMPARE_XDIGITS_RET(pwszUuid1[35], pwszUuid2[35]); /* a */
1689 COMPARE_XDIGITS_RET(pwszUuid1[36], pwszUuid2[36]); /* 5 */
1690 COMPARE_EXACT_RET( pwszUuid1[37], pwszUuid2[37]); /* } */
1691 COMPARE_EXACT_RET( pwszUuid1[38], pwszUuid2[38]); /* \0 */
1692#undef COMPARE_EXACT_RET
1693#undef COMPARE_XDIGITS_RET
1694 return true;
1695}
1696
1697
1698/**
1699 * Checks if the type library ID is one of the ones we wish to clean up.
1700 *
1701 * @returns true if it should be cleaned up, false if not.
1702 * @param pwszTypeLibId The type library ID as a bracketed string.
1703 */
1704DECLINLINE(bool) vbpsIsTypeLibIdToRemove(PRTUTF16 pwszTypeLibId)
1705{
1706 AssertCompile(RT_ELEMENTS(g_apwszTypeLibIds) == 2);
1707#ifdef VBOX_STRICT
1708 static bool s_fDoneStrict = false;
1709 if (s_fDoneStrict) { }
1710 else
1711 {
1712 Assert(RT_ELEMENTS(g_apwszTypeLibIds) == 2);
1713 Assert(g_apwszTypeLibIds[0][0] == '{');
1714 Assert(g_apwszTypeLibIds[1][0] == '{');
1715 Assert(RT_C_IS_XDIGIT(g_apwszTypeLibIds[0][1]));
1716 Assert(RT_C_IS_XDIGIT(g_apwszTypeLibIds[1][1]));
1717 Assert(RT_C_IS_UPPER(g_apwszTypeLibIds[0][1]) || RT_C_IS_DIGIT(g_apwszTypeLibIds[0][1]));
1718 Assert(RT_C_IS_UPPER(g_apwszTypeLibIds[1][1]) || RT_C_IS_DIGIT(g_apwszTypeLibIds[1][1]));
1719 s_fDoneStrict = true;
1720 }
1721#endif
1722
1723 /*
1724 * Rolled out matching with inlined check of the opening braces
1725 * and first two digits.
1726 *
1727 * ASSUMES input buffer is at least 3 wchars big and uppercased UUID in
1728 * our matching array.
1729 */
1730 if (pwszTypeLibId[0] == '{')
1731 {
1732 RTUTF16 const wcFirstDigit = RT_C_TO_UPPER(pwszTypeLibId[1]);
1733 RTUTF16 const wcSecondDigit = RT_C_TO_UPPER(pwszTypeLibId[2]);
1734 PCRTUTF16 pwsz2 = g_apwszTypeLibIds[0];
1735 if ( wcFirstDigit == pwsz2[1]
1736 && wcSecondDigit == pwsz2[2]
1737 && vbpsCompareUuidW(pwszTypeLibId, pwsz2))
1738 return true;
1739 pwsz2 = g_apwszTypeLibIds[1];
1740 if ( wcFirstDigit == pwsz2[1]
1741 && wcSecondDigit == pwsz2[2]
1742 && vbpsCompareUuidW(pwszTypeLibId, pwsz2))
1743 return true;
1744 }
1745 return false;
1746}
1747
1748
1749/**
1750 * Checks if the proxy stub class ID is one of the ones we wish to clean up.
1751 *
1752 * @returns true if it should be cleaned up, false if not.
1753 * @param pwszProxyStubId The proxy stub class ID.
1754 */
1755DECLINLINE(bool) vbpsIsProxyStubClsIdToRemove(PRTUTF16 pwszProxyStubId)
1756{
1757 AssertCompile(RT_ELEMENTS(g_apwszProxyStubClsIds) == 2);
1758#ifdef VBOX_STRICT
1759 static bool s_fDoneStrict = false;
1760 if (s_fDoneStrict) { }
1761 else
1762 {
1763 Assert(RT_ELEMENTS(g_apwszProxyStubClsIds) == 2);
1764 Assert(g_apwszProxyStubClsIds[0][0] == '{');
1765 Assert(g_apwszProxyStubClsIds[1][0] == '{');
1766 Assert(RT_C_IS_XDIGIT(g_apwszProxyStubClsIds[0][1]));
1767 Assert(RT_C_IS_XDIGIT(g_apwszProxyStubClsIds[1][1]));
1768 Assert(RT_C_IS_UPPER(g_apwszProxyStubClsIds[0][1]) || RT_C_IS_DIGIT(g_apwszProxyStubClsIds[0][1]));
1769 Assert(RT_C_IS_UPPER(g_apwszProxyStubClsIds[1][1]) || RT_C_IS_DIGIT(g_apwszProxyStubClsIds[1][1]));
1770 s_fDoneStrict = true;
1771 }
1772#endif
1773
1774 /*
1775 * Rolled out matching with inlined check of the opening braces
1776 * and first two digits.
1777 *
1778 * ASSUMES input buffer is at least 3 wchars big and uppercased UUID in
1779 * our matching array.
1780 */
1781 if (pwszProxyStubId[0] == '{')
1782 {
1783 RTUTF16 const wcFirstDigit = RT_C_TO_UPPER(pwszProxyStubId[1]);
1784 RTUTF16 const wcSecondDigit = RT_C_TO_UPPER(pwszProxyStubId[2]);
1785 PCRTUTF16 pwsz2 = g_apwszProxyStubClsIds[0];
1786 if ( wcFirstDigit == pwsz2[1]
1787 && wcSecondDigit == pwsz2[2]
1788 && vbpsCompareUuidW(pwszProxyStubId, pwsz2))
1789 return true;
1790 pwsz2 = g_apwszProxyStubClsIds[1];
1791 if ( wcFirstDigit == pwsz2[1]
1792 && wcSecondDigit == pwsz2[2]
1793 && vbpsCompareUuidW(pwszProxyStubId, pwsz2))
1794 return true;
1795 }
1796 return false;
1797}
1798
1799
1800/**
1801 * Hack to clean out the interfaces belonging to obsolete typelibs on
1802 * development boxes and such likes.
1803 */
1804static void vbpsRemoveOldInterfaces(VBPSREGSTATE *pState)
1805{
1806 unsigned iAlt = pState->cAltDeletes;
1807 while (iAlt-- > 0)
1808 {
1809 /*
1810 * Open the interface root key. Not using the vbpsRegOpenInterfaceKeys feature
1811 * here in case it messes things up by keeping the special HKEY_CLASSES_ROOT key
1812 * open with possibly pending deletes in parent views or other weird stuff.
1813 */
1814 HKEY hkeyInterfaces;
1815 LRESULT rc = RegOpenKeyExW(pState->aAltDeletes[iAlt].hkeyClasses, L"Interface",
1816 0 /*fOptions*/, pState->fSamDelete, &hkeyInterfaces);
1817 if (rc == ERROR_SUCCESS)
1818 {
1819 /*
1820 * This is kind of expensive, but we have to check all registered interfaces.
1821 * Only use wide APIs to avoid wasting time on string conversion.
1822 */
1823 DWORD idxKey;
1824 for (idxKey = 0;; idxKey++)
1825 {
1826 RTUTF16 wszCurNm[128 + 48];
1827 DWORD cwcCurNm = 128;
1828 rc = RegEnumKeyExW(hkeyInterfaces, idxKey, wszCurNm, &cwcCurNm,
1829 NULL /*pdwReserved*/, NULL /*pwszClass*/, NULL /*pcwcClass*/, NULL /*pLastWriteTime*/);
1830 if (rc == ERROR_SUCCESS)
1831 {
1832 /*
1833 * We match the interface by type library ID or proxy stub class ID.
1834 *
1835 * We have to check the proxy ID last, as it is almost always there
1836 * and we can safely skip it if there is a mismatching type lib
1837 * associated with the interface.
1838 */
1839 static RTUTF16 const s_wszTypeLib[] = L"\\TypeLib";
1840 bool fDeleteMe = false;
1841 HKEY hkeySub;
1842 RTUTF16 wszValue[128];
1843 DWORD cbValue;
1844 DWORD dwType;
1845
1846 /* Skip this entry if it doesn't look like a braced UUID. */
1847 wszCurNm[cwcCurNm] = '\0'; /* paranoia */
1848 if (vbpsIsUuidInBracesQuickW(wszCurNm)) { }
1849 else continue;
1850
1851 /* Try the TypeLib sub-key. */
1852 memcpy(&wszCurNm[cwcCurNm], s_wszTypeLib, sizeof(s_wszTypeLib));
1853 rc = RegOpenKeyExW(hkeyInterfaces, wszCurNm, 0 /*fOptions*/, KEY_QUERY_VALUE, &hkeySub);
1854 if (rc == ERROR_SUCCESS)
1855 {
1856 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
1857 rc = RegQueryValueExW(hkeySub, NULL /*pszValueNm*/, NULL /*pdwReserved*/,
1858 &dwType, (PBYTE)&wszValue[0], &cbValue);
1859 if (rc != ERROR_SUCCESS || dwType != REG_SZ)
1860 cbValue = 0;
1861 wszValue[cbValue / sizeof(RTUTF16)] = '\0';
1862
1863 if ( rc == ERROR_SUCCESS
1864 && vbpsIsTypeLibIdToRemove(wszValue))
1865 {
1866 /* Check the TypeLib/Version value to make sure. */
1867 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
1868 rc = RegQueryValueExW(hkeySub, L"Version", 0 /*pdwReserved*/, &dwType, (PBYTE)&wszValue[0], &cbValue);
1869 if (rc != ERROR_SUCCESS)
1870 cbValue = 0;
1871 wszValue[cbValue] = '\0';
1872
1873 if ( rc == ERROR_SUCCESS
1874 && vbpsIsTypeLibVersionToRemove(wszValue))
1875 fDeleteMe = true;
1876 }
1877 vbpsCloseKey(pState, hkeySub, __LINE__);
1878 }
1879 else if (rc == ERROR_FILE_NOT_FOUND)
1880 {
1881 /* No TypeLib, try the ProxyStubClsid32 sub-key next. */
1882 static RTUTF16 const s_wszProxyStubClsid32[] = L"\\ProxyStubClsid32";
1883 memcpy(&wszCurNm[cwcCurNm], s_wszProxyStubClsid32, sizeof(s_wszProxyStubClsid32));
1884 rc = RegOpenKeyExW(hkeyInterfaces, wszCurNm, 0 /*fOptions*/, KEY_QUERY_VALUE, &hkeySub);
1885 if (rc == ERROR_SUCCESS)
1886 {
1887 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
1888 rc = RegQueryValueExW(hkeySub, NULL /*pszValueNm*/, NULL /*pdwReserved*/,
1889 &dwType, (PBYTE)&wszValue[0], &cbValue);
1890 if (rc != ERROR_SUCCESS || dwType != REG_SZ)
1891 cbValue = 0;
1892 wszValue[cbValue / sizeof(RTUTF16)] = '\0';
1893
1894 if ( rc == ERROR_SUCCESS
1895 && vbpsIsProxyStubClsIdToRemove(wszValue))
1896 fDeleteMe = true;
1897
1898 vbpsCloseKey(pState, hkeySub, __LINE__);
1899 }
1900 }
1901
1902 if (fDeleteMe)
1903 {
1904 /*
1905 * Ok, it's an orphaned VirtualBox interface. Delete it.
1906 */
1907 wszCurNm[cwcCurNm] = '\0';
1908 vbpsDeleteKeyRecursiveW(pState, hkeyInterfaces, wszCurNm, __LINE__);
1909 }
1910 }
1911 else
1912 {
1913 Assert(rc == ERROR_NO_MORE_ITEMS);
1914 break;
1915 }
1916 }
1917
1918 vbpsCloseKey(pState, hkeyInterfaces, __LINE__);
1919 }
1920 }
1921}
1922
1923
1924/**
1925 * Hack to clean out the class IDs belonging to obsolete typelibs on development
1926 * boxes and such likes.
1927 */
1928static void vbpsRemoveOldClassIDs(VBPSREGSTATE *pState)
1929{
1930 unsigned iAlt = pState->cAltDeletes;
1931 while (iAlt-- > 0)
1932 {
1933 /*
1934 * Open the CLSID key if it exists.
1935 * We don't use the hKeyClsid member for the same paranoid reasons as
1936 * already stated in vbpsRemoveOldInterfaces.
1937 */
1938 HKEY hkeyClsIds;
1939 LRESULT rc;
1940 rc = RegOpenKeyExW(pState->aAltDeletes[iAlt].hkeyClasses, L"CLSID", 0 /*fOptions*/, pState->fSamDelete, &hkeyClsIds);
1941 if (rc == ERROR_SUCCESS)
1942 {
1943 /*
1944 * This is kind of expensive, but we have to check all registered interfaces.
1945 * Only use wide APIs to avoid wasting time on string conversion.
1946 */
1947 DWORD idxKey;
1948 for (idxKey = 0;; idxKey++)
1949 {
1950 RTUTF16 wszCurNm[128 + 48];
1951 DWORD cwcCurNm = 128;
1952 rc = RegEnumKeyExW(hkeyClsIds, idxKey, wszCurNm, &cwcCurNm,
1953 NULL /*pdwReserved*/, NULL /*pwszClass*/, NULL /*pcwcClass*/, NULL /*pLastWriteTime*/);
1954 if (rc == ERROR_SUCCESS)
1955 {
1956 /*
1957 * Match both the type library ID and the program ID.
1958 */
1959 static RTUTF16 const s_wszTypeLib[] = L"\\TypeLib";
1960 HKEY hkeySub;
1961 RTUTF16 wszValue[128];
1962 DWORD cbValue;
1963 DWORD dwType;
1964
1965
1966 /* Skip this entry if it doesn't look like a braced UUID. (Microsoft
1967 has one two malformed ones plus a hack.) */
1968 wszCurNm[cwcCurNm] = '\0'; /* paranoia */
1969 if (vbpsIsUuidInBracesQuickW(wszCurNm)) { }
1970 else continue;
1971
1972 /* The TypeLib sub-key. */
1973 memcpy(&wszCurNm[cwcCurNm], s_wszTypeLib, sizeof(s_wszTypeLib));
1974 rc = RegOpenKeyExW(hkeyClsIds, wszCurNm, 0 /*fOptions*/, KEY_QUERY_VALUE, &hkeySub);
1975 if (rc == ERROR_SUCCESS)
1976 {
1977 bool fDeleteMe = false;
1978
1979 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
1980 rc = RegQueryValueExW(hkeySub, NULL /*pszValueNm*/, NULL /*pdwReserved*/,
1981 &dwType, (PBYTE)&wszValue[0], &cbValue);
1982 if (rc != ERROR_SUCCESS || dwType != REG_SZ)
1983 cbValue = 0;
1984 wszValue[cbValue / sizeof(RTUTF16)] = '\0';
1985
1986 if ( rc == ERROR_SUCCESS
1987 && vbpsIsTypeLibIdToRemove(wszValue))
1988 fDeleteMe = true;
1989
1990 vbpsCloseKey(pState, hkeySub, __LINE__);
1991
1992 if (fDeleteMe)
1993 {
1994 /* The ProgId sub-key. */
1995 static RTUTF16 const s_wszProgId[] = L"\\ProgId";
1996 memcpy(&wszCurNm[cwcCurNm], s_wszProgId, sizeof(s_wszProgId));
1997 rc = RegOpenKeyExW(hkeyClsIds, wszCurNm, 0 /*fOptions*/, KEY_QUERY_VALUE, &hkeySub);
1998 if (rc == ERROR_SUCCESS)
1999 {
2000 static RTUTF16 const s_wszProgIdPrefix[] = L"VirtualBox.";
2001
2002 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
2003 rc = RegQueryValueExW(hkeySub, NULL /*pszValueNm*/, NULL /*pdwReserved*/,
2004 &dwType, (PBYTE)&wszValue[0], &cbValue);
2005 if (rc != ERROR_SUCCESS || dwType != REG_SZ)
2006 cbValue = 0;
2007 wszValue[cbValue / sizeof(RTUTF16)] = '\0';
2008
2009 if ( cbValue < sizeof(s_wszProgIdPrefix)
2010 || memcmp(wszValue, s_wszProgIdPrefix, sizeof(s_wszProgIdPrefix) - sizeof(RTUTF16)) != 0)
2011 fDeleteMe = false;
2012
2013 vbpsCloseKey(pState, hkeySub, __LINE__);
2014 }
2015 else
2016 AssertStmt(rc == ERROR_FILE_NOT_FOUND, fDeleteMe = false);
2017
2018 if (fDeleteMe)
2019 {
2020 /*
2021 * Ok, it's an orphaned VirtualBox interface. Delete it.
2022 */
2023 wszCurNm[cwcCurNm] = '\0';
2024 vbpsDeleteKeyRecursiveW(pState, hkeyClsIds, wszCurNm, __LINE__);
2025 }
2026 }
2027 }
2028 else
2029 Assert(rc == ERROR_FILE_NOT_FOUND);
2030 }
2031 else
2032 {
2033 Assert(rc == ERROR_NO_MORE_ITEMS);
2034 break;
2035 }
2036 }
2037
2038 vbpsCloseKey(pState, hkeyClsIds, __LINE__);
2039 }
2040 else
2041 Assert(rc == ERROR_FILE_NOT_FOUND);
2042 }
2043}
2044
2045
2046/**
2047 * Hack to clean obsolete typelibs on development boxes and such.
2048 */
2049static void vbpsRemoveOldTypeLibs(VBPSREGSTATE *pState)
2050{
2051 unsigned iAlt = pState->cAltDeletes;
2052 while (iAlt-- > 0)
2053 {
2054 /*
2055 * Open the TypeLib key, if it exists.
2056 */
2057 HKEY hkeyTypeLibs;
2058 LRESULT rc;
2059 rc = RegOpenKeyExW(pState->aAltDeletes[iAlt].hkeyClasses, L"TypeLib", 0 /*fOptions*/, pState->fSamDelete, &hkeyTypeLibs);
2060 if (rc == ERROR_SUCCESS)
2061 {
2062 /*
2063 * Look for our type library IDs.
2064 */
2065 unsigned iTlb = RT_ELEMENTS(g_apwszTypeLibIds);
2066 while (iTlb-- > 0)
2067 {
2068 HKEY hkeyTypeLibId;
2069 LONG rc = RegOpenKeyExW(hkeyTypeLibs, g_apwszTypeLibIds[iTlb], 0 /*fOptions*/, pState->fSamDelete, &hkeyTypeLibId);
2070 if (rc == ERROR_SUCCESS)
2071 {
2072 unsigned iVer = RT_ELEMENTS(g_apwszTypelibVersions);
2073 while (iVer-- > 0)
2074 {
2075 HKEY hkeyVer;
2076 rc = RegOpenKeyExW(hkeyTypeLibId, g_apwszTypelibVersions[iVer], 0, KEY_READ, &hkeyVer);
2077 if (rc == ERROR_SUCCESS)
2078 {
2079 char szValue[128];
2080 DWORD cbValue = sizeof(szValue) - 1;
2081 rc = RegQueryValueExA(hkeyVer, NULL, NULL, NULL, (PBYTE)&szValue[0], &cbValue);
2082 vbpsCloseKey(pState, hkeyVer, __LINE__);
2083 if (rc == ERROR_SUCCESS)
2084 {
2085 szValue[cbValue] = '\0';
2086 if (!strcmp(szValue, "VirtualBox Type Library"))
2087 {
2088 /*
2089 * Delete the type library version.
2090 * We do not delete the whole type library ID, just this version of it.
2091 */
2092 vbpsDeleteKeyRecursiveW(pState, hkeyTypeLibId, g_apwszTypelibVersions[iVer], __LINE__);
2093 }
2094 }
2095 }
2096 }
2097 vbpsCloseKey(pState, hkeyTypeLibId, __LINE__);
2098
2099 /*
2100 * The type library ID key should be empty now, so we can try remove it (non-recursively).
2101 */
2102 rc = RegDeleteKeyW(hkeyTypeLibs, g_apwszTypeLibIds[iTlb]);
2103 Assert(rc == ERROR_SUCCESS);
2104 }
2105 }
2106 }
2107 else
2108 Assert(rc == ERROR_FILE_NOT_FOUND);
2109 }
2110}
2111
2112
2113/**
2114 * Hack to clean out obsolete typelibs on development boxes and such.
2115 */
2116static void vbpsRemoveOldMessSub(REGSAM fSamWow)
2117{
2118 /*
2119 * Note! The worker procedures does not use the default destination,
2120 * because it's much much simpler to enumerate alternative locations.
2121 */
2122 VBPSREGSTATE State;
2123 LRESULT rc = vbpsRegInit(&State, HKEY_CLASSES_ROOT, NULL, true /*fDelete*/, false /*fUpdate*/, fSamWow);
2124 if (rc == ERROR_SUCCESS)
2125 {
2126 vbpsRegAddAltDelete(&State, HKEY_CURRENT_USER, "Software\\Classes");
2127 vbpsRegAddAltDelete(&State, HKEY_LOCAL_MACHINE, "Software\\Classes");
2128 vbpsRegAddAltDelete(&State, HKEY_CLASSES_ROOT, NULL);
2129
2130 vbpsRemoveOldInterfaces(&State);
2131 vbpsRemoveOldClassIDs(&State);
2132 vbpsRemoveOldTypeLibs(&State);
2133 }
2134 vbpsRegTerm(&State);
2135}
2136
2137
2138/**
2139 * Hack to clean out obsolete typelibs on development boxes and such.
2140 */
2141static void removeOldMess(void)
2142{
2143 vbpsRemoveOldMessSub(0 /*fSamWow*/);
2144#if ARCH_BITS == 64 || defined(VBOX_IN_32_ON_64_MAIN_API)
2145 vbpsRemoveOldMessSub(KEY_WOW64_32KEY);
2146#endif
2147}
2148
2149
2150
2151/**
2152 * Register the interfaces proxied by this DLL, and to avoid duplication and
2153 * minimize work the VBox type library, classes and servers are also registered.
2154 *
2155 * This is normally only used by developers via comregister.cmd and the heat.exe
2156 * tool during MSI creation. The only situation where users may end up here is
2157 * if they're playing around or we recommend it as a solution to COM problems.
2158 * So, no problem if this approach is less gentle, though we leave the cleaning
2159 * up of orphaned interfaces to DllUnregisterServer.
2160 *
2161 * @returns COM status code.
2162 */
2163HRESULT STDAPICALLTYPE DllRegisterServer(void)
2164{
2165 HRESULT hrc;
2166
2167 /*
2168 * Register the type library first.
2169 */
2170 ITypeLib *pITypeLib;
2171 WCHAR wszDllName[MAX_PATH];
2172 DWORD cwcRet = GetModuleFileNameW(g_hDllSelf, wszDllName, RT_ELEMENTS(wszDllName));
2173 AssertReturn(cwcRet > 0 && cwcRet < RT_ELEMENTS(wszDllName), CO_E_PATHTOOLONG);
2174
2175 hrc = LoadTypeLib(wszDllName, &pITypeLib);
2176 AssertMsgReturn(SUCCEEDED(hrc), ("%Rhrc\n", hrc), hrc);
2177 hrc = RegisterTypeLib(pITypeLib, wszDllName, NULL /*pszHelpDir*/);
2178 pITypeLib->lpVtbl->Release(pITypeLib);
2179 AssertMsgReturn(SUCCEEDED(hrc), ("%Rhrc\n", hrc), hrc);
2180
2181 /*
2182 * Register proxy stub.
2183 */
2184 hrc = NdrDllRegisterProxy(g_hDllSelf, &g_apProxyFiles[0], &g_ProxyClsId); /* see DLLREGISTRY_ROUTINES in RpcProxy.h */
2185 AssertMsgReturn(SUCCEEDED(hrc), ("%Rhrc\n", hrc), hrc);
2186
2187 /*
2188 * Register the VBox modules and classes.
2189 */
2190 vbpsDllPathToVBoxDir(wszDllName);
2191 hrc = RegisterXidlModulesAndClasses(wszDllName, true /*fDelete*/, true /*fUpdate*/);
2192 AssertMsgReturn(SUCCEEDED(hrc), ("%Rhrc\n", hrc), hrc);
2193
2194 return S_OK;
2195}
2196
2197
2198/**
2199 * Reverse of DllRegisterServer.
2200 *
2201 * This is normally only used by developers via comregister.cmd. Users may be
2202 * asked to perform it in order to fix some COM issue. So, it's OK if we spend
2203 * some extra time and clean up orphaned interfaces, because developer boxes
2204 * will end up with a bunch of those as interface UUIDs changes.
2205 *
2206 * @returns COM status code.
2207 */
2208HRESULT STDAPICALLTYPE DllUnregisterServer(void)
2209{
2210 HRESULT hrc = S_OK;
2211 HRESULT hrc2;
2212
2213 /*
2214 * Unregister the type library.
2215 *
2216 * We ignore TYPE_E_REGISTRYACCESS as that is what is returned if the
2217 * type lib hasn't been registered (W10).
2218 */
2219 hrc2 = UnRegisterTypeLib(&LIBID_VirtualBox, kTypeLibraryMajorVersion, kTypeLibraryMinorVersion,
2220 0 /*LCid*/, RT_CONCAT(SYS_WIN, ARCH_BITS));
2221 AssertMsgStmt(SUCCEEDED(hrc2) || hrc2 == TYPE_E_REGISTRYACCESS, ("%Rhrc\n", hrc2), if (SUCCEEDED(hrc)) hrc = hrc2);
2222
2223 /*
2224 * Unregister the proxy stub.
2225 *
2226 * We ignore ERROR_FILE_NOT_FOUND as that is returned if not registered (W10).
2227 */
2228 hrc2 = NdrDllUnregisterProxy(g_hDllSelf, &g_apProxyFiles[0], &g_ProxyClsId); /* see DLLREGISTRY_ROUTINES in RpcProxy.h */
2229 AssertMsgStmt( SUCCEEDED(hrc2)
2230 || hrc2 == MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, ERROR_FILE_NOT_FOUND)
2231 || hrc2 == REGDB_E_INVALIDVALUE,
2232 ("%Rhrc\n", hrc2), if (SUCCEEDED(hrc)) hrc = hrc2);
2233
2234 /*
2235 * Register the VBox modules and classes.
2236 */
2237 hrc2 = RegisterXidlModulesAndClasses(NULL, true /*fDelete*/, false /*fUpdate*/);
2238 AssertMsgStmt(SUCCEEDED(hrc2), ("%Rhrc\n", hrc2), if (SUCCEEDED(hrc)) hrc = hrc2);
2239
2240 /*
2241 * Purge old mess.
2242 */
2243 removeOldMess();
2244
2245 return hrc;
2246}
2247
2248
2249/**
2250 * Gently update the COM registrations for VirtualBox.
2251 *
2252 * API that com::Initialize (VBoxCOM/initterm.cpp) calls the first time COM is
2253 * initialized in a process. ASSUMES that the caller has initialized IPRT.
2254 *
2255 * @returns Windows error code.
2256 */
2257DECLEXPORT(uint32_t) VbpsUpdateRegistrations(void)
2258{
2259 LSTATUS rc;
2260 VBPSREGSTATE State;
2261#ifdef VBOX_IN_32_ON_64_MAIN_API
2262 bool const fIs32On64 = true;
2263#else
2264 bool const fIs32On64 = false;
2265#endif
2266
2267 /** @todo Should probably skip this when VBoxSVC is already running... Use
2268 * some mutex or something for checking. */
2269
2270 /*
2271 * Find the VirtualBox application directory first.
2272 */
2273 WCHAR wszVBoxDir[MAX_PATH];
2274 DWORD cwcRet = GetModuleFileNameW(g_hDllSelf, wszVBoxDir, RT_ELEMENTS(wszVBoxDir));
2275 AssertReturn(cwcRet > 0 && cwcRet < RT_ELEMENTS(wszVBoxDir), ERROR_BUFFER_OVERFLOW);
2276 vbpsDllPathToVBoxDir(wszVBoxDir);
2277
2278 /*
2279 * Update registry entries for the current CPU bitness.
2280 */
2281 rc = vbpsRegInit(&State, HKEY_CLASSES_ROOT, NULL, false /*fDelete*/, true /*fUpdate*/, 0);
2282 if (rc == ERROR_SUCCESS && !vbpsIsUpToDate(&State))
2283 {
2284 vbpsUpdateTypeLibRegistration(&State, wszVBoxDir, fIs32On64);
2285 vbpsUpdateProxyStubRegistration(&State, wszVBoxDir, fIs32On64);
2286 vbpsUpdateInterfaceRegistrations(&State);
2287 RegisterXidlModulesAndClassesGenerated(&State, wszVBoxDir, fIs32On64);
2288 vbpsMarkUpToDate(&State);
2289 rc = State.rc;
2290 }
2291 vbpsRegTerm(&State);
2292
2293
2294#if (ARCH_BITS == 64 && defined(VBOX_WITH_32_ON_64_MAIN_API)) /*|| defined(VBOX_IN_32_ON_64_MAIN_API) ??*/
2295 /*
2296 * Update registry entries for the other CPU bitness.
2297 */
2298 if (rc == ERROR_SUCCESS)
2299 {
2300 rc = vbpsRegInit(&State, HKEY_CLASSES_ROOT, NULL, false /*fDelete*/, true /*fUpdate*/,
2301 !fIs32On64 ? KEY_WOW64_32KEY : KEY_WOW64_64KEY);
2302 if (rc == ERROR_SUCCESS && !vbpsIsUpToDate(&State))
2303 {
2304 vbpsUpdateTypeLibRegistration(&State, wszVBoxDir, !fIs32On64);
2305 vbpsUpdateProxyStubRegistration(&State, wszVBoxDir, !fIs32On64);
2306 vbpsUpdateInterfaceRegistrations(&State);
2307 RegisterXidlModulesAndClassesGenerated(&State, wszVBoxDir, !fIs32On64);
2308 vbpsMarkUpToDate(&State);
2309 rc = State.rc;
2310 }
2311 vbpsRegTerm(&State);
2312 }
2313#endif
2314
2315 return VINF_SUCCESS;
2316}
2317
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette