diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index 0e5bcc248..76b9d6732 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -12,6 +12,8 @@ #if defined(OS_WINDOWS) #include #include + + #include #elif defined(OS_LINUX) #include #include @@ -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 }