xref: /reactos/dll/win32/msvcrt40/msvcrt40.c (revision 85fc290b)
1 /*
2  * msvcrt40 main file
3  *
4  * Copyright (C) 2007 Louis Lenders
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #define WIN32_NO_STATUS
22 
23 #include <stdarg.h>
24 //#include <stdio.h>
25 #define _CRT_PRECOMP_H
26 #include <internal/tls.h>
27 //#include <stdlib.h>
28 //#include <windows.h>
29 #include <internal/wine/msvcrt.h>
30 #include <internal/locale.h>
31 //#include <locale.h>
32 //#include <mbctype.h>
33 
34 #include <wine/debug.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 
37 /* EXTERNAL PROTOTYPES ********************************************************/
38 
39 extern int BlockEnvToEnvironA(void);
40 extern int BlockEnvToEnvironW(void);
41 extern void FreeEnvironment(char **environment);
42 
43 extern unsigned int _osplatform;
44 extern unsigned int _osver;
45 extern unsigned int _winminor;
46 extern unsigned int _winmajor;
47 extern unsigned int _winver;
48 
49 extern char* _acmdln;        /* pointer to ascii command line */
50 extern wchar_t* _wcmdln;     /* pointer to wide character command line */
51 #undef _environ
52 extern char** _environ;      /* pointer to environment block */
53 extern char** __initenv;     /* pointer to initial environment block */
54 extern wchar_t** _wenviron;  /* pointer to environment block */
55 extern wchar_t** __winitenv; /* pointer to initial environment block */
56 
57 extern BOOL msvcrt_init_heap(void);
58 
59 /* LIBRARY ENTRY POINT ********************************************************/
60 
61 BOOL
62 WINAPI
63 DllMain(PVOID hinstDll, ULONG dwReason, PVOID reserved)
64 {
65     OSVERSIONINFOW osvi;
66     switch (dwReason)
67     {
68     case DLL_PROCESS_ATTACH:
69         /* initialize version info */
70         TRACE("Process Attach\n");
71         osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
72         GetVersionExW( &osvi );
73         _winver     = (osvi.dwMajorVersion << 8) | osvi.dwMinorVersion;
74         _winmajor   = osvi.dwMajorVersion;
75         _winminor   = osvi.dwMinorVersion;
76         _osplatform = osvi.dwPlatformId;
77         _osver      = osvi.dwBuildNumber;
78 
79         /* create tls stuff */
80         if (!msvcrt_init_tls())
81           return FALSE;
82 
83         if (!msvcrt_init_heap())
84             return FALSE;
85 
86         if (BlockEnvToEnvironA() < 0)
87             return FALSE;
88 
89         if (BlockEnvToEnvironW() < 0)
90         {
91             FreeEnvironment(_environ);
92             return FALSE;
93         }
94 
95         _acmdln = _strdup(GetCommandLineA());
96         _wcmdln = _wcsdup(GetCommandLineW());
97 
98         /* Initialization of the WINE code */
99         msvcrt_init_mt_locks();
100         //msvcrt_init_math();
101         msvcrt_init_io();
102         //msvcrt_init_console();
103         //msvcrt_init_args();
104         //msvcrt_init_signals();
105         TRACE("Attach done\n");
106         break;
107 
108     case DLL_THREAD_ATTACH:
109         //msvcrt_get_thread_data creates data when first called
110         break;
111 
112     case DLL_THREAD_DETACH:
113         msvcrt_free_tls_mem();
114         break;
115 
116     case DLL_PROCESS_DETACH:
117         TRACE("Detach\n");
118         /* Deinit of the WINE code */
119         msvcrt_free_io();
120         if (reserved) break;
121         msvcrt_free_popen_data();
122         msvcrt_free_mt_locks();
123         //msvcrt_free_console();
124         //msvcrt_free_args();
125         //msvcrt_free_signals();
126         msvcrt_free_tls_mem();
127         if (!msvcrt_free_tls())
128           return FALSE;
129         if(global_locale)
130           MSVCRT__free_locale(global_locale);
131 
132         if (__winitenv && __winitenv != _wenviron)
133             FreeEnvironment((char**)__winitenv);
134         if (_wenviron)
135             FreeEnvironment((char**)_wenviron);
136 
137         if (__initenv && __initenv != _environ)
138             FreeEnvironment(__initenv);
139         if (_environ)
140             FreeEnvironment(_environ);
141 
142         TRACE("Detach done\n");
143         break;
144     }
145 
146     return TRUE;
147 }
148 
149 /* EOF */
150