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);
38 /*********************************************************************
41 BOOL WINAPI
MSVCRT_Init(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
43 MSVCRT_thread_data
*tls
;
45 TRACE("(0x%08x, %s, %p) pid(%ld), tid(%ld), tls(%ld)\n",
46 hinstDLL
, msvcrt_get_reason(fdwReason
), lpvReserved
,
47 (long)GetCurrentProcessId(), (long)GetCurrentThreadId(),
48 (long)MSVCRT_tls_index
);
52 case DLL_PROCESS_ATTACH
:
53 if (!msvcrt_init_tls())
55 msvcrt_init_vtables();
56 msvcrt_init_critical_sections();
58 msvcrt_init_console();
60 MSVCRT_setlocale(0, "C");
61 TRACE("finished process init\n");
62 /* FALL THROUGH for Initial TLS allocation!! */
63 case DLL_THREAD_ATTACH
:
64 TRACE("starting thread init\n");
66 tls
= (MSVCRT_thread_data
*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
67 sizeof(MSVCRT_thread_data
));
68 if (!tls
|| !TlsSetValue(MSVCRT_tls_index
, tls
))
70 ERR("TLS init failed! error = %ld\n", GetLastError());
73 TRACE("finished thread init\n");
75 case DLL_PROCESS_DETACH
:
76 msvcrt_free_critical_sections();
78 msvcrt_free_console();
80 if (!msvcrt_free_tls())
82 TRACE("finished process free\n");
84 case DLL_THREAD_DETACH
:
86 tls
= TlsGetValue(MSVCRT_tls_index
);
90 ERR("TLS free failed! error = %ld\n", GetLastError());
93 HeapFree(GetProcessHeap(), 0, tls
);
94 TRACE("finished thread free\n");
100 static inline BOOL
msvcrt_init_tls(void)
102 MSVCRT_tls_index
= TlsAlloc();
104 if (MSVCRT_tls_index
== TLS_OUT_OF_INDEXES
)
106 ERR("TlsAlloc() failed!\n");
112 static inline BOOL
msvcrt_free_tls(void)
114 if (!TlsFree(MSVCRT_tls_index
))
116 ERR("TlsFree() failed!\n");
122 static inline void msvcrt_init_critical_sections(void)
124 InitializeCriticalSectionAndSpinCount(&MSVCRT_heap_cs
, 4000);
125 InitializeCriticalSection(&MSVCRT_file_cs
);
126 InitializeCriticalSection(&MSVCRT_exit_cs
);
127 InitializeCriticalSection(&MSVCRT_console_cs
);
128 InitializeCriticalSection(&MSVCRT_locale_cs
);
131 static inline void msvcrt_free_critical_sections(void)
133 DeleteCriticalSection(&MSVCRT_locale_cs
);
134 DeleteCriticalSection(&MSVCRT_console_cs
);
135 DeleteCriticalSection(&MSVCRT_exit_cs
);
136 DeleteCriticalSection(&MSVCRT_file_cs
);
137 DeleteCriticalSection(&MSVCRT_heap_cs
);
140 const char* msvcrt_get_reason(DWORD reason
)
144 case DLL_PROCESS_ATTACH
: return "DLL_PROCESS_ATTACH";
145 case DLL_PROCESS_DETACH
: return "DLL_PROCESS_DETACH";
146 case DLL_THREAD_ATTACH
: return "DLL_THREAD_ATTACH";
147 case DLL_THREAD_DETACH
: return "DLL_THREAD_DETACH";
153 /*********************************************************************
154 * $I10_OUTPUT (MSVCRT.@)
155 * Function not really understood but needed to make the DLL work
157 void MSVCRT_I10_OUTPUT(void)
159 /* FIXME: This is probably data, not a function */
162 /*********************************************************************
163 * __unDName (MSVCRT.@)
164 * Function not really understood but needed to make the DLL work
166 void MSVCRT___unDName(void)
168 /* Called by all VC compiled progs on startup. No idea what it does */
171 /*********************************************************************
172 * __unDNameEx (MSVCRT.@)
173 * Function not really understood but needed to make the DLL work
175 void MSVCRT___unDNameEx(void)