imm32: Move ImmAssociateContext(Ex) around.
[wine.git] / dlls / msvcrt / main.c
blobe4bcfe35681fcf4a2aa3cb426801822fb1a08492
1 /*
2 * msvcrt.dll initialisation functions
4 * Copyright 2000 Jon Griffiths
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.
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.
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
20 #include <locale.h>
21 #include "msvcrt.h"
22 #include "winternl.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28 /* Index to TLS */
29 DWORD msvcrt_tls_index;
31 static const char* msvcrt_get_reason(DWORD reason)
33 switch (reason)
35 case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
36 case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
37 case DLL_THREAD_ATTACH: return "DLL_THREAD_ATTACH";
38 case DLL_THREAD_DETACH: return "DLL_THREAD_DETACH";
40 return "UNKNOWN";
43 static inline BOOL msvcrt_init_tls(void)
45 msvcrt_tls_index = TlsAlloc();
47 if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
49 ERR("TlsAlloc() failed!\n");
50 return FALSE;
52 return TRUE;
55 static inline BOOL msvcrt_free_tls(void)
57 if (!TlsFree(msvcrt_tls_index))
59 ERR("TlsFree() failed!\n");
60 return FALSE;
62 return TRUE;
65 static inline void msvcrt_free_tls_mem(void)
67 thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
69 if (tls)
71 free(tls->efcvt_buffer);
72 free(tls->asctime_buffer);
73 free(tls->wasctime_buffer);
74 free(tls->strerror_buffer);
75 free(tls->wcserror_buffer);
76 free(tls->time_buffer);
77 free(tls->tmpnam_buffer);
78 free(tls->wtmpnam_buffer);
79 if(tls->locale_flags & LOCALE_FREE) {
80 free_locinfo(tls->locinfo);
81 free_mbcinfo(tls->mbcinfo);
84 HeapFree(GetProcessHeap(), 0, tls);
87 /*********************************************************************
88 * Init
90 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
92 TRACE("(%p, %s, %p) pid(%lx), tid(%lx), tls(%lu)\n",
93 hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
94 GetCurrentProcessId(), GetCurrentThreadId(),
95 msvcrt_tls_index);
97 switch (fdwReason)
99 case DLL_PROCESS_ATTACH:
100 msvcrt_init_exception(hinstDLL);
101 if(!msvcrt_init_heap())
102 return FALSE;
103 if(!msvcrt_init_tls()) {
104 msvcrt_destroy_heap();
105 return FALSE;
107 msvcrt_init_mt_locks();
108 if(!msvcrt_init_locale()) {
109 msvcrt_free_locks();
110 msvcrt_free_tls_mem();
111 msvcrt_destroy_heap();
112 return FALSE;
114 #if defined(__x86_64__) && _MSVCR_VER>=140
115 if(!msvcrt_init_handler4()) {
116 msvcrt_free_locks();
117 msvcrt_free_tls_mem();
118 msvcrt_destroy_heap();
119 _free_locale(MSVCRT_locale);
120 return FALSE;
122 #endif
123 msvcrt_init_math(hinstDLL);
124 msvcrt_init_io();
125 msvcrt_init_args();
126 msvcrt_init_signals();
127 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
128 msvcrt_init_concurrency(hinstDLL);
129 #endif
130 #if _MSVCR_VER == 0
131 /* don't allow unloading msvcrt, we can't setup file handles twice */
132 LdrAddRefDll( LDR_ADDREF_DLL_PIN, hinstDLL );
133 #elif _MSVCR_VER >= 80
134 _set_printf_count_output(0);
135 #endif
136 msvcrt_init_clock();
137 TRACE("finished process init\n");
138 break;
139 case DLL_THREAD_ATTACH:
140 #if defined(__x86_64__) && _MSVCR_VER>=140
141 msvcrt_attach_handler4();
142 #endif
143 break;
144 case DLL_PROCESS_DETACH:
145 msvcrt_free_io();
146 if (lpvReserved) break;
147 msvcrt_free_popen_data();
148 msvcrt_free_locks();
149 msvcrt_free_console();
150 msvcrt_free_args();
151 msvcrt_free_signals();
152 msvcrt_free_tls_mem();
153 if (!msvcrt_free_tls())
154 return FALSE;
155 #if defined(__x86_64__) && _MSVCR_VER>=140
156 msvcrt_free_handler4();
157 #endif
158 _free_locale(MSVCRT_locale);
159 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
160 msvcrt_free_scheduler_thread();
161 msvcrt_free_concurrency();
162 #endif
163 msvcrt_destroy_heap();
164 TRACE("finished process free\n");
165 break;
166 case DLL_THREAD_DETACH:
167 msvcrt_free_tls_mem();
168 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
169 msvcrt_free_scheduler_thread();
170 #endif
171 TRACE("finished thread free\n");
172 break;
174 return TRUE;