1
0
mirror of synced 2024-11-28 01:20:51 +01:00

fix: Decoding of Windows error messages being broken

This commit is contained in:
WerWolv 2024-02-07 21:51:37 +01:00
parent 1195d2f2e4
commit c3d755a3e2

View File

@ -12,6 +12,8 @@
#if defined(OS_WINDOWS)
#include <windows.h>
#include <shellapi.h>
#include <wolv/utils/guards.hpp>
#elif defined(OS_LINUX)
#include <unistd.h>
#include <hex/helpers/utils_linux.hpp>
@ -751,12 +753,24 @@ namespace hex {
}
std::string formatSystemError(i32 error) {
auto message = std::system_category().message(error);
#if defined(OS_WINDOWS)
return hex::utf16ToUtf8({ message.begin(), message.end() });
wchar_t *message = nullptr;
auto wLength = FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(wchar_t*)&message, 0,
nullptr
);
ON_SCOPE_EXIT { LocalFree(message); };
auto length = ::WideCharToMultiByte(CP_UTF8, 0, message, wLength, nullptr, 0, nullptr, nullptr);
std::string result(length, '\x00');
::WideCharToMultiByte(CP_UTF8, 0, message, wLength, result.data(), length, nullptr, nullptr);
return result;
#else
return message;
return std::system_category().message(error);
#endif
}