| 1 | /* | 
|---|
| 2 | * msvcrt.dll initialisation functions | 
|---|
| 3 | * | 
|---|
| 4 | * Copyright 2000 Jon Griffiths | 
|---|
| 5 | * | 
|---|
| 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 | 
|---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
|---|
| 19 | */ | 
|---|
| 20 | #include "msvcrt.h" | 
|---|
| 21 | #define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF) | 
|---|
| 22 | #include "msvcrt/locale.h" | 
|---|
| 23 | #include "msvcrt/stdio.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "wine/debug.h" | 
|---|
| 26 |  | 
|---|
| 27 | WINE_DEFAULT_DEBUG_CHANNEL(msvcrt); | 
|---|
| 28 |  | 
|---|
| 29 | /* Index to TLS */ | 
|---|
| 30 | DWORD MSVCRT_tls_index; | 
|---|
| 31 |  | 
|---|
| 32 | static inline BOOL msvcrt_init_tls(void); | 
|---|
| 33 | static inline BOOL msvcrt_free_tls(void); | 
|---|
| 34 | const char* msvcrt_get_reason(DWORD reason) WINE_UNUSED; | 
|---|
| 35 |  | 
|---|
| 36 | typedef void* (*MSVCRT_malloc_func)(unsigned int); | 
|---|
| 37 | typedef void (*MSVCRT_free_func)(void*); | 
|---|
| 38 |  | 
|---|
| 39 | int _CRT_init (void); | 
|---|
| 40 | void _CRT_term (void); | 
|---|
| 41 | void __ctordtorInit (void); | 
|---|
| 42 | void __ctordtorTerm (void); | 
|---|
| 43 |  | 
|---|
| 44 | static HMODULE dllHandle = 0; | 
|---|
| 45 |  | 
|---|
| 46 | BOOL WINAPI MSVCRT_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | unsigned long _DLL_InitTerm (unsigned long mod_handle, | 
|---|
| 50 | unsigned long flag) | 
|---|
| 51 | { | 
|---|
| 52 | switch (flag) | 
|---|
| 53 | { | 
|---|
| 54 | case 0: | 
|---|
| 55 | if (_CRT_init () != 0) | 
|---|
| 56 | return 0; | 
|---|
| 57 | __ctordtorInit (); | 
|---|
| 58 |  | 
|---|
| 59 | dllHandle = RegisterLxDll(mod_handle, MSVCRT_Init, 0,0,0,0); | 
|---|
| 60 |  | 
|---|
| 61 | return 1; | 
|---|
| 62 | case 1: | 
|---|
| 63 | __ctordtorTerm (); | 
|---|
| 64 | _CRT_term (); | 
|---|
| 65 |  | 
|---|
| 66 | if(dllHandle) { | 
|---|
| 67 | UnregisterLxDll(dllHandle); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | return 1; | 
|---|
| 71 | default: | 
|---|
| 72 | return 0; | 
|---|
| 73 | } | 
|---|
| 74 | return 1; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | /********************************************************************* | 
|---|
| 78 | *                  Init | 
|---|
| 79 | */ | 
|---|
| 80 | BOOL WINAPI MSVCRT_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) | 
|---|
| 81 | { | 
|---|
| 82 | MSVCRT_thread_data *tls; | 
|---|
| 83 |  | 
|---|
| 84 | TRACE("(0x%08x, %s, %p) pid(%ld), tid(%ld), tls(%ld)\n", | 
|---|
| 85 | hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved, | 
|---|
| 86 | (long)GetCurrentProcessId(), (long)GetCurrentThreadId(), | 
|---|
| 87 | (long)MSVCRT_tls_index); | 
|---|
| 88 |  | 
|---|
| 89 | switch (fdwReason) | 
|---|
| 90 | { | 
|---|
| 91 | case DLL_PROCESS_ATTACH: | 
|---|
| 92 | if (!msvcrt_init_tls()) | 
|---|
| 93 | return FALSE; | 
|---|
| 94 | msvcrt_init_mt_locks(); | 
|---|
| 95 | msvcrt_init_vtables(); | 
|---|
| 96 | msvcrt_init_io(); | 
|---|
| 97 | msvcrt_init_console(); | 
|---|
| 98 | msvcrt_init_args(); | 
|---|
| 99 | MSVCRT_setlocale(0, "C"); | 
|---|
| 100 | TRACE("finished process init\n"); | 
|---|
| 101 | break; | 
|---|
| 102 | case DLL_THREAD_ATTACH: | 
|---|
| 103 | break; | 
|---|
| 104 | case DLL_PROCESS_DETACH: | 
|---|
| 105 | msvcrt_free_mt_locks(); | 
|---|
| 106 | msvcrt_free_io(); | 
|---|
| 107 | msvcrt_free_console(); | 
|---|
| 108 | msvcrt_free_args(); | 
|---|
| 109 | if (!msvcrt_free_tls()) | 
|---|
| 110 | return FALSE; | 
|---|
| 111 | TRACE("finished process free\n"); | 
|---|
| 112 | break; | 
|---|
| 113 | case DLL_THREAD_DETACH: | 
|---|
| 114 | /* Free TLS */ | 
|---|
| 115 | tls = TlsGetValue(MSVCRT_tls_index); | 
|---|
| 116 | if (tls) HeapFree(GetProcessHeap(), 0, tls); | 
|---|
| 117 | TRACE("finished thread free\n"); | 
|---|
| 118 | break; | 
|---|
| 119 | } | 
|---|
| 120 | return TRUE; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | static inline BOOL msvcrt_init_tls(void) | 
|---|
| 124 | { | 
|---|
| 125 | MSVCRT_tls_index = TlsAlloc(); | 
|---|
| 126 |  | 
|---|
| 127 | if (MSVCRT_tls_index == TLS_OUT_OF_INDEXES) | 
|---|
| 128 | { | 
|---|
| 129 | ERR("TlsAlloc() failed!\n"); | 
|---|
| 130 | return FALSE; | 
|---|
| 131 | } | 
|---|
| 132 | return TRUE; | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | static inline BOOL msvcrt_free_tls(void) | 
|---|
| 136 | { | 
|---|
| 137 | if (!TlsFree(MSVCRT_tls_index)) | 
|---|
| 138 | { | 
|---|
| 139 | ERR("TlsFree() failed!\n"); | 
|---|
| 140 | return FALSE; | 
|---|
| 141 | } | 
|---|
| 142 | return TRUE; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | const char* msvcrt_get_reason(DWORD reason) | 
|---|
| 146 | { | 
|---|
| 147 | switch (reason) | 
|---|
| 148 | { | 
|---|
| 149 | case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH"; | 
|---|
| 150 | case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH"; | 
|---|
| 151 | case DLL_THREAD_ATTACH:  return "DLL_THREAD_ATTACH"; | 
|---|
| 152 | case DLL_THREAD_DETACH:  return "DLL_THREAD_DETACH"; | 
|---|
| 153 | } | 
|---|
| 154 | return "UNKNOWN"; | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 | /********************************************************************* | 
|---|
| 159 | *              $I10_OUTPUT (MSVCRT.@) | 
|---|
| 160 | * Function not really understood but needed to make the DLL work | 
|---|
| 161 | */ | 
|---|
| 162 | void MSVCRT_I10_OUTPUT(void) | 
|---|
| 163 | { | 
|---|
| 164 | /* FIXME: This is probably data, not a function */ | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 |  | 
|---|
| 168 | /********************************************************************* | 
|---|
| 169 | *              __unDName (MSVCRT.@) | 
|---|
| 170 | * Function not really understood but needed to make the DLL work | 
|---|
| 171 | */ | 
|---|
| 172 | char* MSVCRT___unDName(int unknown, const char* mangled, | 
|---|
| 173 | MSVCRT_malloc_func memget, | 
|---|
| 174 | MSVCRT_free_func memfree, | 
|---|
| 175 | unsigned int flags) | 
|---|
| 176 | { | 
|---|
| 177 | char* ret; | 
|---|
| 178 |  | 
|---|
| 179 | FIXME("(%d,%s,%p,%p,%x) stub!\n", unknown, mangled, memget, memfree, flags); | 
|---|
| 180 |  | 
|---|
| 181 | /* Experimentation reveals the following flag meanings when set: | 
|---|
| 182 | * 0x0001 - Dont show __ in calling convention | 
|---|
| 183 | * 0x0002 - Dont show calling convention at all | 
|---|
| 184 | * 0x0004 - Dont show function/method return value | 
|---|
| 185 | * 0x0010 - Same as 0x1 | 
|---|
| 186 | * 0x0080 - Dont show access specifier (public/protected/private) | 
|---|
| 187 | * 0x0200 - Dont show static specifier | 
|---|
| 188 | * 0x1000 - Only report the variable/class name | 
|---|
| 189 | */ | 
|---|
| 190 | /* Duplicate the mangled name; for comparisons it doesn't matter anyway */ | 
|---|
| 191 | ret = memget(strlen(mangled) + 1); | 
|---|
| 192 | strcpy(ret, mangled); | 
|---|
| 193 | return ret; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 |  | 
|---|
| 197 | /********************************************************************* | 
|---|
| 198 | *              __unDNameEx (MSVCRT.@) | 
|---|
| 199 | * Function not really understood but needed to make the DLL work | 
|---|
| 200 | */ | 
|---|
| 201 | char* MSVCRT___unDNameEx(void) | 
|---|
| 202 | { | 
|---|
| 203 | return NULL; | 
|---|
| 204 | } | 
|---|