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