You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/05/20 13:33:08 UTC

[GitHub] [arrow] pitrou commented on a diff in pull request #13203: ARROW-16617: [C++] Add support for multi-byte system error message on Windows

pitrou commented on code in PR #13203:
URL: https://github.com/apache/arrow/pull/13203#discussion_r878149006


##########
cpp/src/arrow/util/io_util.cc:
##########
@@ -203,16 +203,26 @@ std::string ErrnoMessage(int errnum) { return std::strerror(errnum); }
 
 #if _WIN32
 std::string WinErrorMessage(int errnum) {
-  char buf[1024];
-  auto nchars = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
-                               NULL, errnum, 0, buf, sizeof(buf), NULL);
-  if (nchars == 0) {
+#define MAX_N_CHARACTERS 1024
+#define MAX_N_UTF8_BYTES 4096

Review Comment:
   Can we use `constexpr` instead?



##########
cpp/src/arrow/util/io_util.cc:
##########
@@ -203,16 +203,26 @@ std::string ErrnoMessage(int errnum) { return std::strerror(errnum); }
 
 #if _WIN32
 std::string WinErrorMessage(int errnum) {
-  char buf[1024];
-  auto nchars = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
-                               NULL, errnum, 0, buf, sizeof(buf), NULL);
-  if (nchars == 0) {
+#define MAX_N_CHARACTERS 1024
+#define MAX_N_UTF8_BYTES 4096
+  WCHAR utf16_message[MAX_N_CHARACTERS];
+  auto n_utf16_chars =
+      FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
+                     errnum, 0, utf16_message, sizeof(utf16_message), NULL);
+  if (n_utf16_chars == 0) {
     // Fallback
     std::stringstream ss;
     ss << "Windows error #" << errnum;
     return ss.str();
   }
-  return std::string(buf, nchars);
+  const UINT code_page = CP_UTF8;
+  const DWORD flags = 0;
+  char utf8_message[MAX_N_UTF8_BYTES];
+  auto n_utf8_bytes = WideCharToMultiByte(code_page, flags, utf16_message, n_utf16_chars,

Review Comment:
   You can use `WideStringToUTF8`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org