Reputation: 1594
I have a situation where I need to load a DLL (libcocotb.dll) at runtime (using LoadLibrary
) from another DLL (libembed.dll) that exists in the same directory. These DLLs are not in the same directory as the application.
> tree
.
├── ...
├── libcocotb.dll
└── libembed.dll
Specifying just the library name LoadLibrary("libcocotb.dll")
, did not find the library. LoadLibrary(".\\libcocotb.dll")
, did not find the library. Adding the directory to the PATH
seems like the wrong answer, and I would need to determine the location of the directory at runtime since the binaries need to be relocatable (they are going in a Python wheel at the end of the day).
On Linux, I can just add $ORIGIN
to the RPATH of the loading DLL. Is there nothing similar on Windows? I am not very familiar with Windows loader and library system.
Upvotes: 1
Views: 694
Reputation: 17638
One way to do it in Windows is:
save the HMODULE
of libembed.dll
when it gets loaded, which is passed as the first argument to its DllMain
entry point;
later, when preparing to load the other DLL, use GetModuleFileName
with the saved HMODULE
to get the full path to libembed.dll
;
split the libembed.dll
filename off the full path, using for example the PathRemoveFileSpec
API or the _splitpath
CRT function;
combine the resulting directory path with the libcocotb.dll
filename to build the full path to the other DLL, using for example PathCombine
or _makepath
;
use the full path to libcocotb.dll
to LoadLibrary
the DLL.
Upvotes: 2