I have a dll which is some kind of plugin and I intend to deploy it as a package, the dll and all it's dependencies (also dlls) in one package. The issue that I have is that the dll is checking for it's dependencies in the exe folder instead of its own folder, its dependencies are next to it.
Is there a way to tell it where the dependencies are?
CodePudding user response:
From the documentation:
Note that the standard search strategy and the alternate search strategy specified by
LoadLibraryExwithLOAD_WITH_ALTERED_SEARCH_PATHdiffer in just one way: The standard search begins in the calling application's directory, and the alternate search begins in the directory of the executable module thatLoadLibraryExis loading.
This is exactly the behavior you described. You cannot change this behavior from the build system (in re: the [cmake] tag), but will need to modify the application's plugin-loading logic.
CodePudding user response:
Use LoadLibraryEx in with the LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR flag combined with LOAD_LIBRARY_SEARCH_DEFAULT_DIRS and an absolute path to the dll, see the documentation:
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
If this value is used, the directory that contains the DLL is temporarily added to the beginning of the list of directories that are searched for the DLL's dependencies. Directories in the standard search path are not searched.
The
lpFileNameparameter must specify a fully qualified path. [...].For example, if Lib2.dll is a dependency of C:\Dir1\Lib1.dll, loading Lib1.dll with this value causes the system to search for Lib2.dll only in C:\Dir1. To search for Lib2.dll in C:\Dir1 and all of the directories in the DLL search path, combine this value with LOAD_LIBRARY_SEARCH_DEFAULT_DIRS.
[...]
auto handle = LoadLibraryExA(absoluteDllPath,
nullptr,
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
);
