VirtualBox

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

Last change on this file since 64936 was 63260, 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: 88.0 KB
Line 
1/* $Id: VBoxProxyStub.c 63260 2016-08-10 12:42: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.virtualbox.org. 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 RT_NOREF(fIs32On64);
1328
1329 Assert(pState->fUpdate && !pState->fDelete);
1330
1331 /*
1332 * Type library registration (w/o interfaces).
1333 */
1334
1335 /* Open Classes/TypeLib/. */
1336 rc = vbpsCreateRegKeyA(pState, pState->hkeyClassesRootDst, "TypeLib", &hkeyTypeLibs, __LINE__);
1337 if (rc != ERROR_SUCCESS)
1338 return;
1339
1340 /* Create TypeLib/{UUID}. */
1341 rc = vbpsCreateRegKeyA(pState, hkeyTypeLibs, vbpsFormatUuidInCurly(szTypeLibId, &LIBID_VirtualBox), &hkeyTypeLibId, __LINE__);
1342 if (rc == ERROR_SUCCESS)
1343 {
1344 /* {UUID}/Major.Minor/Default = pszDescription. */
1345 HKEY hkeyMajMin;
1346 char szMajMin[64];
1347 sprintf(szMajMin, "%u.%u", kTypeLibraryMajorVersion, kTypeLibraryMinorVersion);
1348 rc = vbpsCreateRegKeyWithDefaultValueAAEx(pState, hkeyTypeLibId, szMajMin, pszDescription, &hkeyMajMin, __LINE__);
1349 if (rc == ERROR_SUCCESS)
1350 {
1351 RTUTF16 wszBuf[MAX_PATH * 2];
1352
1353 /* {UUID}/Major.Minor/0. */
1354 HKEY hkey0;
1355 rc = vbpsCreateRegKeyA(pState, hkeyMajMin, "0", &hkey0, __LINE__);
1356 if (rc == ERROR_SUCCESS)
1357 {
1358 /* {UUID}/Major.Minor/0/winXX/Default = VBoxProxyStub. */
1359 rc = RTUtf16Copy(wszBuf, MAX_PATH, pwszVBoxDir); AssertRC(rc);
1360 rc = RTUtf16CatAscii(wszBuf, MAX_PATH * 2, pszTypeLibDll); AssertRC(rc);
1361
1362 vbpsCreateRegKeyWithDefaultValueAW(pState, hkey0, pszWinXx, wszBuf, __LINE__);
1363 vbpsCloseKey(pState, hkey0, __LINE__);
1364 }
1365
1366 /* {UUID}/Major.Minor/FLAGS */
1367 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyMajMin, "FLAGS", "0", __LINE__);
1368
1369 /* {UUID}/Major.Minor/HELPDIR */
1370 rc = RTUtf16Copy(wszBuf, MAX_PATH, pwszVBoxDir); AssertRC(rc);
1371#if 0 /* MSI: trailing slash; regsvr32/comregister: strip unnecessary trailing slash. Go with MSI to avoid user issues. */
1372 {
1373 size_t off = RTUtf16Len(wszBuf);
1374 while (off > 2 && wszBuf[off - 2] != ':' && RTPATH_IS_SLASH(wszBuf[off - 1]))
1375 off--;
1376 wszBuf[off] = '\0';
1377 }
1378#endif
1379 vbpsCreateRegKeyWithDefaultValueAW(pState, hkeyMajMin, "HELPDIR", wszBuf, __LINE__);
1380
1381 vbpsCloseKey(pState, hkeyMajMin, __LINE__);
1382 }
1383 vbpsCloseKey(pState, hkeyTypeLibId, __LINE__);
1384 }
1385 vbpsCloseKey(pState, hkeyTypeLibs, __LINE__);
1386}
1387
1388
1389/**
1390 * Update the VBox proxy stub registration.
1391 *
1392 * This is only used when updating COM registrations during com::Initialize.
1393 * For normal registration and unregistrations we use the NdrDllRegisterProxy
1394 * and NdrDllUnregisterProxy.
1395 *
1396 * @param pState The registry modifier state.
1397 * @param pwszVBoxDir The VirtualBox install directory (unicode),
1398 * trailing slash.
1399 * @param fIs32On64 Set if we're registering the 32-bit proxy stub
1400 * on a 64-bit system.
1401 */
1402static void vbpsUpdateProxyStubRegistration(VBPSREGSTATE *pState, PCRTUTF16 pwszVBoxDir, bool fIs32On64)
1403{
1404 /*
1405 * Register the proxy stub factory class ID.
1406 * It's simple compared to the VBox classes, thus all the NULL parameters.
1407 */
1408 const char *pszPsDll = VBPS_PROXY_STUB_FILE(fIs32On64);
1409 RT_NOREF(fIs32On64);
1410 Assert(pState->fUpdate && !pState->fDelete);
1411 VbpsRegisterClassId(pState, &g_ProxyClsId, "PSFactoryBuffer", NULL /*pszAppId*/,
1412 NULL /*pszClassName*/, NULL /*pszCurClassNameVerSuffix*/, NULL /*pTypeLibId*/,
1413 "InprocServer32", pwszVBoxDir, pszPsDll, "Both");
1414}
1415
1416
1417/**
1418 * Updates the VBox interface registrations.
1419 *
1420 * This is only used when updating COM registrations during com::Initialize.
1421 * For normal registration and unregistrations we use the NdrDllRegisterProxy
1422 * and NdrDllUnregisterProxy.
1423 *
1424 * @param pState The registry modifier state.
1425 * @param pwszVBoxDir The VirtualBox install directory (unicode),
1426 * trailing slash.
1427 * @param fIs32On64 Set if we're registering the 32-bit proxy stub
1428 * on a 64-bit system.
1429 */
1430static void vbpsUpdateInterfaceRegistrations(VBPSREGSTATE *pState)
1431{
1432 const ProxyFileInfo **ppProxyFile = &g_apProxyFiles[0];
1433 const ProxyFileInfo *pProxyFile;
1434 LSTATUS rc;
1435 char szProxyClsId[CURLY_UUID_STR_BUF_SIZE];
1436 char szTypeLibId[CURLY_UUID_STR_BUF_SIZE];
1437 char szTypeLibVersion[64];
1438
1439 vbpsFormatUuidInCurly(szProxyClsId, &g_ProxyClsId);
1440 vbpsFormatUuidInCurly(szTypeLibId, &LIBID_VirtualBox);
1441 sprintf(szTypeLibVersion, "%u.%u", kTypeLibraryMajorVersion, kTypeLibraryMinorVersion);
1442
1443 Assert(pState->fUpdate && !pState->fDelete);
1444 rc = vbpsRegOpenInterfaceKeys(pState);
1445 if (rc != ERROR_SUCCESS)
1446 return;
1447
1448 /*
1449 * We walk the proxy file list (even if we only have one).
1450 */
1451 while ((pProxyFile = *ppProxyFile++) != NULL)
1452 {
1453 const PCInterfaceStubVtblList * const papStubVtbls = pProxyFile->pStubVtblList;
1454 const char * const *papszNames = pProxyFile->pNamesArray;
1455 unsigned iIf = pProxyFile->TableSize;
1456 AssertStmt(iIf < 1024, iIf = 0);
1457 Assert(pProxyFile->TableVersion == 2);
1458
1459 /*
1460 * Walk the interfaces in that file, picking data from the various tables.
1461 */
1462 while (iIf-- > 0)
1463 {
1464 char szIfId[CURLY_UUID_STR_BUF_SIZE];
1465 const char * const pszIfNm = papszNames[iIf];
1466 size_t const cchIfNm = RT_VALID_PTR(pszIfNm) ? strlen(pszIfNm) : 0;
1467 char szMethods[32];
1468 uint32_t const cMethods = papStubVtbls[iIf]->header.DispatchTableCount;
1469 HKEY hkeyIfId;
1470
1471 AssertReturnVoidStmt(cchIfNm >= 3 && cchIfNm <= 72, pState->rc = ERROR_INVALID_DATA);
1472
1473 AssertReturnVoidStmt(cMethods >= 3 && cMethods < 1024, pState->rc = ERROR_INVALID_DATA);
1474 sprintf(szMethods, "%u", cMethods);
1475
1476 rc = vbpsCreateRegKeyWithDefaultValueAAEx(pState, pState->hkeyInterfaceRootDst,
1477 vbpsFormatUuidInCurly(szIfId, papStubVtbls[iIf]->header.piid),
1478 pszIfNm, &hkeyIfId, __LINE__);
1479 if (rc == ERROR_SUCCESS)
1480 {
1481 HKEY hkeyTypeLib;
1482 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyIfId, "ProxyStubClsid32", szProxyClsId, __LINE__);
1483 vbpsCreateRegKeyWithDefaultValueAA(pState, hkeyIfId, "NumMethods", szMethods, __LINE__);
1484
1485 /* The MSI seems to still be putting TypeLib keys here. So, let's do that too. */
1486 rc = vbpsCreateRegKeyWithDefaultValueAAEx(pState, hkeyIfId, "TypeLib", szTypeLibId, &hkeyTypeLib, __LINE__);
1487 if (rc == ERROR_SUCCESS)
1488 {
1489 vbpsSetRegValueAA(pState, hkeyTypeLib, "Version", szTypeLibVersion, __LINE__);
1490 vbpsCloseKey(pState, hkeyTypeLib, __LINE__);
1491 }
1492
1493 vbpsCloseKey(pState, hkeyIfId, __LINE__);
1494 }
1495 }
1496 }
1497}
1498
1499
1500static bool vbpsIsUpToDate(VBPSREGSTATE *pState)
1501{
1502 /** @todo read some registry key and */
1503 NOREF(pState);
1504 return false;
1505}
1506
1507static bool vbpsMarkUpToDate(VBPSREGSTATE *pState)
1508{
1509 /** @todo write the key vbpsIsUpToDate uses, if pState indicates success. */
1510 NOREF(pState);
1511 return false;
1512}
1513
1514
1515
1516/**
1517 * Strips the stub dll name and any x86 subdir off the full DLL path to get a
1518 * path to the VirtualBox application directory.
1519 *
1520 * @param pwszDllPath The path to strip, returns will end with a slash.
1521 */
1522static void vbpsDllPathToVBoxDir(PRTUTF16 pwszDllPath)
1523{
1524 RTUTF16 wc;
1525 size_t off = RTUtf16Len(pwszDllPath);
1526 while ( off > 0
1527 && ( (wc = pwszDllPath[off - 1]) >= 127U
1528 || !RTPATH_IS_SEP((unsigned char)wc)))
1529 off--;
1530
1531#ifdef VBOX_IN_32_ON_64_MAIN_API
1532 /*
1533 * The -x86 variant is in a x86 subdirectory, drop it.
1534 */
1535 while ( off > 0
1536 && ( (wc = pwszDllPath[off - 1]) < 127U
1537 && RTPATH_IS_SEP((unsigned char)wc)))
1538 off--;
1539 while ( off > 0
1540 && ( (wc = pwszDllPath[off - 1]) >= 127U
1541 || !RTPATH_IS_SEP((unsigned char)wc)))
1542 off--;
1543#endif
1544 pwszDllPath[off] = '\0';
1545}
1546
1547
1548/**
1549 * Wrapper around RegisterXidlModulesAndClassesGenerated for the convenience of
1550 * the standard registration entry points.
1551 *
1552 * @returns COM status code.
1553 * @param pwszVBoxDir The VirtualBox install directory (unicode),
1554 * trailing slash.
1555 * @param fDelete Whether to delete registration keys and values.
1556 * @param fUpdate Whether to update registration keys and values.
1557 */
1558HRESULT RegisterXidlModulesAndClasses(PRTUTF16 pwszVBoxDir, bool fDelete, bool fUpdate)
1559{
1560#ifdef VBOX_IN_32_ON_64_MAIN_API
1561 bool const fIs32On64 = true;
1562#else
1563 bool const fIs32On64 = false;
1564#endif
1565 VBPSREGSTATE State;
1566 LSTATUS rc;
1567
1568 /*
1569 * Do registration for the current execution mode of the DLL.
1570 */
1571 rc = vbpsRegInit(&State, HKEY_CLASSES_ROOT, NULL /* Alt: HKEY_LOCAL_MACHINE, "Software\\Classes", */, fDelete, fUpdate, 0);
1572 if (rc == ERROR_SUCCESS)
1573 {
1574 if (!fUpdate)
1575 {
1576 /* When only unregistering, really purge everything twice or trice. :-) */
1577 vbpsRegAddAltDelete(&State, HKEY_LOCAL_MACHINE, "Software\\Classes");
1578 vbpsRegAddAltDelete(&State, HKEY_CURRENT_USER, "Software\\Classes");
1579 vbpsRegAddAltDelete(&State, HKEY_CLASSES_ROOT, NULL);
1580 }
1581
1582 RegisterXidlModulesAndClassesGenerated(&State, pwszVBoxDir, fIs32On64);
1583 rc = State.rc;
1584 }
1585 vbpsRegTerm(&State);
1586
1587 /*
1588 * Translate error code? Return.
1589 */
1590 if (rc == ERROR_SUCCESS)
1591 return S_OK;
1592 return E_FAIL;
1593}
1594
1595
1596/**
1597 * Checks if the string matches any of our type library versions.
1598 *
1599 * @returns true on match, false on mismatch.
1600 * @param pwszTypeLibVersion The type library version string.
1601 */
1602DECLINLINE(bool) vbpsIsTypeLibVersionToRemove(PCRTUTF16 pwszTypeLibVersion)
1603{
1604 AssertCompile(RT_ELEMENTS(g_apwszTypelibVersions) == 2);
1605
1606 /* ASSUMES: 1.x version strings and that the input buffer is at least 3 wchars long. */
1607 if ( g_apwszTypelibVersions[0][3] == pwszTypeLibVersion[3]
1608 && RTUtf16Cmp(g_apwszTypelibVersions[0], pwszTypeLibVersion) == 0)
1609 return true;
1610 if ( g_apwszTypelibVersions[1][3] == pwszTypeLibVersion[3]
1611 && RTUtf16Cmp(g_apwszTypelibVersions[1], pwszTypeLibVersion) == 0)
1612 return true;
1613
1614 return false;
1615}
1616
1617
1618/**
1619 * Quick check whether the given string looks like a UUID in braces.
1620 *
1621 * This does not check the whole string, just do a quick sweep.
1622 *
1623 * @returns true if possible UUID, false if definitely not.
1624 * @param pwszUuid Alleged UUID in braces.
1625 */
1626DECLINLINE(bool) vbpsIsUuidInBracesQuickW(PCRTUTF16 pwszUuid)
1627{
1628 return pwszUuid[ 0] == '{'
1629 && pwszUuid[ 9] == '-'
1630 && pwszUuid[14] == '-'
1631 && pwszUuid[19] == '-'
1632 && pwszUuid[24] == '-'
1633 && pwszUuid[37] == '}'
1634 && pwszUuid[38] == '\0'
1635 && RT_C_IS_XDIGIT(pwszUuid[1]);
1636}
1637
1638
1639/**
1640 * Compares two UUIDs (in braces).
1641 *
1642 * @returns true on match, false if no match.
1643 * @param pwszUuid1 The first UUID.
1644 * @param pwszUuid2 The second UUID.
1645 */
1646static bool vbpsCompareUuidW(PCRTUTF16 pwszUuid1, PCRTUTF16 pwszUuid2)
1647{
1648#define COMPARE_EXACT_RET(a_wch1, a_wch2) \
1649 if ((a_wch1) == (a_wch2)) { } else return false
1650
1651#define COMPARE_XDIGITS_RET(a_wch1, a_wch2) \
1652 if ((a_wch1) == (a_wch2)) { } \
1653 else if (RT_C_TO_UPPER(a_wch1) != RT_C_TO_UPPER(a_wch2) || (a_wch1) >= 127U || (a_wch2) >= 127U) \
1654 return false
1655 COMPARE_EXACT_RET( pwszUuid1[ 0], pwszUuid2[ 0]); /* { */
1656 COMPARE_XDIGITS_RET(pwszUuid1[ 1], pwszUuid2[ 1]); /* 5 */
1657 COMPARE_XDIGITS_RET(pwszUuid1[ 2], pwszUuid2[ 2]); /* e */
1658 COMPARE_XDIGITS_RET(pwszUuid1[ 3], pwszUuid2[ 3]); /* 5 */
1659 COMPARE_XDIGITS_RET(pwszUuid1[ 4], pwszUuid2[ 4]); /* e */
1660 COMPARE_XDIGITS_RET(pwszUuid1[ 5], pwszUuid2[ 5]); /* 3 */
1661 COMPARE_XDIGITS_RET(pwszUuid1[ 6], pwszUuid2[ 6]); /* 6 */
1662 COMPARE_XDIGITS_RET(pwszUuid1[ 7], pwszUuid2[ 7]); /* 4 */
1663 COMPARE_XDIGITS_RET(pwszUuid1[ 8], pwszUuid2[ 8]); /* 0 */
1664 COMPARE_EXACT_RET( pwszUuid1[ 9], pwszUuid2[ 9]); /* - */
1665 COMPARE_XDIGITS_RET(pwszUuid1[10], pwszUuid2[10]); /* 7 */
1666 COMPARE_XDIGITS_RET(pwszUuid1[11], pwszUuid2[11]); /* 4 */
1667 COMPARE_XDIGITS_RET(pwszUuid1[12], pwszUuid2[12]); /* f */
1668 COMPARE_XDIGITS_RET(pwszUuid1[13], pwszUuid2[13]); /* 3 */
1669 COMPARE_EXACT_RET( pwszUuid1[14], pwszUuid2[14]); /* - */
1670 COMPARE_XDIGITS_RET(pwszUuid1[15], pwszUuid2[15]); /* 4 */
1671 COMPARE_XDIGITS_RET(pwszUuid1[16], pwszUuid2[16]); /* 6 */
1672 COMPARE_XDIGITS_RET(pwszUuid1[17], pwszUuid2[17]); /* 8 */
1673 COMPARE_XDIGITS_RET(pwszUuid1[18], pwszUuid2[18]); /* 9 */
1674 COMPARE_EXACT_RET( pwszUuid1[19], pwszUuid2[19]); /* - */
1675 COMPARE_XDIGITS_RET(pwszUuid1[20], pwszUuid2[20]); /* 9 */
1676 COMPARE_XDIGITS_RET(pwszUuid1[21], pwszUuid2[21]); /* 7 */
1677 COMPARE_XDIGITS_RET(pwszUuid1[22], pwszUuid2[22]); /* 9 */
1678 COMPARE_XDIGITS_RET(pwszUuid1[23], pwszUuid2[23]); /* f */
1679 COMPARE_EXACT_RET( pwszUuid1[24], pwszUuid2[24]); /* - */
1680 COMPARE_XDIGITS_RET(pwszUuid1[25], pwszUuid2[25]); /* 6 */
1681 COMPARE_XDIGITS_RET(pwszUuid1[26], pwszUuid2[26]); /* b */
1682 COMPARE_XDIGITS_RET(pwszUuid1[27], pwszUuid2[27]); /* 1 */
1683 COMPARE_XDIGITS_RET(pwszUuid1[28], pwszUuid2[28]); /* b */
1684 COMPARE_XDIGITS_RET(pwszUuid1[29], pwszUuid2[29]); /* 8 */
1685 COMPARE_XDIGITS_RET(pwszUuid1[30], pwszUuid2[30]); /* d */
1686 COMPARE_XDIGITS_RET(pwszUuid1[31], pwszUuid2[31]); /* 7 */
1687 COMPARE_XDIGITS_RET(pwszUuid1[32], pwszUuid2[32]); /* 6 */
1688 COMPARE_XDIGITS_RET(pwszUuid1[33], pwszUuid2[33]); /* 0 */
1689 COMPARE_XDIGITS_RET(pwszUuid1[34], pwszUuid2[34]); /* 9 */
1690 COMPARE_XDIGITS_RET(pwszUuid1[35], pwszUuid2[35]); /* a */
1691 COMPARE_XDIGITS_RET(pwszUuid1[36], pwszUuid2[36]); /* 5 */
1692 COMPARE_EXACT_RET( pwszUuid1[37], pwszUuid2[37]); /* } */
1693 COMPARE_EXACT_RET( pwszUuid1[38], pwszUuid2[38]); /* \0 */
1694#undef COMPARE_EXACT_RET
1695#undef COMPARE_XDIGITS_RET
1696 return true;
1697}
1698
1699
1700/**
1701 * Checks if the type library ID is one of the ones we wish to clean up.
1702 *
1703 * @returns true if it should be cleaned up, false if not.
1704 * @param pwszTypeLibId The type library ID as a bracketed string.
1705 */
1706DECLINLINE(bool) vbpsIsTypeLibIdToRemove(PRTUTF16 pwszTypeLibId)
1707{
1708 AssertCompile(RT_ELEMENTS(g_apwszTypeLibIds) == 2);
1709#ifdef VBOX_STRICT
1710 static bool s_fDoneStrict = false;
1711 if (s_fDoneStrict) { }
1712 else
1713 {
1714 Assert(RT_ELEMENTS(g_apwszTypeLibIds) == 2);
1715 Assert(g_apwszTypeLibIds[0][0] == '{');
1716 Assert(g_apwszTypeLibIds[1][0] == '{');
1717 Assert(RT_C_IS_XDIGIT(g_apwszTypeLibIds[0][1]));
1718 Assert(RT_C_IS_XDIGIT(g_apwszTypeLibIds[1][1]));
1719 Assert(RT_C_IS_UPPER(g_apwszTypeLibIds[0][1]) || RT_C_IS_DIGIT(g_apwszTypeLibIds[0][1]));
1720 Assert(RT_C_IS_UPPER(g_apwszTypeLibIds[1][1]) || RT_C_IS_DIGIT(g_apwszTypeLibIds[1][1]));
1721 s_fDoneStrict = true;
1722 }
1723#endif
1724
1725 /*
1726 * Rolled out matching with inlined check of the opening braces
1727 * and first two digits.
1728 *
1729 * ASSUMES input buffer is at least 3 wchars big and uppercased UUID in
1730 * our matching array.
1731 */
1732 if (pwszTypeLibId[0] == '{')
1733 {
1734 RTUTF16 const wcFirstDigit = RT_C_TO_UPPER(pwszTypeLibId[1]);
1735 RTUTF16 const wcSecondDigit = RT_C_TO_UPPER(pwszTypeLibId[2]);
1736 PCRTUTF16 pwsz2 = g_apwszTypeLibIds[0];
1737 if ( wcFirstDigit == pwsz2[1]
1738 && wcSecondDigit == pwsz2[2]
1739 && vbpsCompareUuidW(pwszTypeLibId, pwsz2))
1740 return true;
1741 pwsz2 = g_apwszTypeLibIds[1];
1742 if ( wcFirstDigit == pwsz2[1]
1743 && wcSecondDigit == pwsz2[2]
1744 && vbpsCompareUuidW(pwszTypeLibId, pwsz2))
1745 return true;
1746 }
1747 return false;
1748}
1749
1750
1751/**
1752 * Checks if the proxy stub class ID is one of the ones we wish to clean up.
1753 *
1754 * @returns true if it should be cleaned up, false if not.
1755 * @param pwszProxyStubId The proxy stub class ID.
1756 */
1757DECLINLINE(bool) vbpsIsProxyStubClsIdToRemove(PRTUTF16 pwszProxyStubId)
1758{
1759 AssertCompile(RT_ELEMENTS(g_apwszProxyStubClsIds) == 2);
1760#ifdef VBOX_STRICT
1761 static bool s_fDoneStrict = false;
1762 if (s_fDoneStrict) { }
1763 else
1764 {
1765 Assert(RT_ELEMENTS(g_apwszProxyStubClsIds) == 2);
1766 Assert(g_apwszProxyStubClsIds[0][0] == '{');
1767 Assert(g_apwszProxyStubClsIds[1][0] == '{');
1768 Assert(RT_C_IS_XDIGIT(g_apwszProxyStubClsIds[0][1]));
1769 Assert(RT_C_IS_XDIGIT(g_apwszProxyStubClsIds[1][1]));
1770 Assert(RT_C_IS_UPPER(g_apwszProxyStubClsIds[0][1]) || RT_C_IS_DIGIT(g_apwszProxyStubClsIds[0][1]));
1771 Assert(RT_C_IS_UPPER(g_apwszProxyStubClsIds[1][1]) || RT_C_IS_DIGIT(g_apwszProxyStubClsIds[1][1]));
1772 s_fDoneStrict = true;
1773 }
1774#endif
1775
1776 /*
1777 * Rolled out matching with inlined check of the opening braces
1778 * and first two digits.
1779 *
1780 * ASSUMES input buffer is at least 3 wchars big and uppercased UUID in
1781 * our matching array.
1782 */
1783 if (pwszProxyStubId[0] == '{')
1784 {
1785 RTUTF16 const wcFirstDigit = RT_C_TO_UPPER(pwszProxyStubId[1]);
1786 RTUTF16 const wcSecondDigit = RT_C_TO_UPPER(pwszProxyStubId[2]);
1787 PCRTUTF16 pwsz2 = g_apwszProxyStubClsIds[0];
1788 if ( wcFirstDigit == pwsz2[1]
1789 && wcSecondDigit == pwsz2[2]
1790 && vbpsCompareUuidW(pwszProxyStubId, pwsz2))
1791 return true;
1792 pwsz2 = g_apwszProxyStubClsIds[1];
1793 if ( wcFirstDigit == pwsz2[1]
1794 && wcSecondDigit == pwsz2[2]
1795 && vbpsCompareUuidW(pwszProxyStubId, pwsz2))
1796 return true;
1797 }
1798 return false;
1799}
1800
1801
1802/**
1803 * Hack to clean out the interfaces belonging to obsolete typelibs on
1804 * development boxes and such likes.
1805 */
1806static void vbpsRemoveOldInterfaces(VBPSREGSTATE *pState)
1807{
1808 unsigned iAlt = pState->cAltDeletes;
1809 while (iAlt-- > 0)
1810 {
1811 /*
1812 * Open the interface root key. Not using the vbpsRegOpenInterfaceKeys feature
1813 * here in case it messes things up by keeping the special HKEY_CLASSES_ROOT key
1814 * open with possibly pending deletes in parent views or other weird stuff.
1815 */
1816 HKEY hkeyInterfaces;
1817 LRESULT rc = RegOpenKeyExW(pState->aAltDeletes[iAlt].hkeyClasses, L"Interface",
1818 0 /*fOptions*/, pState->fSamDelete, &hkeyInterfaces);
1819 if (rc == ERROR_SUCCESS)
1820 {
1821 /*
1822 * This is kind of expensive, but we have to check all registered interfaces.
1823 * Only use wide APIs to avoid wasting time on string conversion.
1824 */
1825 DWORD idxKey;
1826 for (idxKey = 0;; idxKey++)
1827 {
1828 RTUTF16 wszCurNm[128 + 48];
1829 DWORD cwcCurNm = 128;
1830 rc = RegEnumKeyExW(hkeyInterfaces, idxKey, wszCurNm, &cwcCurNm,
1831 NULL /*pdwReserved*/, NULL /*pwszClass*/, NULL /*pcwcClass*/, NULL /*pLastWriteTime*/);
1832 if (rc == ERROR_SUCCESS)
1833 {
1834 /*
1835 * We match the interface by type library ID or proxy stub class ID.
1836 *
1837 * We have to check the proxy ID last, as it is almost always there
1838 * and we can safely skip it if there is a mismatching type lib
1839 * associated with the interface.
1840 */
1841 static RTUTF16 const s_wszTypeLib[] = L"\\TypeLib";
1842 bool fDeleteMe = false;
1843 HKEY hkeySub;
1844 RTUTF16 wszValue[128];
1845 DWORD cbValue;
1846 DWORD dwType;
1847
1848 /* Skip this entry if it doesn't look like a braced UUID. */
1849 wszCurNm[cwcCurNm] = '\0'; /* paranoia */
1850 if (vbpsIsUuidInBracesQuickW(wszCurNm)) { }
1851 else continue;
1852
1853 /* Try the TypeLib sub-key. */
1854 memcpy(&wszCurNm[cwcCurNm], s_wszTypeLib, sizeof(s_wszTypeLib));
1855 rc = RegOpenKeyExW(hkeyInterfaces, wszCurNm, 0 /*fOptions*/, KEY_QUERY_VALUE, &hkeySub);
1856 if (rc == ERROR_SUCCESS)
1857 {
1858 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
1859 rc = RegQueryValueExW(hkeySub, NULL /*pszValueNm*/, NULL /*pdwReserved*/,
1860 &dwType, (PBYTE)&wszValue[0], &cbValue);
1861 if (rc != ERROR_SUCCESS || dwType != REG_SZ)
1862 cbValue = 0;
1863 wszValue[cbValue / sizeof(RTUTF16)] = '\0';
1864
1865 if ( rc == ERROR_SUCCESS
1866 && vbpsIsTypeLibIdToRemove(wszValue))
1867 {
1868 /* Check the TypeLib/Version value to make sure. */
1869 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
1870 rc = RegQueryValueExW(hkeySub, L"Version", 0 /*pdwReserved*/, &dwType, (PBYTE)&wszValue[0], &cbValue);
1871 if (rc != ERROR_SUCCESS)
1872 cbValue = 0;
1873 wszValue[cbValue] = '\0';
1874
1875 if ( rc == ERROR_SUCCESS
1876 && vbpsIsTypeLibVersionToRemove(wszValue))
1877 fDeleteMe = true;
1878 }
1879 vbpsCloseKey(pState, hkeySub, __LINE__);
1880 }
1881 else if (rc == ERROR_FILE_NOT_FOUND)
1882 {
1883 /* No TypeLib, try the ProxyStubClsid32 sub-key next. */
1884 static RTUTF16 const s_wszProxyStubClsid32[] = L"\\ProxyStubClsid32";
1885 memcpy(&wszCurNm[cwcCurNm], s_wszProxyStubClsid32, sizeof(s_wszProxyStubClsid32));
1886 rc = RegOpenKeyExW(hkeyInterfaces, wszCurNm, 0 /*fOptions*/, KEY_QUERY_VALUE, &hkeySub);
1887 if (rc == ERROR_SUCCESS)
1888 {
1889 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
1890 rc = RegQueryValueExW(hkeySub, NULL /*pszValueNm*/, NULL /*pdwReserved*/,
1891 &dwType, (PBYTE)&wszValue[0], &cbValue);
1892 if (rc != ERROR_SUCCESS || dwType != REG_SZ)
1893 cbValue = 0;
1894 wszValue[cbValue / sizeof(RTUTF16)] = '\0';
1895
1896 if ( rc == ERROR_SUCCESS
1897 && vbpsIsProxyStubClsIdToRemove(wszValue))
1898 fDeleteMe = true;
1899
1900 vbpsCloseKey(pState, hkeySub, __LINE__);
1901 }
1902 }
1903
1904 if (fDeleteMe)
1905 {
1906 /*
1907 * Ok, it's an orphaned VirtualBox interface. Delete it.
1908 */
1909 wszCurNm[cwcCurNm] = '\0';
1910 vbpsDeleteKeyRecursiveW(pState, hkeyInterfaces, wszCurNm, __LINE__);
1911 }
1912 }
1913 else
1914 {
1915 Assert(rc == ERROR_NO_MORE_ITEMS);
1916 break;
1917 }
1918 }
1919
1920 vbpsCloseKey(pState, hkeyInterfaces, __LINE__);
1921 }
1922 }
1923}
1924
1925
1926/**
1927 * Hack to clean out the class IDs belonging to obsolete typelibs on development
1928 * boxes and such likes.
1929 */
1930static void vbpsRemoveOldClassIDs(VBPSREGSTATE *pState)
1931{
1932 unsigned iAlt = pState->cAltDeletes;
1933 while (iAlt-- > 0)
1934 {
1935 /*
1936 * Open the CLSID key if it exists.
1937 * We don't use the hKeyClsid member for the same paranoid reasons as
1938 * already stated in vbpsRemoveOldInterfaces.
1939 */
1940 HKEY hkeyClsIds;
1941 LRESULT rc;
1942 rc = RegOpenKeyExW(pState->aAltDeletes[iAlt].hkeyClasses, L"CLSID", 0 /*fOptions*/, pState->fSamDelete, &hkeyClsIds);
1943 if (rc == ERROR_SUCCESS)
1944 {
1945 /*
1946 * This is kind of expensive, but we have to check all registered interfaces.
1947 * Only use wide APIs to avoid wasting time on string conversion.
1948 */
1949 DWORD idxKey;
1950 for (idxKey = 0;; idxKey++)
1951 {
1952 RTUTF16 wszCurNm[128 + 48];
1953 DWORD cwcCurNm = 128;
1954 rc = RegEnumKeyExW(hkeyClsIds, idxKey, wszCurNm, &cwcCurNm,
1955 NULL /*pdwReserved*/, NULL /*pwszClass*/, NULL /*pcwcClass*/, NULL /*pLastWriteTime*/);
1956 if (rc == ERROR_SUCCESS)
1957 {
1958 /*
1959 * Match both the type library ID and the program ID.
1960 */
1961 static RTUTF16 const s_wszTypeLib[] = L"\\TypeLib";
1962 HKEY hkeySub;
1963 RTUTF16 wszValue[128];
1964 DWORD cbValue;
1965 DWORD dwType;
1966
1967
1968 /* Skip this entry if it doesn't look like a braced UUID. (Microsoft
1969 has one two malformed ones plus a hack.) */
1970 wszCurNm[cwcCurNm] = '\0'; /* paranoia */
1971 if (vbpsIsUuidInBracesQuickW(wszCurNm)) { }
1972 else continue;
1973
1974 /* The TypeLib sub-key. */
1975 memcpy(&wszCurNm[cwcCurNm], s_wszTypeLib, sizeof(s_wszTypeLib));
1976 rc = RegOpenKeyExW(hkeyClsIds, wszCurNm, 0 /*fOptions*/, KEY_QUERY_VALUE, &hkeySub);
1977 if (rc == ERROR_SUCCESS)
1978 {
1979 bool fDeleteMe = false;
1980
1981 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
1982 rc = RegQueryValueExW(hkeySub, NULL /*pszValueNm*/, NULL /*pdwReserved*/,
1983 &dwType, (PBYTE)&wszValue[0], &cbValue);
1984 if (rc != ERROR_SUCCESS || dwType != REG_SZ)
1985 cbValue = 0;
1986 wszValue[cbValue / sizeof(RTUTF16)] = '\0';
1987
1988 if ( rc == ERROR_SUCCESS
1989 && vbpsIsTypeLibIdToRemove(wszValue))
1990 fDeleteMe = true;
1991
1992 vbpsCloseKey(pState, hkeySub, __LINE__);
1993
1994 if (fDeleteMe)
1995 {
1996 /* The ProgId sub-key. */
1997 static RTUTF16 const s_wszProgId[] = L"\\ProgId";
1998 memcpy(&wszCurNm[cwcCurNm], s_wszProgId, sizeof(s_wszProgId));
1999 rc = RegOpenKeyExW(hkeyClsIds, wszCurNm, 0 /*fOptions*/, KEY_QUERY_VALUE, &hkeySub);
2000 if (rc == ERROR_SUCCESS)
2001 {
2002 static RTUTF16 const s_wszProgIdPrefix[] = L"VirtualBox.";
2003
2004 cbValue = sizeof(wszValue) - sizeof(RTUTF16);
2005 rc = RegQueryValueExW(hkeySub, NULL /*pszValueNm*/, NULL /*pdwReserved*/,
2006 &dwType, (PBYTE)&wszValue[0], &cbValue);
2007 if (rc != ERROR_SUCCESS || dwType != REG_SZ)
2008 cbValue = 0;
2009 wszValue[cbValue / sizeof(RTUTF16)] = '\0';
2010
2011 if ( cbValue < sizeof(s_wszProgIdPrefix)
2012 || memcmp(wszValue, s_wszProgIdPrefix, sizeof(s_wszProgIdPrefix) - sizeof(RTUTF16)) != 0)
2013 fDeleteMe = false;
2014
2015 vbpsCloseKey(pState, hkeySub, __LINE__);
2016 }
2017 else
2018 AssertStmt(rc == ERROR_FILE_NOT_FOUND, fDeleteMe = false);
2019
2020 if (fDeleteMe)
2021 {
2022 /*
2023 * Ok, it's an orphaned VirtualBox interface. Delete it.
2024 */
2025 wszCurNm[cwcCurNm] = '\0';
2026 vbpsDeleteKeyRecursiveW(pState, hkeyClsIds, wszCurNm, __LINE__);
2027 }
2028 }
2029 }
2030 else
2031 Assert(rc == ERROR_FILE_NOT_FOUND);
2032 }
2033 else
2034 {
2035 Assert(rc == ERROR_NO_MORE_ITEMS);
2036 break;
2037 }
2038 }
2039
2040 vbpsCloseKey(pState, hkeyClsIds, __LINE__);
2041 }
2042 else
2043 Assert(rc == ERROR_FILE_NOT_FOUND);
2044 }
2045}
2046
2047
2048/**
2049 * Hack to clean obsolete typelibs on development boxes and such.
2050 */
2051static void vbpsRemoveOldTypeLibs(VBPSREGSTATE *pState)
2052{
2053 unsigned iAlt = pState->cAltDeletes;
2054 while (iAlt-- > 0)
2055 {
2056 /*
2057 * Open the TypeLib key, if it exists.
2058 */
2059 HKEY hkeyTypeLibs;
2060 LRESULT rc;
2061 rc = RegOpenKeyExW(pState->aAltDeletes[iAlt].hkeyClasses, L"TypeLib", 0 /*fOptions*/, pState->fSamDelete, &hkeyTypeLibs);
2062 if (rc == ERROR_SUCCESS)
2063 {
2064 /*
2065 * Look for our type library IDs.
2066 */
2067 unsigned iTlb = RT_ELEMENTS(g_apwszTypeLibIds);
2068 while (iTlb-- > 0)
2069 {
2070 HKEY hkeyTypeLibId;
2071 LONG rc = RegOpenKeyExW(hkeyTypeLibs, g_apwszTypeLibIds[iTlb], 0 /*fOptions*/, pState->fSamDelete, &hkeyTypeLibId);
2072 if (rc == ERROR_SUCCESS)
2073 {
2074 unsigned iVer = RT_ELEMENTS(g_apwszTypelibVersions);
2075 while (iVer-- > 0)
2076 {
2077 HKEY hkeyVer;
2078 rc = RegOpenKeyExW(hkeyTypeLibId, g_apwszTypelibVersions[iVer], 0, KEY_READ, &hkeyVer);
2079 if (rc == ERROR_SUCCESS)
2080 {
2081 char szValue[128];
2082 DWORD cbValue = sizeof(szValue) - 1;
2083 rc = RegQueryValueExA(hkeyVer, NULL, NULL, NULL, (PBYTE)&szValue[0], &cbValue);
2084 vbpsCloseKey(pState, hkeyVer, __LINE__);
2085 if (rc == ERROR_SUCCESS)
2086 {
2087 szValue[cbValue] = '\0';
2088 if (!strcmp(szValue, "VirtualBox Type Library"))
2089 {
2090 /*
2091 * Delete the type library version.
2092 * We do not delete the whole type library ID, just this version of it.
2093 */
2094 vbpsDeleteKeyRecursiveW(pState, hkeyTypeLibId, g_apwszTypelibVersions[iVer], __LINE__);
2095 }
2096 }
2097 }
2098 }
2099 vbpsCloseKey(pState, hkeyTypeLibId, __LINE__);
2100
2101 /*
2102 * The type library ID key should be empty now, so we can try remove it (non-recursively).
2103 */
2104 rc = RegDeleteKeyW(hkeyTypeLibs, g_apwszTypeLibIds[iTlb]);
2105 Assert(rc == ERROR_SUCCESS);
2106 }
2107 }
2108 }
2109 else
2110 Assert(rc == ERROR_FILE_NOT_FOUND);
2111 }
2112}
2113
2114
2115/**
2116 * Hack to clean out obsolete typelibs on development boxes and such.
2117 */
2118static void vbpsRemoveOldMessSub(REGSAM fSamWow)
2119{
2120 /*
2121 * Note! The worker procedures does not use the default destination,
2122 * because it's much much simpler to enumerate alternative locations.
2123 */
2124 VBPSREGSTATE State;
2125 LRESULT rc = vbpsRegInit(&State, HKEY_CLASSES_ROOT, NULL, true /*fDelete*/, false /*fUpdate*/, fSamWow);
2126 if (rc == ERROR_SUCCESS)
2127 {
2128 vbpsRegAddAltDelete(&State, HKEY_CURRENT_USER, "Software\\Classes");
2129 vbpsRegAddAltDelete(&State, HKEY_LOCAL_MACHINE, "Software\\Classes");
2130 vbpsRegAddAltDelete(&State, HKEY_CLASSES_ROOT, NULL);
2131
2132 vbpsRemoveOldInterfaces(&State);
2133 vbpsRemoveOldClassIDs(&State);
2134 vbpsRemoveOldTypeLibs(&State);
2135 }
2136 vbpsRegTerm(&State);
2137}
2138
2139
2140/**
2141 * Hack to clean out obsolete typelibs on development boxes and such.
2142 */
2143static void removeOldMess(void)
2144{
2145 vbpsRemoveOldMessSub(0 /*fSamWow*/);
2146#if ARCH_BITS == 64 || defined(VBOX_IN_32_ON_64_MAIN_API)
2147 vbpsRemoveOldMessSub(KEY_WOW64_32KEY);
2148#endif
2149}
2150
2151
2152
2153/**
2154 * Register the interfaces proxied by this DLL, and to avoid duplication and
2155 * minimize work the VBox type library, classes and servers are also registered.
2156 *
2157 * This is normally only used by developers via comregister.cmd and the heat.exe
2158 * tool during MSI creation. The only situation where users may end up here is
2159 * if they're playing around or we recommend it as a solution to COM problems.
2160 * So, no problem if this approach is less gentle, though we leave the cleaning
2161 * up of orphaned interfaces to DllUnregisterServer.
2162 *
2163 * @returns COM status code.
2164 */
2165HRESULT STDAPICALLTYPE DllRegisterServer(void)
2166{
2167 HRESULT hrc;
2168
2169 /*
2170 * Register the type library first.
2171 */
2172 ITypeLib *pITypeLib;
2173 WCHAR wszDllName[MAX_PATH];
2174 DWORD cwcRet = GetModuleFileNameW(g_hDllSelf, wszDllName, RT_ELEMENTS(wszDllName));
2175 AssertReturn(cwcRet > 0 && cwcRet < RT_ELEMENTS(wszDllName), CO_E_PATHTOOLONG);
2176
2177 hrc = LoadTypeLib(wszDllName, &pITypeLib);
2178 AssertMsgReturn(SUCCEEDED(hrc), ("%Rhrc\n", hrc), hrc);
2179 hrc = RegisterTypeLib(pITypeLib, wszDllName, NULL /*pszHelpDir*/);
2180 pITypeLib->lpVtbl->Release(pITypeLib);
2181 AssertMsgReturn(SUCCEEDED(hrc), ("%Rhrc\n", hrc), hrc);
2182
2183 /*
2184 * Register proxy stub.
2185 */
2186 hrc = NdrDllRegisterProxy(g_hDllSelf, &g_apProxyFiles[0], &g_ProxyClsId); /* see DLLREGISTRY_ROUTINES in RpcProxy.h */
2187 AssertMsgReturn(SUCCEEDED(hrc), ("%Rhrc\n", hrc), hrc);
2188
2189 /*
2190 * Register the VBox modules and classes.
2191 */
2192 vbpsDllPathToVBoxDir(wszDllName);
2193 hrc = RegisterXidlModulesAndClasses(wszDllName, true /*fDelete*/, true /*fUpdate*/);
2194 AssertMsgReturn(SUCCEEDED(hrc), ("%Rhrc\n", hrc), hrc);
2195
2196 return S_OK;
2197}
2198
2199
2200/**
2201 * Reverse of DllRegisterServer.
2202 *
2203 * This is normally only used by developers via comregister.cmd. Users may be
2204 * asked to perform it in order to fix some COM issue. So, it's OK if we spend
2205 * some extra time and clean up orphaned interfaces, because developer boxes
2206 * will end up with a bunch of those as interface UUIDs changes.
2207 *
2208 * @returns COM status code.
2209 */
2210HRESULT STDAPICALLTYPE DllUnregisterServer(void)
2211{
2212 HRESULT hrc = S_OK;
2213 HRESULT hrc2;
2214
2215 /*
2216 * Unregister the type library.
2217 *
2218 * We ignore TYPE_E_REGISTRYACCESS as that is what is returned if the
2219 * type lib hasn't been registered (W10).
2220 */
2221 hrc2 = UnRegisterTypeLib(&LIBID_VirtualBox, kTypeLibraryMajorVersion, kTypeLibraryMinorVersion,
2222 0 /*LCid*/, RT_CONCAT(SYS_WIN, ARCH_BITS));
2223 AssertMsgStmt(SUCCEEDED(hrc2) || hrc2 == TYPE_E_REGISTRYACCESS, ("%Rhrc\n", hrc2), if (SUCCEEDED(hrc)) hrc = hrc2);
2224
2225 /*
2226 * Unregister the proxy stub.
2227 *
2228 * We ignore ERROR_FILE_NOT_FOUND as that is returned if not registered (W10).
2229 */
2230 hrc2 = NdrDllUnregisterProxy(g_hDllSelf, &g_apProxyFiles[0], &g_ProxyClsId); /* see DLLREGISTRY_ROUTINES in RpcProxy.h */
2231 AssertMsgStmt( SUCCEEDED(hrc2)
2232 || hrc2 == MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, ERROR_FILE_NOT_FOUND)
2233 || hrc2 == REGDB_E_INVALIDVALUE,
2234 ("%Rhrc\n", hrc2), if (SUCCEEDED(hrc)) hrc = hrc2);
2235
2236 /*
2237 * Register the VBox modules and classes.
2238 */
2239 hrc2 = RegisterXidlModulesAndClasses(NULL, true /*fDelete*/, false /*fUpdate*/);
2240 AssertMsgStmt(SUCCEEDED(hrc2), ("%Rhrc\n", hrc2), if (SUCCEEDED(hrc)) hrc = hrc2);
2241
2242 /*
2243 * Purge old mess.
2244 */
2245 removeOldMess();
2246
2247 return hrc;
2248}
2249
2250
2251/**
2252 * Gently update the COM registrations for VirtualBox.
2253 *
2254 * API that com::Initialize (VBoxCOM/initterm.cpp) calls the first time COM is
2255 * initialized in a process. ASSUMES that the caller has initialized IPRT.
2256 *
2257 * @returns Windows error code.
2258 */
2259DECLEXPORT(uint32_t) VbpsUpdateRegistrations(void)
2260{
2261 LSTATUS rc;
2262 VBPSREGSTATE State;
2263#ifdef VBOX_IN_32_ON_64_MAIN_API
2264 bool const fIs32On64 = true;
2265#else
2266 bool const fIs32On64 = false;
2267#endif
2268
2269 /** @todo Should probably skip this when VBoxSVC is already running... Use
2270 * some mutex or something for checking. */
2271
2272 /*
2273 * Find the VirtualBox application directory first.
2274 */
2275 WCHAR wszVBoxDir[MAX_PATH];
2276 DWORD cwcRet = GetModuleFileNameW(g_hDllSelf, wszVBoxDir, RT_ELEMENTS(wszVBoxDir));
2277 AssertReturn(cwcRet > 0 && cwcRet < RT_ELEMENTS(wszVBoxDir), ERROR_BUFFER_OVERFLOW);
2278 vbpsDllPathToVBoxDir(wszVBoxDir);
2279
2280 /*
2281 * Update registry entries for the current CPU bitness.
2282 */
2283 rc = vbpsRegInit(&State, HKEY_CLASSES_ROOT, NULL, false /*fDelete*/, true /*fUpdate*/, 0);
2284 if (rc == ERROR_SUCCESS && !vbpsIsUpToDate(&State))
2285 {
2286 vbpsUpdateTypeLibRegistration(&State, wszVBoxDir, fIs32On64);
2287 vbpsUpdateProxyStubRegistration(&State, wszVBoxDir, fIs32On64);
2288 vbpsUpdateInterfaceRegistrations(&State);
2289 RegisterXidlModulesAndClassesGenerated(&State, wszVBoxDir, fIs32On64);
2290 vbpsMarkUpToDate(&State);
2291 rc = State.rc;
2292 }
2293 vbpsRegTerm(&State);
2294
2295
2296#if (ARCH_BITS == 64 && defined(VBOX_WITH_32_ON_64_MAIN_API)) /*|| defined(VBOX_IN_32_ON_64_MAIN_API) ??*/
2297 /*
2298 * Update registry entries for the other CPU bitness.
2299 */
2300 if (rc == ERROR_SUCCESS)
2301 {
2302 rc = vbpsRegInit(&State, HKEY_CLASSES_ROOT, NULL, false /*fDelete*/, true /*fUpdate*/,
2303 !fIs32On64 ? KEY_WOW64_32KEY : KEY_WOW64_64KEY);
2304 if (rc == ERROR_SUCCESS && !vbpsIsUpToDate(&State))
2305 {
2306 vbpsUpdateTypeLibRegistration(&State, wszVBoxDir, !fIs32On64);
2307 vbpsUpdateProxyStubRegistration(&State, wszVBoxDir, !fIs32On64);
2308 vbpsUpdateInterfaceRegistrations(&State);
2309 RegisterXidlModulesAndClassesGenerated(&State, wszVBoxDir, !fIs32On64);
2310 vbpsMarkUpToDate(&State);
2311 rc = State.rc;
2312 }
2313 vbpsRegTerm(&State);
2314 }
2315#endif
2316
2317 return VINF_SUCCESS;
2318}
2319
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