From 9082209b4e1233341d5e990194d903872f71e4ce Mon Sep 17 00:00:00 2001 From: dragonmux Date: Tue, 31 Oct 2023 22:52:55 +0000 Subject: [PATCH] windows/serialInterface: Force FormatMessage() into wide mode to guarantee we get valid Unicode output from it to feed into substrate --- src/windows/serialInterface.cxx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/windows/serialInterface.cxx b/src/windows/serialInterface.cxx index c5cad1f..9b18509 100644 --- a/src/windows/serialInterface.cxx +++ b/src/windows/serialInterface.cxx @@ -24,10 +24,11 @@ constexpr static auto uncDeviceSuffix{"\\\\.\\"sv}; void displayError(const LSTATUS error, const std::string_view operation, const std::string_view path) { - char *message{nullptr}; + wchar_t *message{nullptr}; // Ask Windows to please tell us what the error value we have means, and allocate + store it in `message` - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, - error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast(&message), 0, nullptr); + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, + static_cast(error), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast(&message), 0, + nullptr); console.error("Failed to "sv, operation, ' ', path, ", got error "sv, asHex_t<8, '0'>{uint32_t(error)}, ": "sv, message); // Clean up properly after ourselves @@ -225,10 +226,10 @@ void serialInterface_t::handleDeviceError(const std::string_view operation) noex // If there is no error and no device (we failed to look up the device node), return early if (error == ERROR_SUCCESS && device == INVALID_HANDLE_VALUE) return; - char *message{nullptr}; + wchar_t *message{nullptr}; // Ask Windows to please tell us what the error value we have means, and allocate + store it in `message` - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, - error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast(&message), 0, nullptr); + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, + error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast(&message), 0, nullptr); console.error("Failed to "sv, operation, " ("sv, asHex_t<8, '0'>{error}, "): "sv, message); // Clean up properly after ourselves LocalFree(message);