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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
28 DWORD msvcrt_tls_index
;
30 static const char* msvcrt_get_reason(DWORD reason
)
34 case DLL_PROCESS_ATTACH
: return "DLL_PROCESS_ATTACH";
35 case DLL_PROCESS_DETACH
: return "DLL_PROCESS_DETACH";
36 case DLL_THREAD_ATTACH
: return "DLL_THREAD_ATTACH";
37 case DLL_THREAD_DETACH
: return "DLL_THREAD_DETACH";
42 static inline BOOL
msvcrt_init_tls(void)
44 msvcrt_tls_index
= TlsAlloc();
46 if (msvcrt_tls_index
== TLS_OUT_OF_INDEXES
)
48 ERR("TlsAlloc() failed!\n");
54 static inline BOOL
msvcrt_free_tls(void)
56 if (!TlsFree(msvcrt_tls_index
))
58 ERR("TlsFree() failed!\n");
64 static inline void msvcrt_free_tls_mem(void)
66 thread_data_t
*tls
= TlsGetValue(msvcrt_tls_index
);
70 MSVCRT_free(tls
->efcvt_buffer
);
71 MSVCRT_free(tls
->asctime_buffer
);
72 MSVCRT_free(tls
->wasctime_buffer
);
73 MSVCRT_free(tls
->strerror_buffer
);
74 MSVCRT_free(tls
->wcserror_buffer
);
75 MSVCRT_free(tls
->time_buffer
);
76 MSVCRT_free(tls
->tmpnam_buffer
);
77 MSVCRT_free(tls
->wtmpnam_buffer
);
78 if(tls
->have_locale
) {
79 free_locinfo(tls
->locinfo
);
80 free_mbcinfo(tls
->mbcinfo
);
83 HeapFree(GetProcessHeap(), 0, tls
);
86 /*********************************************************************
89 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
91 TRACE("(%p, %s, %p) pid(%x), tid(%x), tls(%u)\n",
92 hinstDLL
, msvcrt_get_reason(fdwReason
), lpvReserved
,
93 GetCurrentProcessId(), GetCurrentThreadId(),
98 case DLL_PROCESS_ATTACH
:
99 msvcrt_init_exception(hinstDLL
);
100 if(!msvcrt_init_heap())
102 if(!msvcrt_init_tls()) {
103 msvcrt_destroy_heap();
106 msvcrt_init_mt_locks();
107 if(!msvcrt_init_locale()) {
109 msvcrt_free_tls_mem();
110 msvcrt_destroy_heap();
115 msvcrt_init_console();
117 msvcrt_init_signals();
118 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
119 msvcrt_init_scheduler(hinstDLL
);
122 /* don't allow unloading msvcrt, we can't setup file handles twice */
123 LdrAddRefDll( LDR_ADDREF_DLL_PIN
, hinstDLL
);
124 #elif _MSVCR_VER >= 80
125 MSVCRT__set_printf_count_output(0);
127 TRACE("finished process init\n");
129 case DLL_THREAD_ATTACH
:
131 case DLL_PROCESS_DETACH
:
133 if (lpvReserved
) break;
134 msvcrt_free_popen_data();
136 msvcrt_free_console();
138 msvcrt_free_signals();
139 msvcrt_free_tls_mem();
140 if (!msvcrt_free_tls())
142 MSVCRT__free_locale(MSVCRT_locale
);
143 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
144 msvcrt_free_scheduler_thread();
145 msvcrt_free_scheduler();
147 msvcrt_destroy_heap();
148 TRACE("finished process free\n");
150 case DLL_THREAD_DETACH
:
151 msvcrt_free_tls_mem();
152 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
153 msvcrt_free_scheduler_thread();
155 TRACE("finished thread free\n");