VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use