VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/win/dllmain.cpp@ 94521

Last change on this file since 94521 was 93115, checked in by vboxsync, 2 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* $Id: dllmain.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * VBoxC - COM DLL exports and DLL init/term.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "VBox/com/defs.h"
23
24#include <SessionImpl.h>
25#include <VirtualBoxClientImpl.h>
26
27#include <iprt/initterm.h>
28
29
30/*********************************************************************************************************************************
31* Global Variables *
32*********************************************************************************************************************************/
33static ATL::CComModule *g_pAtlComModule;
34
35BEGIN_OBJECT_MAP(ObjectMap)
36 OBJECT_ENTRY(CLSID_Session, Session)
37 OBJECT_ENTRY(CLSID_VirtualBoxClient, VirtualBoxClient)
38END_OBJECT_MAP()
39
40
41/////////////////////////////////////////////////////////////////////////////
42// DLL Entry Point
43
44extern "C"
45BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
46{
47 if (dwReason == DLL_PROCESS_ATTACH)
48 {
49 // idempotent, so doesn't harm, and needed for COM embedding scenario
50 RTR3InitDll(RTR3INIT_FLAGS_UNOBTRUSIVE);
51
52 g_pAtlComModule = new(ATL::CComModule);
53 if (!g_pAtlComModule)
54 return FALSE;
55
56 g_pAtlComModule->Init(ObjectMap, hInstance, &LIBID_VirtualBox);
57 DisableThreadLibraryCalls(hInstance);
58 }
59 else if (dwReason == DLL_PROCESS_DETACH)
60 {
61 if (g_pAtlComModule)
62 {
63 g_pAtlComModule->Term();
64 delete g_pAtlComModule;
65 g_pAtlComModule = NULL;
66 }
67 }
68 return TRUE;
69}
70
71/////////////////////////////////////////////////////////////////////////////
72// Used to determine whether the DLL can be unloaded by OLE
73
74STDAPI DllCanUnloadNow(void)
75{
76 AssertReturn(g_pAtlComModule, S_OK);
77 LONG const cLocks = g_pAtlComModule->GetLockCount();
78 Assert(cLocks >= VirtualBoxClient::s_cUnnecessaryAtlModuleLocks);
79 return cLocks <= VirtualBoxClient::s_cUnnecessaryAtlModuleLocks ? S_OK : S_FALSE;
80}
81
82/////////////////////////////////////////////////////////////////////////////
83// Returns a class factory to create an object of the requested type
84
85STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
86{
87 AssertReturn(g_pAtlComModule, E_UNEXPECTED);
88 return g_pAtlComModule->GetClassObject(rclsid, riid, ppv);
89}
90
91/////////////////////////////////////////////////////////////////////////////
92// DllRegisterServer - Adds entries to the system registry
93
94STDAPI DllRegisterServer(void)
95{
96#ifndef VBOX_WITH_MIDL_PROXY_STUB
97 // registers object, typelib and all interfaces in typelib
98 AssertReturn(g_pAtlComModule, E_UNEXPECTED);
99 return g_pAtlComModule->RegisterServer(TRUE);
100#else
101 return S_OK; /* VBoxProxyStub does all the work, no need to duplicate it here. */
102#endif
103}
104
105/////////////////////////////////////////////////////////////////////////////
106// DllUnregisterServer - Removes entries from the system registry
107
108STDAPI DllUnregisterServer(void)
109{
110#ifndef VBOX_WITH_MIDL_PROXY_STUB
111 AssertReturn(g_pAtlComModule, E_UNEXPECTED);
112 HRESULT hrc = g_pAtlComModule->UnregisterServer(TRUE);
113 return hrc;
114#else
115 return S_OK; /* VBoxProxyStub does all the work, no need to duplicate it here. */
116#endif
117}
118
119
120#ifdef RT_OS_WINDOWS
121/*
122 * HACK ALERT! Really ugly trick to make the VirtualBoxClient object go away
123 * when nobody uses it anymore. This is to prevent its uninit()
124 * method from accessing IVirtualBox and similar proxy stubs after
125 * COM has been officially shut down.
126 *
127 * It is simply TOO LATE to destroy the client object from DllMain/detach!
128 *
129 * This hack ASSUMES ObjectMap order.
130 * This hack is subject to a re-instantiation race.
131 */
132ULONG VirtualBoxClient::InternalRelease()
133{
134 ULONG cRefs = VirtualBoxClientWrap::InternalRelease();
135# ifdef DEBUG_bird
136 char szMsg[64];
137 RTStrPrintf(szMsg, sizeof(szMsg), "VirtualBoxClient: cRefs=%d\n", cRefs);
138 OutputDebugStringA(szMsg);
139# endif
140# if 1 /* enable ugly hack */
141 if (cRefs == 1)
142 {
143 /* Make the factory to drop its reference. */
144 if (ObjectMap[1].pCF)
145 {
146 InternalAddRef();
147
148 CMyComClassFactorySingleton<VirtualBoxClient> *pFactory;
149 pFactory = dynamic_cast<CMyComClassFactorySingleton<VirtualBoxClient> *>(ObjectMap[1].pCF);
150 Assert(pFactory);
151 if (pFactory)
152 {
153 IUnknown *pUnknown = pFactory->m_spObj;
154 pFactory->m_spObj = NULL;
155 if (pUnknown)
156 pUnknown->Release();
157 }
158
159 cRefs = VirtualBoxClientWrap::InternalRelease();
160 }
161 }
162# endif
163 return cRefs;
164}
165#endif
166
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use