From 77736d35e56c217b8c3e836eaf29730bb14178cb Mon Sep 17 00:00:00 2001 From: dankmeme01 <42031238+dankmeme01@users.noreply.github.com> Date: Tue, 23 Jan 2024 00:21:09 +0100 Subject: [PATCH] better error messages on dll load fail --- loader/src/platform/windows/ModImpl.cpp | 26 ++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/loader/src/platform/windows/ModImpl.cpp b/loader/src/platform/windows/ModImpl.cpp index 21418a61..9154270a 100644 --- a/loader/src/platform/windows/ModImpl.cpp +++ b/loader/src/platform/windows/ModImpl.cpp @@ -49,12 +49,28 @@ std::string getLastWinError() { if (!err) return "None (0)"; auto useful = getUsefulError(err); if (useful) return useful; - auto len = FormatMessageA( + + char* errorBuf = nullptr; + auto result = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), nullptr, 0, nullptr - ); - std::string msg = len == 0 ? "Unknown" : "Very Unknown"; - return msg + " (" + std::to_string(err) + ")"; + nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&errorBuf, 0, nullptr); + + std::string msg; + if (result == 0 || !errorBuf) { + msg = fmt::format("Unknown ({})", err); + } else { + msg = std::string(errorBuf, errorBuf + result); + // the string sometimes includes a crlf, strip it + msg.erase(std::find_if(msg.rbegin(), msg.rend(), [](unsigned char ch) { + return ch != '\r' && ch != '\n'; + }).base(), msg.end()); + } + + if (errorBuf) { + LocalFree(errorBuf); + } + + return msg; } Result<> Mod::Impl::loadPlatformBinary() {