1 | /* $Id: nocrt-startup-dll-win.cpp 95887 2022-07-28 01:40:23Z 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-2022 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * The contents of this file may alternatively be used under the terms
|
---|
20 | * of the Common Development and Distribution License Version 1.0
|
---|
21 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
22 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
23 | * CDDL are applicable instead of those of the GPL.
|
---|
24 | *
|
---|
25 | * You may elect to license modified versions of this file under the
|
---|
26 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
27 | */
|
---|
28 |
|
---|
29 |
|
---|
30 | /*********************************************************************************************************************************
|
---|
31 | * Header Files *
|
---|
32 | *********************************************************************************************************************************/
|
---|
33 | #include "internal/iprt.h"
|
---|
34 | #include "internal/process.h"
|
---|
35 |
|
---|
36 | #include <iprt/nt/nt-and-windows.h>
|
---|
37 | #include <iprt/getopt.h>
|
---|
38 | #include <iprt/message.h>
|
---|
39 | #include <iprt/path.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/utf16.h>
|
---|
42 |
|
---|
43 | #ifdef IPRT_NO_CRT
|
---|
44 | # include <iprt/asm.h>
|
---|
45 | # include <iprt/nocrt/stdlib.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #include "internal/compiler-vcc.h"
|
---|
49 |
|
---|
50 |
|
---|
51 | /*********************************************************************************************************************************
|
---|
52 | * Global Variables *
|
---|
53 | *********************************************************************************************************************************/
|
---|
54 | static volatile int32_t g_cAttached = 0;
|
---|
55 |
|
---|
56 |
|
---|
57 | /*********************************************************************************************************************************
|
---|
58 | * Internal Functions *
|
---|
59 | *********************************************************************************************************************************/
|
---|
60 | extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID pvReserved);
|
---|
61 |
|
---|
62 |
|
---|
63 | DECL_NO_INLINE(static, BOOL) rtVccDllMainForward(HINSTANCE hInstance, DWORD dwReason, LPVOID pvReserved)
|
---|
64 | {
|
---|
65 | return DllMain(hInstance, dwReason, pvReserved);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | DECL_NO_INLINE(static, BOOL) rtVccDllMainProcessAttach(HINSTANCE hInstance, LPVOID pvReserved)
|
---|
71 | {
|
---|
72 | /*
|
---|
73 | * Initialize the CRT the first time thru.
|
---|
74 | */
|
---|
75 | if (g_cAttached == 0)
|
---|
76 | {
|
---|
77 | rtVccWinInitProcExecPath();
|
---|
78 |
|
---|
79 | int rc = rtVccInitializersRunInit();
|
---|
80 | if (RT_FAILURE(rc))
|
---|
81 | return FALSE;
|
---|
82 | }
|
---|
83 | g_cAttached++;
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * Call the DllMain function.
|
---|
87 | */
|
---|
88 | BOOL fRet = rtVccDllMainForward(hInstance, DLL_PROCESS_ATTACH, pvReserved);
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * On failure, we call the DllMain function again, decrement the init counter
|
---|
92 | * and probably run termination callbacks.
|
---|
93 | */
|
---|
94 | if (!fRet)
|
---|
95 | {
|
---|
96 | rtVccDllMainForward(hInstance, DLL_PROCESS_DETACH, pvReserved);
|
---|
97 | if (--g_cAttached == 0)
|
---|
98 | {
|
---|
99 | rtVccTermRunAtExit();
|
---|
100 | rtVccInitializersRunTerm();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | return fRet;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | DECL_NO_INLINE(static, BOOL) rtVccDllMainProcessDetach(HINSTANCE hInstance, LPVOID pvReserved)
|
---|
108 | {
|
---|
109 | /*
|
---|
110 | * Make sure there isn't an imbalance before calling DllMain and shutting
|
---|
111 | * down our own internals.
|
---|
112 | */
|
---|
113 | if (g_cAttached <= 0)
|
---|
114 | return FALSE;
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Call DllMain.
|
---|
118 | */
|
---|
119 | BOOL fRet = rtVccDllMainForward(hInstance, DLL_PROCESS_DETACH, pvReserved);
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Work g_cAttached and probably do uninitialization. We'll do this regardless
|
---|
123 | * of what DllMain returned.
|
---|
124 | */
|
---|
125 | if (--g_cAttached == 0)
|
---|
126 | {
|
---|
127 | rtVccTermRunAtExit();
|
---|
128 | rtVccInitializersRunTerm();
|
---|
129 | }
|
---|
130 | return fRet;
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | extern "C" BOOL WINAPI _DllMainCRTStartup(HINSTANCE hInstance, DWORD dwReason, LPVOID pvReserved)
|
---|
135 | {
|
---|
136 | switch (dwReason)
|
---|
137 | {
|
---|
138 | case DLL_PROCESS_ATTACH:
|
---|
139 | rtVccInitSecurityCookie(); /* This function must be minimal because of this! */
|
---|
140 | return rtVccDllMainProcessAttach(hInstance, pvReserved);
|
---|
141 |
|
---|
142 | case DLL_PROCESS_DETACH:
|
---|
143 | return rtVccDllMainProcessDetach(hInstance, pvReserved);
|
---|
144 |
|
---|
145 | default:
|
---|
146 | return rtVccDllMainForward(hInstance, dwReason, pvReserved);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|