2 * msvcrt.dll initialisation functions
4 * Copyright 2000 Jon Griffiths
8 #include "msvcrt/locale.h"
9 #include "msvcrt/stdio.h"
12 DEFAULT_DEBUG_CHANNEL(msvcrt);
15 DWORD MSVCRT_tls_index;
18 CRITICAL_SECTION MSVCRT_heap_cs;
19 CRITICAL_SECTION MSVCRT_file_cs;
20 CRITICAL_SECTION MSVCRT_exit_cs;
21 CRITICAL_SECTION MSVCRT_console_cs;
22 CRITICAL_SECTION MSVCRT_locale_cs;
24 static inline BOOL msvcrt_init_tls(void);
25 static inline BOOL msvcrt_free_tls(void);
26 static inline void msvcrt_init_critical_sections(void);
27 static inline void msvcrt_free_critical_sections(void);
28 const char* msvcrt_get_reason(DWORD reason) WINE_UNUSED;
30 void msvcrt_init_io(void);
31 void msvcrt_init_console(void);
32 void msvcrt_free_console(void);
33 void msvcrt_init_args(void);
34 void msvcrt_free_args(void);
35 void msvcrt_init_vtables(void);
37 typedef void* (*MSVCRT_malloc_func)(unsigned int);
38 typedef void (*MSVCRT_free_func)(void*);
40 /*********************************************************************
43 BOOL WINAPI MSVCRT_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
45 MSVCRT_thread_data *tls;
47 TRACE("(0x%08x, %s, %p) pid(%ld), tid(%ld), tls(%ld)\n",
48 hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
49 (long)GetCurrentProcessId(), (long)GetCurrentThreadId(),
50 (long)MSVCRT_tls_index);
54 case DLL_PROCESS_ATTACH:
55 if (!msvcrt_init_tls())
57 msvcrt_init_vtables();
58 msvcrt_init_critical_sections();
60 msvcrt_init_console();
62 MSVCRT_setlocale(0, "C");
63 TRACE("finished process init\n");
64 /* FALL THROUGH for Initial TLS allocation!! */
65 case DLL_THREAD_ATTACH:
66 TRACE("starting thread init\n");
68 tls = (MSVCRT_thread_data*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
69 sizeof(MSVCRT_thread_data));
70 if (!tls || !TlsSetValue(MSVCRT_tls_index, tls))
72 ERR("TLS init failed! error = %ld\n", GetLastError());
75 TRACE("finished thread init\n");
77 case DLL_PROCESS_DETACH:
78 msvcrt_free_critical_sections();
80 msvcrt_free_console();
82 if (!msvcrt_free_tls())
84 TRACE("finished process free\n");
86 case DLL_THREAD_DETACH:
88 tls = TlsGetValue(MSVCRT_tls_index);
92 ERR("TLS free failed! error = %ld\n", GetLastError());
95 HeapFree(GetProcessHeap(), 0, tls);
96 TRACE("finished thread free\n");
102 static inline BOOL msvcrt_init_tls(void)
104 MSVCRT_tls_index = TlsAlloc();
106 if (MSVCRT_tls_index == TLS_OUT_OF_INDEXES)
108 ERR("TlsAlloc() failed!\n");
114 static inline BOOL msvcrt_free_tls(void)
116 if (!TlsFree(MSVCRT_tls_index))
118 ERR("TlsFree() failed!\n");
124 static inline void msvcrt_init_critical_sections(void)
126 InitializeCriticalSectionAndSpinCount(&MSVCRT_heap_cs, 4000);
127 InitializeCriticalSection(&MSVCRT_file_cs);
128 InitializeCriticalSection(&MSVCRT_exit_cs);
129 InitializeCriticalSection(&MSVCRT_console_cs);
130 InitializeCriticalSection(&MSVCRT_locale_cs);
133 static inline void msvcrt_free_critical_sections(void)
135 DeleteCriticalSection(&MSVCRT_locale_cs);
136 DeleteCriticalSection(&MSVCRT_console_cs);
137 DeleteCriticalSection(&MSVCRT_exit_cs);
138 DeleteCriticalSection(&MSVCRT_file_cs);
139 DeleteCriticalSection(&MSVCRT_heap_cs);
142 const char* msvcrt_get_reason(DWORD reason)
146 case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
147 case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
148 case DLL_THREAD_ATTACH: return "DLL_THREAD_ATTACH";
149 case DLL_THREAD_DETACH: return "DLL_THREAD_DETACH";
155 /*********************************************************************
156 * $I10_OUTPUT (MSVCRT.@)
157 * Function not really understood but needed to make the DLL work
159 void MSVCRT_I10_OUTPUT(void)
161 /* FIXME: This is probably data, not a function */
165 /*********************************************************************
166 * __unDName (MSVCRT.@)
167 * Function not really understood but needed to make the DLL work
169 char* MSVCRT___unDName(int unknown, const char* mangled,
170 MSVCRT_malloc_func memget,
171 MSVCRT_free_func memfree,
176 FIXME("(%d,%s,%p,%p,%x) stub!\n", unknown, mangled, memget, memfree, flags);
178 /* Experimentation reveals the following flag meanings when set:
179 * 0x0001 - Dont show __ in calling convention
180 * 0x0002 - Dont show calling convention at all
181 * 0x0004 - Dont show function/method return value
182 * 0x0010 - Same as 0x1
183 * 0x0080 - Dont show access specifier (public/protected/private)
184 * 0x0200 - Dont show static specifier
185 * 0x1000 - Only report the variable/class name
187 /* Duplicate the mangled name; for comparisons it doesn't matter anyway */
188 ret = memget(strlen(mangled) + 1);
189 strcpy(ret, mangled);
194 /*********************************************************************
195 * __unDNameEx (MSVCRT.@)
196 * Function not really understood but needed to make the DLL work
198 char* MSVCRT___unDNameEx(void)