xref: /reactos/dll/win32/msvcrt/dllmain.c (revision 3e1f4074)
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 BOOL crt_process_init(void);
25 
26 extern void FreeEnvironment(char **environment);
27 
28 #undef _environ
29 extern char** _environ;      /* pointer to environment block */
30 extern char** __initenv;     /* pointer to initial environment block */
31 extern wchar_t** _wenviron;  /* pointer to environment block */
32 extern wchar_t** __winitenv; /* pointer to initial environment block */
33 
34 /* LIBRARY ENTRY POINT ********************************************************/
35 
36 BOOL
37 WINAPI
38 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
39 {
40     switch (dwReason)
41     {
42     case DLL_PROCESS_ATTACH:
43 
44         TRACE("Process Attach\n");
45 
46         if (!crt_process_init())
47         {
48             ERR("crt_init() failed!\n");
49             return FALSE;
50         }
51 
52         TRACE("Attach done\n");
53         break;
54 
55     case DLL_THREAD_ATTACH:
56         //msvcrt_get_thread_data creates data when first called
57         break;
58 
59     case DLL_THREAD_DETACH:
60         msvcrt_free_tls_mem();
61         break;
62 
63     case DLL_PROCESS_DETACH:
64         TRACE("Detach\n");
65         /* Deinit of the WINE code */
66         msvcrt_free_io();
67         if (reserved) break;
68         msvcrt_free_popen_data();
69         msvcrt_free_mt_locks();
70         //msvcrt_free_console();
71         //msvcrt_free_args();
72         //msvcrt_free_signals();
73         msvcrt_free_tls_mem();
74         if (!msvcrt_free_tls())
75           return FALSE;
76         if(global_locale)
77           MSVCRT__free_locale(global_locale);
78 
79         if (__winitenv && __winitenv != _wenviron)
80             FreeEnvironment((char**)__winitenv);
81         if (_wenviron)
82             FreeEnvironment((char**)_wenviron);
83 
84         if (__initenv && __initenv != _environ)
85             FreeEnvironment(__initenv);
86         if (_environ)
87             FreeEnvironment(_environ);
88 
89         TRACE("Detach done\n");
90         break;
91     }
92 
93     return TRUE;
94 }
95 
96 /* FIXME: This hack is required to prevent the VC linker from linking these
97    exports to the functions from libntdll. See CORE-10753 */
98 #ifdef _MSC_VER
99 #ifdef _M_IX86
100 #pragma comment(linker, "/include:__vsnprintf")
101 #pragma comment(linker, "/include:_bsearch")
102 #pragma comment(linker, "/include:_strcspn")
103 #else
104 #pragma comment(linker, "/include:_vsnprintf")
105 #pragma comment(linker, "/include:bsearch")
106 #pragma comment(linker, "/include:strcspn")
107 #endif // _M_IX86
108 #endif // _MSC_VER
109 
110 /* EOF */
111