The following code is part of the plugin system that I am developing. It basically loads the DLL and, in the event of a failure, displays 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 mistake is the lack of dependency on the loaded DLL (also applies to dependency dependencies, etc.): the specified module was not found. Unfortunately, this error is not very useful for determining dependency.
, Dependency Walker, ( , ), , , LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, . AddDllDirectory RemoveDllDirectory .
Dependency Walker : , ( , ).
: , ? , .
: Visual Studio 2017, (DLL) ( .lib, ).