blob: b42b64a37b2772b5bf87d495f1b97bfe0e5f7360 [file] [log] [blame]
Jon Griffiths1db20bf2001-01-10 23:59:25 +00001/*
2 * msvcrt.dll initialisation functions
3 *
4 * Copyright 2000 Jon Griffiths
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Jon Griffiths1db20bf2001-01-10 23:59:25 +000019 */
20#include "msvcrt.h"
21
Alexandre Julliardbd1689e2002-01-22 00:57:16 +000022#include "wine/debug.h"
23
24WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
Jon Griffiths1db20bf2001-01-10 23:59:25 +000025
26/* Index to TLS */
Dimitrie O. Paun03774622004-06-25 01:19:15 +000027DWORD msvcrt_tls_index;
Jon Griffiths1db20bf2001-01-10 23:59:25 +000028
Dmitry Timoshkovada9dc92006-12-25 16:41:14 +080029static const char* msvcrt_get_reason(DWORD reason)
30{
31 switch (reason)
32 {
33 case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
34 case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
35 case DLL_THREAD_ATTACH: return "DLL_THREAD_ATTACH";
36 case DLL_THREAD_DETACH: return "DLL_THREAD_DETACH";
37 }
38 return "UNKNOWN";
39}
Jon Griffiths1db20bf2001-01-10 23:59:25 +000040
Michael Stefaniuc814ca122007-06-19 22:49:04 +020041static inline BOOL msvcrt_init_tls(void)
42{
43 msvcrt_tls_index = TlsAlloc();
44
45 if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
46 {
47 ERR("TlsAlloc() failed!\n");
48 return FALSE;
49 }
50 return TRUE;
51}
52
53static inline BOOL msvcrt_free_tls(void)
54{
55 if (!TlsFree(msvcrt_tls_index))
56 {
57 ERR("TlsFree() failed!\n");
58 return FALSE;
59 }
60 return TRUE;
61}
62
Jon Griffiths1db20bf2001-01-10 23:59:25 +000063/*********************************************************************
64 * Init
65 */
Alexandre Julliard1e1313d2002-11-04 23:53:41 +000066BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Jon Griffiths1db20bf2001-01-10 23:59:25 +000067{
Dimitrie O. Paun03774622004-06-25 01:19:15 +000068 thread_data_t *tls;
Jon Griffiths1db20bf2001-01-10 23:59:25 +000069
Alexandre Julliard4305e6d2008-12-11 21:13:20 +010070 TRACE("(%p, %s, %p) pid(%x), tid(%x), tls(%u)\n",
Francois Gouget203a8f82001-04-10 21:16:07 +000071 hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
Alexandre Julliardac13d2f2003-02-18 23:29:47 +000072 GetCurrentProcessId(), GetCurrentThreadId(),
Alexandre Julliard4305e6d2008-12-11 21:13:20 +010073 msvcrt_tls_index);
Jon Griffiths1db20bf2001-01-10 23:59:25 +000074
75 switch (fdwReason)
76 {
77 case DLL_PROCESS_ATTACH:
Francois Gouget203a8f82001-04-10 21:16:07 +000078 if (!msvcrt_init_tls())
Jon Griffiths1db20bf2001-01-10 23:59:25 +000079 return FALSE;
Peter Hunnisettd1a79ea2002-02-21 20:22:00 +000080 msvcrt_init_mt_locks();
Francois Gouget203a8f82001-04-10 21:16:07 +000081 msvcrt_init_io();
82 msvcrt_init_console();
83 msvcrt_init_args();
Juan Lange5b4a692005-05-13 17:44:28 +000084 msvcrt_init_signals();
Jon Griffiths1db20bf2001-01-10 23:59:25 +000085 MSVCRT_setlocale(0, "C");
Mikolaj Zalewskiea824f82007-08-19 20:19:37 -070086 _setmbcp(_MB_CP_LOCALE);
Jon Griffiths1db20bf2001-01-10 23:59:25 +000087 TRACE("finished process init\n");
Alexandre Julliard44b42352002-07-19 03:24:50 +000088 break;
Jon Griffiths1db20bf2001-01-10 23:59:25 +000089 case DLL_THREAD_ATTACH:
Jon Griffiths1db20bf2001-01-10 23:59:25 +000090 break;
91 case DLL_PROCESS_DETACH:
Peter Hunnisettd1a79ea2002-02-21 20:22:00 +000092 msvcrt_free_mt_locks();
Alexandre Julliardaf0d2062002-07-05 21:23:07 +000093 msvcrt_free_io();
Francois Gouget203a8f82001-04-10 21:16:07 +000094 msvcrt_free_console();
95 msvcrt_free_args();
Juan Lange5b4a692005-05-13 17:44:28 +000096 msvcrt_free_signals();
Francois Gouget203a8f82001-04-10 21:16:07 +000097 if (!msvcrt_free_tls())
Jon Griffiths1db20bf2001-01-10 23:59:25 +000098 return FALSE;
99 TRACE("finished process free\n");
100 break;
101 case DLL_THREAD_DETACH:
102 /* Free TLS */
Dimitrie O. Paun03774622004-06-25 01:19:15 +0000103 tls = TlsGetValue(msvcrt_tls_index);
Maxime Bellengédef75632005-12-26 12:57:06 +0100104 if (tls)
105 {
106 HeapFree(GetProcessHeap(),0,tls->efcvt_buffer);
Alexandre Julliardf9e5b0f2006-01-14 17:22:03 +0100107 HeapFree(GetProcessHeap(),0,tls->asctime_buffer);
Maxime Bellengédef75632005-12-26 12:57:06 +0100108 HeapFree(GetProcessHeap(),0,tls->wasctime_buffer);
Alexandre Julliard2dacd3c2006-01-23 19:58:10 +0100109 HeapFree(GetProcessHeap(),0,tls->strerror_buffer);
Maxime Bellengédef75632005-12-26 12:57:06 +0100110 }
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +0000111 HeapFree(GetProcessHeap(), 0, tls);
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000112 TRACE("finished thread free\n");
113 break;
114 }
115 return TRUE;
116}
117
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000118/*********************************************************************
Patrik Stridvall044855c2001-07-11 18:56:41 +0000119 * $I10_OUTPUT (MSVCRT.@)
120 * Function not really understood but needed to make the DLL work
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000121 */
Alexandre Julliard24beabf2006-06-13 11:21:19 +0200122void CDECL MSVCRT_I10_OUTPUT(void)
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000123{
124 /* FIXME: This is probably data, not a function */
Rein Klazes03d9cf22004-12-10 15:28:25 +0000125 /* no it is a function. I10 is an Int of 10 bytes */
Austin English05faae72008-04-08 16:22:43 -0500126 /* also known as 80 bit floating point (long double */
Rein Klazes03d9cf22004-12-10 15:28:25 +0000127 /* for some compilers, not MSVC) */
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000128}