1 | /* $Id: nocrt-startup-dll-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - No-CRT - Windows EXE startup code.
|
---|
4 | *
|
---|
5 | * @note Does not run static constructors and destructors!
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox base platform packages, as
|
---|
12 | * available from https://www.virtualbox.org.
|
---|
13 | *
|
---|
14 | * This program is free software; you can redistribute it and/or
|
---|
15 | * modify it under the terms of the GNU General Public License
|
---|
16 | * as published by the Free Software Foundation, in version 3 of the
|
---|
17 | * License.
|
---|
18 | *
|
---|
19 | * This program is distributed in the hope that it will be useful, but
|
---|
20 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
22 | * General Public License for more details.
|
---|
23 | *
|
---|
24 | * You should have received a copy of the GNU General Public License
|
---|
25 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
26 | *
|
---|
27 | * The contents of this file may alternatively be used under the terms
|
---|
28 | * of the Common Development and Distribution License Version 1.0
|
---|
29 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
30 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
31 | * CDDL are applicable instead of those of the GPL.
|
---|
32 | *
|
---|
33 | * You may elect to license modified versions of this file under the
|
---|
34 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
35 | *
|
---|
36 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
37 | */
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Header Files *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | #include "internal/iprt.h"
|
---|
44 | #include "internal/process.h"
|
---|
45 |
|
---|
46 | #include <iprt/nt/nt-and-windows.h>
|
---|
47 | #include <iprt/getopt.h>
|
---|
48 | #include <iprt/message.h>
|
---|
49 | #include <iprt/path.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include <iprt/utf16.h>
|
---|
52 |
|
---|
53 | #ifdef IPRT_NO_CRT
|
---|
54 | # include <iprt/asm.h>
|
---|
55 | # include <iprt/nocrt/stdlib.h>
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #include "internal/compiler-vcc.h"
|
---|
59 |
|
---|
60 |
|
---|
61 | /*********************************************************************************************************************************
|
---|
62 | * Global Variables *
|
---|
63 | *********************************************************************************************************************************/
|
---|
64 | static volatile int32_t g_cAttached = 0;
|
---|
65 |
|
---|
66 |
|
---|
67 | /*********************************************************************************************************************************
|
---|
68 | * Internal Functions *
|
---|
69 | *********************************************************************************************************************************/
|
---|
70 | extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID pvReserved);
|
---|
71 |
|
---|
72 |
|
---|
73 | DECL_NO_INLINE(static, BOOL) rtVccDllMainForward(HINSTANCE hInstance, DWORD dwReason, LPVOID pvReserved)
|
---|
74 | {
|
---|
75 | return DllMain(hInstance, dwReason, pvReserved);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 |
|
---|
80 | DECL_NO_INLINE(static, BOOL) rtVccDllMainProcessAttach(HINSTANCE hInstance, LPVOID pvReserved)
|
---|
81 | {
|
---|
82 | /*
|
---|
83 | * Initialize the CRT the first time thru.
|
---|
84 | */
|
---|
85 | if (g_cAttached == 0)
|
---|
86 | {
|
---|
87 | rtVccWinInitProcExecPath();
|
---|
88 |
|
---|
89 | int rc = rtVccInitializersRunInit();
|
---|
90 | if (RT_FAILURE(rc))
|
---|
91 | return FALSE;
|
---|
92 | }
|
---|
93 | g_cAttached++;
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Call the DllMain function.
|
---|
97 | */
|
---|
98 | BOOL fRet = rtVccDllMainForward(hInstance, DLL_PROCESS_ATTACH, pvReserved);
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * On failure, we call the DllMain function again, decrement the init counter
|
---|
102 | * and probably run termination callbacks.
|
---|
103 | */
|
---|
104 | if (!fRet)
|
---|
105 | {
|
---|
106 | rtVccDllMainForward(hInstance, DLL_PROCESS_DETACH, pvReserved);
|
---|
107 | if (--g_cAttached == 0)
|
---|
108 | {
|
---|
109 | rtVccTermRunAtExit();
|
---|
110 | rtVccInitializersRunTerm();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | return fRet;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | DECL_NO_INLINE(static, BOOL) rtVccDllMainProcessDetach(HINSTANCE hInstance, LPVOID pvReserved)
|
---|
118 | {
|
---|
119 | /*
|
---|
120 | * Make sure there isn't an imbalance before calling DllMain and shutting
|
---|
121 | * down our own internals.
|
---|
122 | */
|
---|
123 | if (g_cAttached <= 0)
|
---|
124 | return FALSE;
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Call DllMain.
|
---|
128 | */
|
---|
129 | BOOL fRet = rtVccDllMainForward(hInstance, DLL_PROCESS_DETACH, pvReserved);
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * Work g_cAttached and probably do uninitialization. We'll do this regardless
|
---|
133 | * of what DllMain returned.
|
---|
134 | */
|
---|
135 | if (--g_cAttached == 0)
|
---|
136 | {
|
---|
137 | rtVccTermRunAtExit();
|
---|
138 | rtVccInitializersRunTerm();
|
---|
139 | }
|
---|
140 | return fRet;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | extern "C" BOOL WINAPI _DllMainCRTStartup(HINSTANCE hInstance, DWORD dwReason, LPVOID pvReserved)
|
---|
145 | {
|
---|
146 | switch (dwReason)
|
---|
147 | {
|
---|
148 | case DLL_PROCESS_ATTACH:
|
---|
149 | #ifdef RT_ARCH_X86
|
---|
150 | rtVccWinInitBssOnNt3((PVOID)hInstance);
|
---|
151 | #endif
|
---|
152 | rtVccInitSecurityCookie(); /* This function must be minimal because of this! */
|
---|
153 | return rtVccDllMainProcessAttach(hInstance, pvReserved);
|
---|
154 |
|
---|
155 | case DLL_PROCESS_DETACH:
|
---|
156 | return rtVccDllMainProcessDetach(hInstance, pvReserved);
|
---|
157 |
|
---|
158 | default:
|
---|
159 | return rtVccDllMainForward(hInstance, dwReason, pvReserved);
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|