From e7748e3f73073df38d5f1485d964273b570d6d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Vuchener?= Date: Tue, 29 Dec 2020 17:03:50 +0100 Subject: [PATCH] Add an error message when LoadLibrary fails --- library/PlugLoad-windows.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/library/PlugLoad-windows.cpp b/library/PlugLoad-windows.cpp index 8d64e315c..96c2e900a 100644 --- a/library/PlugLoad-windows.cpp +++ b/library/PlugLoad-windows.cpp @@ -24,6 +24,7 @@ distribution. #define DFhackCExport extern "C" __declspec(dllexport) +#define NOMINMAX #include #include #include @@ -35,6 +36,7 @@ distribution. #include "tinythread.h" #include "modules/Graphic.h" +#include "../plugins/uicommon.h" /* * Plugin loading functions @@ -44,7 +46,22 @@ namespace DFHack DFLibrary* GLOBAL_NAMES = (DFLibrary*)GetModuleHandle(nullptr); DFLibrary * OpenPlugin (const char * filename) { - return (DFLibrary *) LoadLibrary(filename); + DFLibrary *ret = (DFLibrary *) LoadLibrary(filename); + if (!ret) { + DWORD error = GetLastError(); + LPSTR buf = NULL; + DWORD len = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0, NULL); + if (len > 0) { + std::string message = buf; + std::cerr << "LoadLibrary " << filename << ": " << rtrim(message) << " (" << error << ")" << std::endl; + LocalFree(buf); + } + else { + std::cerr << "LoadLibrary " << filename << ": unknown error " << error << std::endl; + } + } + return ret; } void * LookupPlugin (DFLibrary * plugin ,const char * function) { @@ -54,4 +71,4 @@ namespace DFHack { FreeLibrary((HMODULE) plugin); } -} \ No newline at end of file +}