ui: improved windows error handling for loading external dlls to display an error text

This commit is contained in:
mlangkabel
2018-07-16 12:54:17 +02:00
parent 855d8f4e2e
commit 4be04e28bf
+28 -2
View File
@@ -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<Ret(Args...)>();
}