bae034d040ec7a847d294321986798ac0dbaff23
[reactos.git] / reactos / lib / 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 <stdio.h>
25 #include <internal/tls.h>
26 #include <stdlib.h>
27 #include <internal/wine/msvcrt.h>
28
29 #define NDEBUG
30 #include <internal/debug.h>
31
32
33 /* EXTERNAL PROTOTYPES ********************************************************/
34
35 //void __fileno_init(void);
36 extern BOOL __fileno_init(void);
37 extern int BlockEnvToEnvironA(void);
38 extern int BlockEnvToEnvironW(void);
39 extern void FreeEnvironment(char **environment);
40 extern void _atexit_cleanup(void);
41
42 extern unsigned int _osver;
43 extern unsigned int _winminor;
44 extern unsigned int _winmajor;
45 extern unsigned int _winver;
46
47 extern char* _acmdln; /* pointer to ascii command line */
48 extern wchar_t* _wcmdln; /* pointer to wide character command line */
49 #undef _environ
50 extern char** _environ; /* pointer to environment block */
51 extern char** __initenv; /* pointer to initial environment block */
52 extern wchar_t** _wenviron; /* pointer to environment block */
53 extern wchar_t** __winitenv; /* pointer to initial environment block */
54
55
56 /* LIBRARY GLOBAL VARIABLES ***************************************************/
57
58 HANDLE hHeap = NULL; /* handle for heap */
59
60
61 /* LIBRARY ENTRY POINT ********************************************************/
62
63 BOOL
64 STDCALL
65 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
66 {
67 switch (dwReason)
68 {
69 case DLL_PROCESS_ATTACH://1
70 /* initialize version info */
71 //DPRINT1("Process Attach %d\n", nAttachCount);
72 //DPRINT1("Process Attach\n");
73 _osver = GetVersion();
74 _winmajor = (_osver >> 8) & 0xFF;
75 _winminor = _osver & 0xFF;
76 _winver = (_winmajor << 8) + _winminor;
77 _osver = (_osver >> 16) & 0xFFFF;
78 hHeap = HeapCreate(0, 100000, 0);
79 if (hHeap == NULL)
80 return FALSE;
81 if (!__fileno_init())
82 return FALSE;
83
84 /* create tls stuff */
85 if (!CreateThreadData())
86 return FALSE;
87
88 if (BlockEnvToEnvironA() < 0)
89 return FALSE;
90
91 if (BlockEnvToEnvironW() < 0)
92 {
93 FreeEnvironment(_environ);
94 return FALSE;
95 }
96
97 _acmdln = _strdup(GetCommandLineA());
98 _wcmdln = _wcsdup(GetCommandLineW());
99
100 /* FIXME: more initializations... */
101
102 /* FIXME: Initialization of the WINE code */
103 msvcrt_init_mt_locks();
104
105 DPRINT("Attach done\n");
106 break;
107
108 case DLL_THREAD_ATTACH://2
109 break;
110
111 case DLL_THREAD_DETACH://4
112 FreeThreadData(NULL);
113 break;
114
115 case DLL_PROCESS_DETACH://0
116 //DPRINT1("Detach %d\n", nAttachCount);
117 //DPRINT("Detach\n");
118 /* FIXME: more cleanup... */
119 _fcloseall();
120 _atexit_cleanup();
121
122 /* destroy tls stuff */
123 DestroyThreadData();
124
125 if (__winitenv && __winitenv != _wenviron)
126 FreeEnvironment((char**)__winitenv);
127 if (_wenviron)
128 FreeEnvironment((char**)_wenviron);
129
130 if (__initenv && __initenv != _environ)
131 FreeEnvironment(__initenv);
132 if (_environ)
133 FreeEnvironment(_environ);
134
135 /* destroy heap */
136 HeapDestroy(hHeap);
137
138 DPRINT("Detach done\n");
139 break;
140 }
141
142 return TRUE;
143 }
144
145 /* EOF */