branch update for HEAD-2003021201
[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
46
47 /* LIBRARY GLOBAL VARIABLES ***************************************************/
48
49 static int nAttachCount = 0;
50
51 HANDLE hHeap = NULL;        /* handle for heap */
52
53
54 /* LIBRARY ENTRY POINT ********************************************************/
55
56 BOOL
57 STDCALL
58 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
59 {
60     switch (dwReason)
61     {
62     case DLL_PROCESS_ATTACH://1
63 #if 0
64 #else
65         /* initialize version info */
66         DPRINT("Attach %d\n", nAttachCount);
67         _osver = GetVersion();
68         _winmajor = (_osver >> 8) & 0xFF;
69         _winminor = _osver & 0xFF;
70         _winver = (_winmajor << 8) + _winminor;
71         _osver = (_osver >> 16) & 0xFFFF;
72         if (hHeap == NULL || hHeap == INVALID_HANDLE_VALUE)
73         {
74             hHeap = HeapCreate(0, 100000, 0);
75             if (hHeap == NULL || hHeap == INVALID_HANDLE_VALUE)
76             {
77                 return FALSE;
78             }
79         }
80         if (nAttachCount==0)
81         {
82             if (!__fileno_init()) {
83                 HeapDestroy(hHeap);
84                 hHeap = NULL;
85                 return FALSE;
86             }
87         }
88
89         /* create tls stuff */
90         if (!CreateThreadData())
91             return FALSE;
92
93         _acmdln = (char *)GetCommandLineA();
94
95         /* FIXME: This crashes all applications */
96         if (BlockEnvToEnviron() < 0)
97           return FALSE;
98
99         /* FIXME: more initializations... */
100
101         nAttachCount++;
102         DPRINT("Attach done\n");
103 #endif
104         break;
105
106     case DLL_THREAD_ATTACH://2
107         break;
108
109     case DLL_THREAD_DETACH://4
110         FreeThreadData(NULL);
111         break;
112
113     case DLL_PROCESS_DETACH://0
114         DPRINT("Detach %d\n", nAttachCount);
115         if (nAttachCount > 0)
116         {
117             nAttachCount--;
118
119             /* FIXME: more cleanup... */
120             _fcloseall();
121
122             /* destroy tls stuff */
123             DestroyThreadData();
124
125             /* destroy heap */
126             if (nAttachCount == 0)
127             {
128
129                 if (_environ)
130                 {
131                     FreeEnvironmentStringsA(_environ[0]);
132                     free(_environ);
133                     _environ = NULL;
134                 }
135 #if 1
136                 HeapDestroy(hHeap);
137                 hHeap = NULL;
138 #endif
139             }
140         DPRINT("Detach done\n");
141         }
142         break;
143     }
144
145     return TRUE;
146 }
147
148 /* EOF */