aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2021-12-21 01:09:09 +0100
committerPali Rohár <pali.rohar@gmail.com>2021-12-21 01:09:09 +0100
commit318030d5419b1064941700a1c3c21d7f9bd30149 (patch)
treec3a8e201d6438ef8f3eba64cbbe39bf9054cf048
parentfe1e475144c895341ffecd8af14b7b1f4d8f4c38 (diff)
downloaddlfcn-win32-318030d5419b1064941700a1c3c21d7f9bd30149.tar.gz
dlfcn-win32-318030d5419b1064941700a1c3c21d7f9bd30149.tar.bz2
dlfcn-win32-318030d5419b1064941700a1c3c21d7f9bd30149.zip
Prefer usage of thread-safe function SetThreadErrorMode() instead of process-global function SetErrorMode()
Use GetProcAddress() wrapper as SetThreadErrorMode() is not available on older Windows versions.
-rw-r--r--src/dlfcn.c37
1 files changed, 33 insertions, 4 deletions
diff --git a/src/dlfcn.c b/src/dlfcn.c
index 9f5bde1..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;
@@ -276,7 +305,7 @@ static BOOL MyEnumProcessModules( HANDLE hProcess, HMODULE *lphModule, DWORD cb,
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 {
@@ -284,7 +313,7 @@ static BOOL MyEnumProcessModules( HANDLE hProcess, HMODULE *lphModule, DWORD cb,
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}