oleaut32: Remove some useless null checks.
[wine/multimedia.git] / dlls / msvcrt / main.c
blob323f8b770cb963f9490eb1c3b97118cec8cde988
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 "msvcrt.h"
21 #include "winternl.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
27 /* Index to TLS */
28 DWORD msvcrt_tls_index;
30 static const char* msvcrt_get_reason(DWORD reason)
32 switch (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";
39 return "UNKNOWN";
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");
49 return FALSE;
51 return TRUE;
54 static inline BOOL msvcrt_free_tls(void)
56 if (!TlsFree(msvcrt_tls_index))
58 ERR("TlsFree() failed!\n");
59 return FALSE;
61 return TRUE;
64 static inline void msvcrt_free_tls_mem(void)
66 thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
67 if (tls)
69 HeapFree(GetProcessHeap(),0,tls->efcvt_buffer);
70 HeapFree(GetProcessHeap(),0,tls->asctime_buffer);
71 HeapFree(GetProcessHeap(),0,tls->wasctime_buffer);
72 HeapFree(GetProcessHeap(),0,tls->strerror_buffer);
73 MSVCRT__free_locale(tls->locale);
75 HeapFree(GetProcessHeap(), 0, tls);
78 /*********************************************************************
79 * Init
81 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
83 TRACE("(%p, %s, %p) pid(%x), tid(%x), tls(%u)\n",
84 hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
85 GetCurrentProcessId(), GetCurrentThreadId(),
86 msvcrt_tls_index);
88 switch (fdwReason)
90 case DLL_PROCESS_ATTACH:
91 if (!msvcrt_init_tls())
92 return FALSE;
93 msvcrt_init_mt_locks();
94 if(!MSVCRT_setlocale(0, "C")) {
95 msvcrt_free_mt_locks();
96 msvcrt_free_tls_mem();
97 return FALSE;
99 msvcrt_init_io();
100 msvcrt_init_console();
101 msvcrt_init_args();
102 msvcrt_init_signals();
103 _setmbcp(_MB_CP_LOCALE);
104 /* don't allow unloading msvcrt, we can't setup file handles twice */
105 LdrAddRefDll( 0, hinstDLL );
106 TRACE("finished process init\n");
107 break;
108 case DLL_THREAD_ATTACH:
109 break;
110 case DLL_PROCESS_DETACH:
111 msvcrt_free_mt_locks();
112 msvcrt_free_io();
113 msvcrt_free_console();
114 msvcrt_free_args();
115 msvcrt_free_signals();
116 msvcrt_free_tls_mem();
117 if (!msvcrt_free_tls())
118 return FALSE;
119 MSVCRT__free_locale(MSVCRT_locale);
120 TRACE("finished process free\n");
121 break;
122 case DLL_THREAD_DETACH:
123 msvcrt_free_tls_mem();
124 TRACE("finished thread free\n");
125 break;
127 return TRUE;