99822fc46a72808ee403d72712894d2fe79d0a8e
[reactos.git] / reactos / 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 _osver;
39 extern unsigned int _winminor;
40 extern unsigned int _winmajor;
41 extern unsigned int _winver;
42
43 extern char* _acmdln; /* pointer to ascii command line */
44 extern wchar_t* _wcmdln; /* pointer to wide character command line */
45 #undef _environ
46 extern char** _environ; /* pointer to environment block */
47 extern char** __initenv; /* pointer to initial environment block */
48 extern wchar_t** _wenviron; /* pointer to environment block */
49 extern wchar_t** __winitenv; /* pointer to initial environment block */
50
51
52 /* LIBRARY GLOBAL VARIABLES ***************************************************/
53
54 HANDLE hHeap = NULL; /* handle for heap */
55
56
57 /* LIBRARY ENTRY POINT ********************************************************/
58
59 BOOL
60 WINAPI
61 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
62 {
63 OSVERSIONINFOW osvi;
64 switch (dwReason)
65 {
66 case DLL_PROCESS_ATTACH://1
67 /* initialize version info */
68 //DPRINT1("Process Attach %d\n", nAttachCount);
69 //DPRINT1("Process Attach\n");
70 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
71 GetVersionExW( &osvi );
72 _winver = (osvi.dwMajorVersion << 8) | osvi.dwMinorVersion;
73 _winmajor = osvi.dwMajorVersion;
74 _winminor = osvi.dwMinorVersion;
75 _osver = osvi.dwBuildNumber;
76 hHeap = HeapCreate(0, 100000, 0);
77 if (hHeap == NULL)
78 return FALSE;
79
80 /* create tls stuff */
81 if (!CreateThreadData())
82 return FALSE;
83
84 if (BlockEnvToEnvironA() < 0)
85 return FALSE;
86
87 if (BlockEnvToEnvironW() < 0)
88 {
89 FreeEnvironment(_environ);
90 return FALSE;
91 }
92
93 _acmdln = _strdup(GetCommandLineA());
94 _wcmdln = _wcsdup(GetCommandLineW());
95
96 /* FIXME: more initializations... */
97
98 /* Initialization of the WINE code */
99 msvcrt_init_mt_locks();
100 msvcrt_init_io();
101 setlocale(0, "C");
102 //_setmbcp(_MB_CP_LOCALE);
103
104 TRACE("Attach done\n");
105 break;
106
107 case DLL_THREAD_ATTACH:
108 break;
109
110 case DLL_THREAD_DETACH:
111 FreeThreadData(NULL);
112 break;
113
114 case DLL_PROCESS_DETACH:
115 //DPRINT1("Detach %d\n", nAttachCount);
116 //DPRINT("Detach\n");
117 /* FIXME: more cleanup... */
118 /* Deinit of the WINE code */
119 msvcrt_free_io();
120 msvcrt_free_mt_locks();
121
122 _atexit_cleanup();
123
124
125 /* destroy tls stuff */
126 DestroyThreadData();
127
128 if (__winitenv && __winitenv != _wenviron)
129 FreeEnvironment((char**)__winitenv);
130 if (_wenviron)
131 FreeEnvironment((char**)_wenviron);
132
133 if (__initenv && __initenv != _environ)
134 FreeEnvironment(__initenv);
135 if (_environ)
136 FreeEnvironment(_environ);
137
138 /* destroy heap */
139 HeapDestroy(hHeap);
140
141 TRACE("Detach done\n");
142 break;
143 }
144
145 return TRUE;
146 }
147
148 /* EOF */