From 4be04e28bf0c382f53786f57954a05b28825f403 Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Mon, 16 Jul 2018 12:54:17 +0200 Subject: [PATCH] ui: improved windows error handling for loading external dlls to display an error text --- src/lib/utility/utilityLibrary.h | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/lib/utility/utilityLibrary.h b/src/lib/utility/utilityLibrary.h index 35b59079..ed1e1614 100644 --- a/src/lib/utility/utilityLibrary.h +++ b/src/lib/utility/utilityLibrary.h @@ -14,6 +14,7 @@ #endif #include "utility/file/FilePath.h" +#include "utility/utilityString.h" namespace utility { @@ -26,9 +27,34 @@ namespace utility #ifdef _WIN32 const std::string libraryPathString = libraryPath.getBackslashedString(); HINSTANCE handle = LoadLibrary(libraryPathString.c_str()); - if (handle == NULL) + if (handle == nullptr) { - errorString = "Could not load library \"" + libraryPathString + "\""; + DWORD errorCode = GetLastError(); + std::string errorReasonString; + + LPSTR messageBuffer = nullptr; + const size_t size = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, + errorCode, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&messageBuffer, + 0, + nullptr + ); + if (size > 0) + { + std::string message(messageBuffer, size); + errorReasonString = "error \"" + utility::trim(message) + "\" (code " + std::to_string(errorCode) + ")."; + } + else + { + errorReasonString = "error code " + std::to_string(errorCode) + "."; + } + + LocalFree(messageBuffer); + + errorString = "Could not load library \"" + libraryPathString + "\" because of " + errorReasonString; return std::function(); }