Load Psapi.dll at runtime, this avoids linking caveat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 07addbb..e44343d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,7 +17,6 @@ if (BUILD_SHARED_LIBS)
endif (BUILD_SHARED_LIBS)
add_library(dl ${sources})
-target_link_libraries(dl psapi)
install (TARGETS dl EXPORT dlfcn-win32-targets
RUNTIME DESTINATION bin
diff --git a/Makefile b/Makefile
index efac5af..9f5985e 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,6 @@
#
include config.mak
CFLAGS = -Wall -O3 -fomit-frame-pointer
-LIBS += -lpsapi
ifeq ($(BUILD_SHARED),yes)
TARGETS += libdl.dll
@@ -34,7 +33,7 @@ libdl.a: $(LIB_OBJS)
$(RANLIB) libdl.a
libdl.dll: $(LIB_OBJS)
- $(CC) $(SHFLAGS) -shared -o $@ $^ $(LIBS)
+ $(CC) $(SHFLAGS) -shared -o $@ $^
libdl.lib: libdl.dll
$(LIBCMD) /machine:i386 /def:libdl.def
@@ -54,20 +53,20 @@ static-install: include-install
mkdir -p $(DESTDIR)$(libdir)
cp libdl.a $(DESTDIR)$(libdir)
-lib-install: $(LIBS)
+lib-install:
mkdir -p $(DESTDIR)$(libdir)
cp libdl.lib $(DESTDIR)$(libdir)
install: $(INSTALL)
test.exe: test.o $(TARGETS)
- $(CC) -o $@ $< -L. -ldl $(LIBS)
+ $(CC) -o $@ $< -L. -ldl
testdll.dll: testdll.c
$(CC) -shared -o $@ $^
testdll2.dll: testdll2.c $(TARGETS)
- $(CC) -shared -o $@ $< -L. -ldl $(LIBS)
+ $(CC) -shared -o $@ $< -L. -ldl
testdll3.dll: testdll3.c
$(CC) -shared -o $@ $^
diff --git a/README.md b/README.md
index 6e8ec21..a5fdb02 100644
--- a/README.md
+++ b/README.md
@@ -43,12 +43,6 @@ target_link_libraries(<target> ${CMAKE_DL_LIBS})
...
~~~
-### Linking caveat
-This library uses the Process Status API in Windows (`psapi.lib`). If you are
-linking to the static `dl.lib` or `libdl.a`, then you would need to explicitly
-add `psapi.lib` or `-lpsapi` to your linking command, depending on if MinGW is
-used.
-
Author
------
diff --git a/dlfcn.c b/dlfcn.c
index 8687c51..69670d1 100644
--- a/dlfcn.c
+++ b/dlfcn.c
@@ -24,9 +24,7 @@
#include <stdlib.h>
#include <crtdbg.h>
#endif
-#define PSAPI_VERSION 1
#include <windows.h>
-#include <psapi.h>
#include <stdio.h>
#include <stdlib.h>
@@ -192,6 +190,24 @@ static void save_err_ptr_str( const void *ptr )
save_err_str( ptr_buf );
}
+/* Load Psapi.dll at runtime, this avoids linking caveat */
+static BOOL MyEnumProcessModules( HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded )
+{
+ static BOOL (WINAPI *EnumProcessModulesPtr)(HANDLE, HMODULE *, DWORD, LPDWORD);
+ HMODULE psapi;
+
+ if( !EnumProcessModulesPtr )
+ {
+ psapi = LoadLibraryA( "Psapi.dll" );
+ if( psapi )
+ EnumProcessModulesPtr = (BOOL (WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) GetProcAddress( psapi, "EnumProcessModules" );
+ if( !EnumProcessModulesPtr )
+ return 0;
+ }
+
+ return EnumProcessModulesPtr( hProcess, lphModule, cb, lpcbNeeded );
+}
+
void *dlopen( const char *file, int mode )
{
HMODULE hModule;
@@ -240,7 +256,7 @@ void *dlopen( const char *file, int mode )
hCurrentProc = GetCurrentProcess( );
- if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 )
+ if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsBefore ) == 0 )
dwProcModsBefore = 0;
/* POSIX says the search path is implementation-defined.
@@ -251,7 +267,7 @@ void *dlopen( const char *file, int mode )
hModule = LoadLibraryExA(lpFileName, NULL,
LOAD_WITH_ALTERED_SEARCH_PATH );
- if( EnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 )
+ if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwProcModsAfter ) == 0 )
dwProcModsAfter = 0;
/* 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 )
* if we want to get ALL loaded module including those in linked DLLs,
* we have to use EnumProcessModules( ).
*/
- if( EnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 )
+ if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 )
{
modules = malloc( dwSize );
if( modules )
{
- if( EnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded )
+ if( MyEnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded )
{
for( i = 0; i < dwSize / sizeof( HMODULE ); i++ )
{
diff --git a/visual-studio/12/dl/dl.vcxproj b/visual-studio/12/dl/dl.vcxproj
index 77c7cdb..baa22d1 100644
--- a/visual-studio/12/dl/dl.vcxproj
+++ b/visual-studio/12/dl/dl.vcxproj
@@ -129,7 +129,6 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|Win32'">
@@ -151,7 +150,6 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|x64'">
@@ -177,7 +175,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'">
@@ -207,7 +204,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'">
diff --git a/visual-studio/12/test/test.vcxproj b/visual-studio/12/test/test.vcxproj
index aabc464..7f5feff 100644
--- a/visual-studio/12/test/test.vcxproj
+++ b/visual-studio/12/test/test.vcxproj
@@ -128,7 +128,6 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|Win32'">
@@ -139,7 +138,6 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -150,7 +148,6 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='DebugStatic|x64'">
@@ -161,7 +158,6 @@
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -176,7 +172,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|Win32'">
@@ -191,7 +186,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -206,7 +200,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseStatic|x64'">
@@ -221,7 +214,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
- <AdditionalDependencies>psapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>