aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/dlfcn.c43
1 files changed, 36 insertions, 7 deletions
diff --git a/src/dlfcn.c b/src/dlfcn.c
index 7bca267..ad8dc3f 100644
--- a/src/dlfcn.c
+++ b/src/dlfcn.c
@@ -216,6 +216,35 @@ static void save_err_ptr_str( const void *ptr, DWORD dwMessageId )
216 save_err_str( ptr_buf, dwMessageId ); 216 save_err_str( ptr_buf, dwMessageId );
217} 217}
218 218
219static UINT MySetErrorMode( UINT uMode )
220{
221 static BOOL (WINAPI *SetThreadErrorModePtr)(DWORD, DWORD *) = NULL;
222 static BOOL failed = FALSE;
223 HMODULE kernel32;
224 DWORD oldMode;
225
226 if( !failed && SetThreadErrorModePtr == NULL )
227 {
228 kernel32 = GetModuleHandleA( "Kernel32.dll" );
229 if( kernel32 != NULL )
230 SetThreadErrorModePtr = (BOOL (WINAPI *)(DWORD, DWORD *)) (LPVOID) GetProcAddress( kernel32, "SetThreadErrorMode" );
231 if( SetThreadErrorModePtr == NULL )
232 failed = TRUE;
233 }
234
235 if( !failed )
236 {
237 if( !SetThreadErrorModePtr( uMode, &oldMode ) )
238 return 0;
239 else
240 return oldMode;
241 }
242 else
243 {
244 return SetErrorMode( uMode );
245 }
246}
247
219static HMODULE MyGetModuleHandleFromAddress( const void *addr ) 248static HMODULE MyGetModuleHandleFromAddress( const void *addr )
220{ 249{
221 static BOOL (WINAPI *GetModuleHandleExAPtr)(DWORD, LPCSTR, HMODULE *) = NULL; 250 static BOOL (WINAPI *GetModuleHandleExAPtr)(DWORD, LPCSTR, HMODULE *) = NULL;
@@ -229,7 +258,7 @@ static HMODULE MyGetModuleHandleFromAddress( const void *addr )
229 { 258 {
230 kernel32 = GetModuleHandleA( "Kernel32.dll" ); 259 kernel32 = GetModuleHandleA( "Kernel32.dll" );
231 if( kernel32 != NULL ) 260 if( kernel32 != NULL )
232 GetModuleHandleExAPtr = (BOOL (WINAPI *)(DWORD, LPCSTR, HMODULE *)) GetProcAddress( kernel32, "GetModuleHandleExA" ); 261 GetModuleHandleExAPtr = (BOOL (WINAPI *)(DWORD, LPCSTR, HMODULE *)) (LPVOID) GetProcAddress( kernel32, "GetModuleHandleExA" );
233 if( GetModuleHandleExAPtr == NULL ) 262 if( GetModuleHandleExAPtr == NULL )
234 failed = TRUE; 263 failed = TRUE;
235 } 264 }
@@ -270,21 +299,21 @@ static BOOL MyEnumProcessModules( HANDLE hProcess, HMODULE *lphModule, DWORD cb,
270 /* Windows 7 and newer versions have K32EnumProcessModules in Kernel32.dll which is always pre-loaded */ 299 /* Windows 7 and newer versions have K32EnumProcessModules in Kernel32.dll which is always pre-loaded */
271 psapi = GetModuleHandleA( "Kernel32.dll" ); 300 psapi = GetModuleHandleA( "Kernel32.dll" );
272 if( psapi != NULL ) 301 if( psapi != NULL )
273 EnumProcessModulesPtr = (BOOL (WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) GetProcAddress( psapi, "K32EnumProcessModules" ); 302 EnumProcessModulesPtr = (BOOL (WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) (LPVOID) GetProcAddress( psapi, "K32EnumProcessModules" );
274 303
275 /* Windows Vista and older version have EnumProcessModules in Psapi.dll which needs to be loaded */ 304 /* Windows Vista and older version have EnumProcessModules in Psapi.dll which needs to be loaded */
276 if( EnumProcessModulesPtr == NULL ) 305 if( EnumProcessModulesPtr == NULL )
277 { 306 {
278 /* Do not let Windows display the critical-error-handler message box */ 307 /* Do not let Windows display the critical-error-handler message box */
279 uMode = SetErrorMode( SEM_FAILCRITICALERRORS ); 308 uMode = MySetErrorMode( SEM_FAILCRITICALERRORS );
280 psapi = LoadLibraryA( "Psapi.dll" ); 309 psapi = LoadLibraryA( "Psapi.dll" );
281 if( psapi != NULL ) 310 if( psapi != NULL )
282 { 311 {
283 EnumProcessModulesPtr = (BOOL (WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) GetProcAddress( psapi, "EnumProcessModules" ); 312 EnumProcessModulesPtr = (BOOL (WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) (LPVOID) GetProcAddress( psapi, "EnumProcessModules" );
284 if( EnumProcessModulesPtr == NULL ) 313 if( EnumProcessModulesPtr == NULL )
285 FreeLibrary( psapi ); 314 FreeLibrary( psapi );
286 } 315 }
287 SetErrorMode( uMode ); 316 MySetErrorMode( uMode );
288 } 317 }
289 318
290 if( EnumProcessModulesPtr == NULL ) 319 if( EnumProcessModulesPtr == NULL )
@@ -306,7 +335,7 @@ void *dlopen( const char *file, int mode )
306 error_occurred = FALSE; 335 error_occurred = FALSE;
307 336
308 /* Do not let Windows display the critical-error-handler message box */ 337 /* Do not let Windows display the critical-error-handler message box */
309 uMode = SetErrorMode( SEM_FAILCRITICALERRORS ); 338 uMode = MySetErrorMode( SEM_FAILCRITICALERRORS );
310 339
311 if( file == NULL ) 340 if( file == NULL )
312 { 341 {
@@ -399,7 +428,7 @@ void *dlopen( const char *file, int mode )
399 } 428 }
400 429
401 /* Return to previous state of the error-mode bit flags. */ 430 /* Return to previous state of the error-mode bit flags. */
402 SetErrorMode( uMode ); 431 MySetErrorMode( uMode );
403 432
404 return (void *) hModule; 433 return (void *) hModule;
405} 434}