From 24a205a1aa80318c72ffd9d2a84b05b88845cb3a Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Mon, 8 Mar 2021 22:53:59 +0100
Subject: [PATCH] win32/HResult: try to use FormatMessage()

---
 src/win32/HResult.cxx | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/win32/HResult.cxx b/src/win32/HResult.cxx
index a40116a1b..0daa80163 100644
--- a/src/win32/HResult.cxx
+++ b/src/win32/HResult.cxx
@@ -18,6 +18,7 @@
  */
 
 #include "HResult.hxx"
+#include "system/Error.hxx"
 
 #include <cassert>
 #include <cstdarg>
@@ -27,11 +28,21 @@
 std::string
 HResultCategory::message(int Errcode) const
 {
+	char buffer[256];
+
+	/* FormatMessage() supports some HRESULT values (depending on
+	   the Windows version) */
+	if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
+			   FORMAT_MESSAGE_IGNORE_INSERTS,
+			   nullptr, Errcode, 0,
+			   buffer, sizeof(buffer),
+			   nullptr))
+		return buffer;
+
 	const auto msg = HRESULTToString(Errcode);
 	if (!msg.empty())
 		return std::string(msg);
 
-	char buffer[11]; // "0x12345678\0"
 	int size = snprintf(buffer, sizeof(buffer), "0x%1x", Errcode);
 	assert(2 <= size && size <= 10);
 	return std::string(buffer, size);