2001-01-11 00:59:25 +01:00
|
|
|
/*
|
|
|
|
* msvcrt.dll initialisation functions
|
|
|
|
*
|
|
|
|
* Copyright 2000 Jon Griffiths
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2001-01-11 00:59:25 +01:00
|
|
|
*/
|
2020-11-26 19:19:24 +01:00
|
|
|
#include <locale.h>
|
2001-01-11 00:59:25 +01:00
|
|
|
#include "msvcrt.h"
|
2010-05-28 20:33:27 +02:00
|
|
|
#include "winternl.h"
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2002-01-22 01:57:16 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
/* Index to TLS */
|
2004-06-25 03:19:15 +02:00
|
|
|
DWORD msvcrt_tls_index;
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2006-12-25 09:41:14 +01:00
|
|
|
static const char* msvcrt_get_reason(DWORD reason)
|
|
|
|
{
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
|
|
|
|
case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
|
|
|
|
case DLL_THREAD_ATTACH: return "DLL_THREAD_ATTACH";
|
|
|
|
case DLL_THREAD_DETACH: return "DLL_THREAD_DETACH";
|
|
|
|
}
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
2001-01-11 00:59:25 +01:00
|
|
|
|
2007-06-19 22:49:04 +02:00
|
|
|
static inline BOOL msvcrt_init_tls(void)
|
|
|
|
{
|
|
|
|
msvcrt_tls_index = TlsAlloc();
|
|
|
|
|
|
|
|
if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
|
|
|
|
{
|
|
|
|
ERR("TlsAlloc() failed!\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline BOOL msvcrt_free_tls(void)
|
|
|
|
{
|
|
|
|
if (!TlsFree(msvcrt_tls_index))
|
|
|
|
{
|
|
|
|
ERR("TlsFree() failed!\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-12-14 15:13:57 +01:00
|
|
|
static inline void msvcrt_free_tls_mem(void)
|
|
|
|
{
|
|
|
|
thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
|
2011-05-24 17:22:42 +02:00
|
|
|
|
2009-12-14 15:13:57 +01:00
|
|
|
if (tls)
|
|
|
|
{
|
2020-12-01 13:51:32 +01:00
|
|
|
free(tls->efcvt_buffer);
|
|
|
|
free(tls->asctime_buffer);
|
|
|
|
free(tls->wasctime_buffer);
|
|
|
|
free(tls->strerror_buffer);
|
|
|
|
free(tls->wcserror_buffer);
|
|
|
|
free(tls->time_buffer);
|
|
|
|
free(tls->tmpnam_buffer);
|
|
|
|
free(tls->wtmpnam_buffer);
|
2020-11-25 20:24:26 +01:00
|
|
|
if(tls->locale_flags & LOCALE_FREE) {
|
2011-05-26 11:40:04 +02:00
|
|
|
free_locinfo(tls->locinfo);
|
|
|
|
free_mbcinfo(tls->mbcinfo);
|
|
|
|
}
|
2009-12-14 15:13:57 +01:00
|
|
|
}
|
|
|
|
HeapFree(GetProcessHeap(), 0, tls);
|
|
|
|
}
|
|
|
|
|
2001-01-11 00:59:25 +01:00
|
|
|
/*********************************************************************
|
|
|
|
* Init
|
|
|
|
*/
|
2002-11-05 00:53:41 +01:00
|
|
|
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
2001-01-11 00:59:25 +01:00
|
|
|
{
|
2008-12-11 21:13:20 +01:00
|
|
|
TRACE("(%p, %s, %p) pid(%x), tid(%x), tls(%u)\n",
|
2001-04-10 23:16:07 +02:00
|
|
|
hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
|
2003-02-19 00:29:47 +01:00
|
|
|
GetCurrentProcessId(), GetCurrentThreadId(),
|
2008-12-11 21:13:20 +01:00
|
|
|
msvcrt_tls_index);
|
2001-01-11 00:59:25 +01:00
|
|
|
|
|
|
|
switch (fdwReason)
|
|
|
|
{
|
|
|
|
case DLL_PROCESS_ATTACH:
|
2012-09-07 11:01:17 +02:00
|
|
|
msvcrt_init_exception(hinstDLL);
|
2014-04-22 14:38:47 +02:00
|
|
|
if(!msvcrt_init_heap())
|
|
|
|
return FALSE;
|
|
|
|
if(!msvcrt_init_tls()) {
|
|
|
|
msvcrt_destroy_heap();
|
2010-04-15 23:13:59 +02:00
|
|
|
return FALSE;
|
2014-04-22 14:38:47 +02:00
|
|
|
}
|
2002-02-21 21:22:00 +01:00
|
|
|
msvcrt_init_mt_locks();
|
2011-05-26 11:40:04 +02:00
|
|
|
if(!msvcrt_init_locale()) {
|
2014-06-24 11:57:47 +02:00
|
|
|
msvcrt_free_locks();
|
2010-04-15 23:13:59 +02:00
|
|
|
msvcrt_free_tls_mem();
|
2014-04-22 14:38:47 +02:00
|
|
|
msvcrt_destroy_heap();
|
2010-04-15 14:25:13 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
2020-11-18 16:14:50 +01:00
|
|
|
msvcrt_init_math(hinstDLL);
|
2001-04-10 23:16:07 +02:00
|
|
|
msvcrt_init_io();
|
|
|
|
msvcrt_init_args();
|
2005-05-13 19:44:28 +02:00
|
|
|
msvcrt_init_signals();
|
2017-03-23 11:41:05 +01:00
|
|
|
#if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
|
|
|
|
msvcrt_init_scheduler(hinstDLL);
|
|
|
|
#endif
|
2014-04-16 16:15:41 +02:00
|
|
|
#if _MSVCR_VER == 0
|
2010-05-28 20:33:27 +02:00
|
|
|
/* don't allow unloading msvcrt, we can't setup file handles twice */
|
2013-10-15 08:22:23 +02:00
|
|
|
LdrAddRefDll( LDR_ADDREF_DLL_PIN, hinstDLL );
|
2014-04-16 16:15:41 +02:00
|
|
|
#elif _MSVCR_VER >= 80
|
2020-12-03 19:10:54 +01:00
|
|
|
_set_printf_count_output(0);
|
2014-04-16 16:15:41 +02:00
|
|
|
#endif
|
2019-10-16 13:20:58 +02:00
|
|
|
msvcrt_init_clock();
|
2001-01-11 00:59:25 +01:00
|
|
|
TRACE("finished process init\n");
|
2002-07-19 05:24:50 +02:00
|
|
|
break;
|
2001-01-11 00:59:25 +01:00
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
break;
|
|
|
|
case DLL_PROCESS_DETACH:
|
2002-07-05 23:23:07 +02:00
|
|
|
msvcrt_free_io();
|
2013-05-20 12:28:38 +02:00
|
|
|
if (lpvReserved) break;
|
|
|
|
msvcrt_free_popen_data();
|
2014-06-24 11:57:47 +02:00
|
|
|
msvcrt_free_locks();
|
2001-04-10 23:16:07 +02:00
|
|
|
msvcrt_free_console();
|
|
|
|
msvcrt_free_args();
|
2005-05-13 19:44:28 +02:00
|
|
|
msvcrt_free_signals();
|
2009-12-14 15:13:57 +01:00
|
|
|
msvcrt_free_tls_mem();
|
2001-04-10 23:16:07 +02:00
|
|
|
if (!msvcrt_free_tls())
|
2001-01-11 00:59:25 +01:00
|
|
|
return FALSE;
|
2020-11-26 19:19:24 +01:00
|
|
|
_free_locale(MSVCRT_locale);
|
2017-03-23 11:41:05 +01:00
|
|
|
#if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
|
|
|
|
msvcrt_free_scheduler_thread();
|
|
|
|
msvcrt_free_scheduler();
|
|
|
|
#endif
|
2014-04-22 14:38:47 +02:00
|
|
|
msvcrt_destroy_heap();
|
2001-01-11 00:59:25 +01:00
|
|
|
TRACE("finished process free\n");
|
|
|
|
break;
|
|
|
|
case DLL_THREAD_DETACH:
|
2009-12-14 15:13:57 +01:00
|
|
|
msvcrt_free_tls_mem();
|
2017-03-23 11:41:05 +01:00
|
|
|
#if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
|
|
|
|
msvcrt_free_scheduler_thread();
|
|
|
|
#endif
|
2001-01-11 00:59:25 +01:00
|
|
|
TRACE("finished thread free\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|