问题
The following code is part of a plugin system I'm developing. Basically it loads a DLL and, if fails, shows an error message.
HMODULE loadPlugin(LPTSTR path) {
const auto module = LoadLibraryEx(path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
if (module != nullptr) return module;
LPTSTR errorText = nullptr;
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), 0,
(LPTSTR)&errorText, 0, NULL);
if (errorText != nullptr) {
MessageBox(0, errorText, L"Error", MB_OK | MB_ICONWARNING);
LocalFree(errorText);
errorText = NULL;
}
return nullptr;
}
The most common error is a missing dependency of the DLL being loaded (also applies for dependencies of dependencies, and so on): The specified module could not be found. Unfortunately, this error is not very helpful in order to know which dependency is.
I know I can use Dependency Walker to find it manually (actually it's how I'm doing it right now), but the process is quite complex since, as you noted by the LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
flag, dependencies can be located in different directories. The search path is specified before loading the plugin using AddDllDirectory
, and restored with RemoveDllDirectory
after it is loaded.
For reason above, using Dependency Walker is quite tedious: for each plugin that fails I have to add the directories used to load that plugin (there are many plugins and each one may have a different search path set).
Question: is there any way to know, at runtime, the name of the module that wasn't found? I'm the developer of the plugins too, so I can modify them if necessary.
Additional notes: Using Visual Studio 2017, and almost all the dependencies (DLLs) have exported symbols (.lib file associated and linked with the plugin).
来源:https://stackoverflow.com/questions/44430822/get-name-of-missing-dependencies-at-runtime-the-specified-module-could-not-be