VirtualBox

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

Last change on this file since 65787 was 65088, checked in by vboxsync, 8 years ago

Main: doxygen fixes

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

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