xref: /reactos/dll/win32/msvcrt/dllmain.c (revision 5100859e)
1 /*
2  * dllmain.c
3  *
4  * ReactOS MSVCRT.DLL Compatibility Library
5  *
6  *  THIS SOFTWARE IS NOT COPYRIGHTED
7  *
8  *  This source code is offered for use in the public domain. You may
9  *  use, modify or distribute it freely.
10  *
11  *  This code is distributed in the hope that it will be useful but
12  *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
13  *  DISCLAMED. This includes but is not limited to warranties of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  */
17 
18 #include "precomp.h"
19 
20 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
21 
22 /* EXTERNAL PROTOTYPES ********************************************************/
23 
24 extern int BlockEnvToEnvironA(void);
25 extern int BlockEnvToEnvironW(void);
26 extern void FreeEnvironment(char **environment);
27 
28 extern unsigned int _osplatform;
29 extern unsigned int _osver;
30 extern unsigned int _winminor;
31 extern unsigned int _winmajor;
32 extern unsigned int _winver;
33 
34 extern char* _acmdln;        /* pointer to ascii command line */
35 extern wchar_t* _wcmdln;     /* pointer to wide character command line */
36 #undef _environ
37 extern char** _environ;      /* pointer to environment block */
38 extern char** __initenv;     /* pointer to initial environment block */
39 extern wchar_t** _wenviron;  /* pointer to environment block */
40 extern wchar_t** __winitenv; /* pointer to initial environment block */
41 
42 /* LIBRARY ENTRY POINT ********************************************************/
43 
44 BOOL
45 WINAPI
46 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
47 {
48     OSVERSIONINFOW osvi;
49     switch (dwReason)
50     {
51     case DLL_PROCESS_ATTACH:
52         /* initialize version info */
53         TRACE("Process Attach\n");
54         osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
55         GetVersionExW( &osvi );
56         _winver     = (osvi.dwMajorVersion << 8) | osvi.dwMinorVersion;
57         _winmajor   = osvi.dwMajorVersion;
58         _winminor   = osvi.dwMinorVersion;
59         _osplatform = osvi.dwPlatformId;
60         _osver      = osvi.dwBuildNumber;
61 
62         /* create tls stuff */
63         if (!msvcrt_init_tls())
64           return FALSE;
65 
66         if (BlockEnvToEnvironA() < 0)
67             return FALSE;
68 
69         if (BlockEnvToEnvironW() < 0)
70         {
71             FreeEnvironment(_environ);
72             return FALSE;
73         }
74 
75         _acmdln = _strdup(GetCommandLineA());
76         _wcmdln = _wcsdup(GetCommandLineW());
77 
78         /* Initialization of the WINE code */
79         msvcrt_init_mt_locks();
80         //msvcrt_init_math();
81         msvcrt_init_io();
82         //msvcrt_init_console();
83         //msvcrt_init_args();
84         //msvcrt_init_signals();
85         TRACE("Attach done\n");
86         break;
87 
88     case DLL_THREAD_ATTACH:
89         //msvcrt_get_thread_data creates data when first called
90         break;
91 
92     case DLL_THREAD_DETACH:
93         msvcrt_free_tls_mem();
94         break;
95 
96     case DLL_PROCESS_DETACH:
97         TRACE("Detach\n");
98         /* Deinit of the WINE code */
99         msvcrt_free_io();
100         if (reserved) break;
101         msvcrt_free_popen_data();
102         msvcrt_free_mt_locks();
103         //msvcrt_free_console();
104         //msvcrt_free_args();
105         //msvcrt_free_signals();
106         msvcrt_free_tls_mem();
107         if (!msvcrt_free_tls())
108           return FALSE;
109         if(global_locale)
110           MSVCRT__free_locale(global_locale);
111 
112         if (__winitenv && __winitenv != _wenviron)
113             FreeEnvironment((char**)__winitenv);
114         if (_wenviron)
115             FreeEnvironment((char**)_wenviron);
116 
117         if (__initenv && __initenv != _environ)
118             FreeEnvironment(__initenv);
119         if (_environ)
120             FreeEnvironment(_environ);
121 
122         TRACE("Detach done\n");
123         break;
124     }
125 
126     return TRUE;
127 }
128 
129 /* FIXME: This hack is required to prevent the VC linker from linking these
130    exports to the functions from libntdll. See CORE-10753 */
131 #ifdef _MSC_VER
132 #ifdef _M_IX86
133 #pragma comment(linker, "/include:__vsnprintf")
134 #pragma comment(linker, "/include:_bsearch")
135 #pragma comment(linker, "/include:_strcspn")
136 #else
137 #pragma comment(linker, "/include:_vsnprintf")
138 #pragma comment(linker, "/include:bsearch")
139 #pragma comment(linker, "/include:strcspn")
140 #endif // _M_IX86
141 #endif // _MSC_VER
142 
143 /* EOF */
144