From 31ae813286ea386c3c39c9144781326dbc27fb4a Mon Sep 17 00:00:00 2001 From: Waldi Ravens Date: Fri, 14 Jun 2019 22:50:44 +0200 Subject: [PATCH] Allow plugins to load private DLL files from the plugins folder The problem is the way Windows searches for dynamically linked DLLs (Dynamic-Link Library Search Order). For example, LuaScript.dll is the plug-in DLL, loaded by Notepad++.exe, and lua53.dll is the auxiliary DLL, required by LuaScript.dll and also by user-installed Lua modules. Both DLLs should be located in \plugins\LuaScript. But, when Notepad++ calls LoadLibrary to load LuaScript.dll, Windows will not find the dynamically linked lua53.dll, hence the LoadLibrary call fails. This happens, because Windows will look in the application directory (which is the location of Notepad++.exe), and not in the LuaScript.dll directory. To make Windows search in the LuaScript.dll directory, Notepad++ needs to call LoadLibraryEx with the LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR flag. Close #5802, close #5853 --- PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp b/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp index 7a099761..9e3ebd38 100644 --- a/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp +++ b/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp @@ -114,6 +114,9 @@ cleanup: // release all of our handles return machine_type; } +#define LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR 0x00000100 +#define LOAD_LIBRARY_SEARCH_DEFAULT_DIRS 0x00001000 + int PluginsManager::loadPlugin(const TCHAR *pluginFilePath) { const TCHAR *pluginFileName = ::PathFindFileName(pluginFilePath); @@ -130,7 +133,8 @@ int PluginsManager::loadPlugin(const TCHAR *pluginFilePath) if (GetBinaryArchitectureType(pluginFilePath) != ARCH_TYPE) throw generic_string(ARCH_ERR_MSG); - pi->_hLib = ::LoadLibrary(pluginFilePath); + const DWORD dwFlags = GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "AddDllDirectory") != NULL ? LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS : 0; + pi->_hLib = ::LoadLibraryEx(pluginFilePath, NULL, dwFlags); if (!pi->_hLib) { generic_string lastErrorMsg = GetLastErrorAsString();