diff --git a/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp b/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp index 663cce543..03200698b 100644 --- a/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp +++ b/PowerEditor/src/MISC/PluginsManager/PluginsManager.cpp @@ -27,15 +27,23 @@ #include +#include #include #include "PluginsManager.h" #include "resource.h" using namespace std; -const TCHAR * USERMSG = TEXT("This plugin is not compatible with current version of Notepad++.\n\n\ -Do you want to remove this plugin from plugins directory to prevent this message from the next launch time?"); +const TCHAR * USERMSG = TEXT(" is not compatible with the current version of Notepad++.\n\n\ +Do you want to remove this plugin from the plugins directory to prevent this message from the next launch?"); +#ifdef _WIN64 +#define ARCH_TYPE IMAGE_FILE_MACHINE_AMD64 +const TCHAR *ARCH_ERR_MSG = TEXT("Cannot load 32-bit plugin."); +#else +#define ARCH_TYPE IMAGE_FILE_MACHINE_I386 +const TCHAR *ARCH_ERR_MSG = TEXT("Cannot load 64-bit plugin."); +#endif @@ -82,6 +90,44 @@ static std::wstring GetLastErrorAsString() return message; } +static WORD GetBinaryArchitectureType(const TCHAR *filePath) +{ + WORD machine_type = IMAGE_FILE_MACHINE_UNKNOWN; + HANDLE hMapping = NULL; + LPVOID addrHeader = NULL; + + HANDLE hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); + if (hFile == INVALID_HANDLE_VALUE) + goto cleanup; + + hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY | SEC_IMAGE, 0, 0, NULL); + if (hMapping == NULL) + goto cleanup; + + addrHeader = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); + if (addrHeader == NULL) + goto cleanup; // couldn't memory map the file + + PIMAGE_NT_HEADERS peHdr = ImageNtHeader(addrHeader); + if (peHdr == NULL) + goto cleanup; // couldn't read the header + + // Found the binary and architecture type + machine_type = peHdr->FileHeader.Machine; + +cleanup: // release all of our handles + if (addrHeader != NULL) + UnmapViewOfFile(addrHeader); + + if (hMapping != NULL) + CloseHandle(hMapping); + + if (hFile != INVALID_HANDLE_VALUE) + CloseHandle(hFile); + + return machine_type; +} + int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector & dll2Remove) { const TCHAR *pluginFileName = ::PathFindFileName(pluginFilePath); @@ -95,6 +141,9 @@ int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector_moduleName = PathFindFileName(pluginFilePath); + if (GetBinaryArchitectureType(pluginFilePath) != ARCH_TYPE) + throw generic_string(ARCH_ERR_MSG); + pi->_hLib = ::LoadLibrary(pluginFilePath); if (!pi->_hLib) { @@ -230,6 +279,7 @@ int PluginsManager::loadPlugin(const TCHAR *pluginFilePath, vector /fixed:no %(AdditionalOptions) - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -142,7 +142,7 @@ /fixed:no %(AdditionalOptions) - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -182,7 +182,7 @@ true - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -231,7 +231,7 @@ copy ..\src\contextMenu.xml ..\bin\contextMenu.xml true - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 diff --git a/PowerEditor/visual.net/notepadPlus.vs2015.vcxproj b/PowerEditor/visual.net/notepadPlus.vs2015.vcxproj index bbdb3c058..8cad6e577 100644 --- a/PowerEditor/visual.net/notepadPlus.vs2015.vcxproj +++ b/PowerEditor/visual.net/notepadPlus.vs2015.vcxproj @@ -108,7 +108,7 @@ /fixed:no %(AdditionalOptions) - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -144,7 +144,7 @@ /fixed:no %(AdditionalOptions) - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -185,7 +185,7 @@ 4091;4456;4457;4459 - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0 @@ -236,7 +236,7 @@ copy ..\src\contextMenu.xml ..\bin\contextMenu.xml 4091;4456;4457;4459 - comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;%(AdditionalDependencies) + comctl32.lib;shlwapi.lib;shell32.lib;Oleacc.lib;Dbghelp.lib;%(AdditionalDependencies) LinkVerboseLib $(OutDir)notepad++.exe 1.0