- Update to r53061
[reactos.git] / dll / win32 / msvcrt / dllmain.c
1 /* $Id$
2 *
3 * dllmain.c
4 *
5 * ReactOS MSVCRT.DLL Compatibility Library
6 *
7 * THIS SOFTWARE IS NOT COPYRIGHTED
8 *
9 * This source code is offered for use in the public domain. You may
10 * use, modify or distribute it freely.
11 *
12 * This code is distributed in the hope that it will be useful but
13 * WITHOUT ANY WARRANTY. ALL WARRENTIES, EXPRESS OR IMPLIED ARE HEREBY
14 * DISCLAMED. This includes but is not limited to warrenties of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * $Revision: 1.24 $
18 * $Author$
19 * $Date$
20 *
21 */
22
23 #include <precomp.h>
24 #include <internal/wine/msvcrt.h>
25 #include <locale.h>
26 #include <mbctype.h>
27
28 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
30
31 /* EXTERNAL PROTOTYPES ********************************************************/
32
33 extern int BlockEnvToEnvironA(void);
34 extern int BlockEnvToEnvironW(void);
35 extern void FreeEnvironment(char **environment);
36 extern void _atexit_cleanup(void);
37
38 extern unsigned int _osplatform;
39 extern unsigned int _osver;
40 extern unsigned int _winminor;
41 extern unsigned int _winmajor;
42 extern unsigned int _winver;
43
44 extern char* _acmdln; /* pointer to ascii command line */
45 extern wchar_t* _wcmdln; /* pointer to wide character command line */
46 #undef _environ
47 extern char** _environ; /* pointer to environment block */
48 extern char** __initenv; /* pointer to initial environment block */
49 extern wchar_t** _wenviron; /* pointer to environment block */
50 extern wchar_t** __winitenv; /* pointer to initial environment block */
51
52
53 /* LIBRARY GLOBAL VARIABLES ***************************************************/
54
55 HANDLE hHeap = NULL; /* handle for heap */
56
57
58 /* LIBRARY ENTRY POINT ********************************************************/
59
60 BOOL
61 WINAPI
62 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
63 {
64 OSVERSIONINFOW osvi;
65 switch (dwReason)
66 {
67 case DLL_PROCESS_ATTACH://1
68 /* initialize version info */
69 //DPRINT1("Process Attach %d\n", nAttachCount);
70 //DPRINT1("Process Attach\n");
71 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
72 GetVersionExW( &osvi );
73 _winver = (osvi.dwMajorVersion << 8) | osvi.dwMinorVersion;
74 _winmajor = osvi.dwMajorVersion;
75 _winminor = osvi.dwMinorVersion;
76 _osplatform = osvi.dwPlatformId;
77 _osver = osvi.dwBuildNumber;
78 hHeap = HeapCreate(0, 100000, 0);
79 if (hHeap == NULL)
80 return FALSE;
81
82 /* create tls stuff */
83 if (!CreateThreadData())
84 return FALSE;
85
86 if (BlockEnvToEnvironA() < 0)
87 return FALSE;
88
89 if (BlockEnvToEnvironW() < 0)
90 {
91 FreeEnvironment(_environ);
92 return FALSE;
93 }
94
95 _acmdln = _strdup(GetCommandLineA());
96 _wcmdln = _wcsdup(GetCommandLineW());
97
98 /* FIXME: more initializations... */
99
100 /* Initialization of the WINE code */
101 msvcrt_init_mt_locks();
102 msvcrt_init_io();
103 setlocale(0, "C");
104 //_setmbcp(_MB_CP_LOCALE);
105
106 TRACE("Attach done\n");
107 break;
108
109 case DLL_THREAD_ATTACH:
110 break;
111
112 case DLL_THREAD_DETACH:
113 FreeThreadData(NULL);
114 break;
115
116 case DLL_PROCESS_DETACH:
117 //DPRINT1("Detach %d\n", nAttachCount);
118 //DPRINT("Detach\n");
119 /* FIXME: more cleanup... */
120 /* Deinit of the WINE code */
121 msvcrt_free_io();
122 msvcrt_free_mt_locks();
123
124 _atexit_cleanup();
125
126
127 /* destroy tls stuff */
128 DestroyThreadData();
129
130 if (__winitenv && __winitenv != _wenviron)
131 FreeEnvironment((char**)__winitenv);
132 if (_wenviron)
133 FreeEnvironment((char**)_wenviron);
134
135 if (__initenv && __initenv != _environ)
136 FreeEnvironment(__initenv);
137 if (_environ)
138 FreeEnvironment(_environ);
139
140 /* destroy heap */
141 HeapDestroy(hHeap);
142
143 TRACE("Detach done\n");
144 break;
145 }
146
147 return TRUE;
148 }
149
150 /* EOF */