Skip to content

Commit

Permalink
windows/serialInterface: Force FormatMessage() into wide mode to guar…
Browse files Browse the repository at this point in the history
…antee we get valid Unicode output from it to feed into substrate
  • Loading branch information
dragonmux authored and esden committed Nov 13, 2023
1 parent 6143a6b commit 9082209
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/windows/serialInterface.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<char *>(&message), 0, nullptr);
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
static_cast<DWORD>(error), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<wchar_t *>(&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
Expand Down Expand Up @@ -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<char *>(&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<wchar_t *>(&message), 0, nullptr);
console.error("Failed to "sv, operation, " ("sv, asHex_t<8, '0'>{error}, "): "sv, message);
// Clean up properly after ourselves
LocalFree(message);
Expand Down

0 comments on commit 9082209

Please sign in to comment.