hack into private crt headers
[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/msvcrtdbg.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
41 extern unsigned int _osver;
42 extern unsigned int _winminor;
43 extern unsigned int _winmajor;
44 extern unsigned int _winver;
45
46 extern char* _acmdln; /* pointer to ascii command line */
47 extern wchar_t* _wcmdln; /* pointer to wide character command line */
48 #undef _environ
49 extern char** _environ; /* pointer to environment block */
50 extern char** __initenv; /* pointer to initial environment block */
51 extern wchar_t** _wenviron; /* pointer to environment block */
52 extern wchar_t** __winitenv; /* pointer to initial environment block */
53
54
55 /* LIBRARY GLOBAL VARIABLES ***************************************************/
56
57 HANDLE hHeap = NULL; /* handle for heap */
58
59
60 /* LIBRARY ENTRY POINT ********************************************************/
61
62 BOOL
63 STDCALL
64 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
65 {
66 switch (dwReason)
67 {
68 case DLL_PROCESS_ATTACH://1
69 /* initialize version info */
70 //DPRINT1("Process Attach %d\n", nAttachCount);
71 //DPRINT1("Process Attach\n");
72 _osver = GetVersion();
73 _winmajor = (_osver >> 8) & 0xFF;
74 _winminor = _osver & 0xFF;
75 _winver = (_winmajor << 8) + _winminor;
76 _osver = (_osver >> 16) & 0xFFFF;
77 hHeap = HeapCreate(0, 100000, 0);
78 if (hHeap == NULL)
79 return FALSE;
80 if (!__fileno_init())
81 return FALSE;
82
83 /* create tls stuff */
84 if (!CreateThreadData())
85 return FALSE;
86
87 if (BlockEnvToEnvironA() < 0)
88 return FALSE;
89
90 if (BlockEnvToEnvironW() < 0)
91 {
92 FreeEnvironment((char**)_wenviron);
93 return FALSE;
94 }
95
96 _acmdln = _strdup(GetCommandLineA());
97 _wcmdln = _wcsdup(GetCommandLineW());
98
99 /* FIXME: more initializations... */
100
101 /* FIXME: Initialization of the WINE code */
102 msvcrt_init_mt_locks();
103
104 DPRINT("Attach done\n");
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 //DPRINT1("Detach %d\n", nAttachCount);
116 //DPRINT("Detach\n");
117 /* FIXME: more cleanup... */
118 _fcloseall();
119
120 /* destroy tls stuff */
121 DestroyThreadData();
122
123 if (__winitenv && __winitenv != _wenviron)
124 FreeEnvironment((char**)__winitenv);
125 if (_wenviron)
126 FreeEnvironment((char**)_wenviron);
127
128 if (__initenv && __initenv != _environ)
129 FreeEnvironment(__initenv);
130 if (_environ)
131 FreeEnvironment(_environ);
132
133 /* destroy heap */
134 HeapDestroy(hHeap);
135
136 DPRINT("Detach done\n");
137 break;
138 }
139
140 return TRUE;
141 }
142
143 /* EOF */