diff options
Diffstat (limited to '')
-rw-r--r-- | dlfcn.c | 28 |
1 files changed, 22 insertions, 6 deletions
@@ -24,9 +24,7 @@ | |||
24 | #include <stdlib.h> | 24 | #include <stdlib.h> |
25 | #include <crtdbg.h> | 25 | #include <crtdbg.h> |
26 | #endif | 26 | #endif |
27 | #define PSAPI_VERSION 1 | ||
28 | #include <windows.h> | 27 | #include <windows.h> |
29 | #include <psapi.h> | ||
30 | #include <stdio.h> | 28 | #include <stdio.h> |
31 | #include <stdlib.h> | 29 | #include <stdlib.h> |
32 | 30 | ||
@@ -192,6 +190,24 @@ static void save_err_ptr_str( const void *ptr ) | |||
192 | save_err_str( ptr_buf ); | 190 | save_err_str( ptr_buf ); |
193 | } | 191 | } |
194 | 192 | ||
193 | /* Load Psapi.dll at runtime, this avoids linking caveat */ | ||
194 | static BOOL MyEnumProcessModules( HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded ) | ||
195 | { | ||
196 | static BOOL (WINAPI *EnumProcessModulesPtr)(HANDLE, HMODULE *, DWORD, LPDWORD); | ||
197 | HMODULE psapi; | ||
198 | |||
199 | if( !EnumProcessModulesPtr ) | ||
200 | { | ||
201 | psapi = LoadLibraryA( "Psapi.dll" ); | ||
202 | if( psapi ) | ||
203 | EnumProcessModulesPtr = (BOOL (WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) GetProcAddress( psapi, "EnumProcessModules" ); | ||
204 | if( !EnumProcessModulesPtr ) | ||
205 | return 0; | ||
206 | } | ||
207 | |||
208 | return EnumProcessModulesPtr( hProcess, lphModule, cb, lpcbNeeded ); | ||
209 | } | ||
210 | |||
195 | void *dlopen( const char *file, int mode ) | 211 | void *dlopen( const char *file, int mode ) |
196 | { | 212 | { |
197 | HMODULE hModule; | 213 | HMODULE hModule; |
@@ -240,7 +256,7 @@ void *dlopen( const char *file, int mode ) | |||
240 | 256 | ||
241 | hCurrentProc = GetCurrentProcess( ); | 257 | hCurrentProc = GetCurrentProcess( ); |
242 | 258 | ||
243 | if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 ) | 259 | if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 ) |
244 | dwProcModsBefore = 0; | 260 | dwProcModsBefore = 0; |
245 | 261 | ||
246 | /* POSIX says the search path is implementation-defined. | 262 | /* POSIX says the search path is implementation-defined. |
@@ -251,7 +267,7 @@ void *dlopen( const char *file, int mode ) | |||
251 | hModule = LoadLibraryExA(lpFileName, NULL, | 267 | hModule = LoadLibraryExA(lpFileName, NULL, |
252 | LOAD_WITH_ALTERED_SEARCH_PATH ); | 268 | LOAD_WITH_ALTERED_SEARCH_PATH ); |
253 | 269 | ||
254 | if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 ) | 270 | if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 ) |
255 | dwProcModsAfter = 0; | 271 | dwProcModsAfter = 0; |
256 | 272 | ||
257 | /* If the object was loaded with RTLD_LOCAL, add it to list of local | 273 | /* If the object was loaded with RTLD_LOCAL, add it to list of local |
@@ -366,12 +382,12 @@ void *dlsym( void *handle, const char *name ) | |||
366 | * if we want to get ALL loaded module including those in linked DLLs, | 382 | * if we want to get ALL loaded module including those in linked DLLs, |
367 | * we have to use EnumProcessModules( ). | 383 | * we have to use EnumProcessModules( ). |
368 | */ | 384 | */ |
369 | if( EnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 ) | 385 | if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 ) |
370 | { | 386 | { |
371 | modules = malloc( dwSize ); | 387 | modules = malloc( dwSize ); |
372 | if( modules ) | 388 | if( modules ) |
373 | { | 389 | { |
374 | if( EnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded ) | 390 | if( MyEnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded ) |
375 | { | 391 | { |
376 | for( i = 0; i < dwSize / sizeof( HMODULE ); i++ ) | 392 | for( i = 0; i < dwSize / sizeof( HMODULE ); i++ ) |
377 | { | 393 | { |