VirtualBox

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

Last change on this file since 72332 was 71717, checked in by vboxsync, 7 years ago

s/VBOX_SDS_CLIENT_WATCHER/VBOX_WITH_SDS_CLIENT_WATCHER/g

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