From e39f48742b702400424db46f3b28e8a09459ca38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Sat, 25 May 2024 00:37:41 +0200 Subject: [PATCH] Retrieve Windows error message on clipboard failures --- src/SFML/Window/Win32/ClipboardImpl.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/SFML/Window/Win32/ClipboardImpl.cpp b/src/SFML/Window/Win32/ClipboardImpl.cpp index 342bb2d38..8c594bb10 100644 --- a/src/SFML/Window/Win32/ClipboardImpl.cpp +++ b/src/SFML/Window/Win32/ClipboardImpl.cpp @@ -31,6 +31,21 @@ #include #include +namespace +{ + std::string getErrorString(DWORD error) + { + PTCHAR buffer; + + if (FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, reinterpret_cast(&buffer), 0, NULL) == 0) + return "Unknown error."; + + sf::String message = buffer; + LocalFree(buffer); + return message.toAnsiString(); + } +} + namespace sf { @@ -43,13 +58,13 @@ String ClipboardImpl::getString() if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) { - err() << "Failed to get the clipboard data in Unicode format." << std::endl; + err() << "Failed to get the clipboard data in Unicode format: " << getErrorString(GetLastError()) << std::endl; return text; } if (!OpenClipboard(NULL)) { - err() << "Failed to open the Win32 clipboard." << std::endl; + err() << "Failed to open the Win32 clipboard: " << getErrorString(GetLastError()) << std::endl; return text; } @@ -57,7 +72,7 @@ String ClipboardImpl::getString() if (!clipboard_handle) { - err() << "Failed to get Win32 handle for clipboard content." << std::endl; + err() << "Failed to get Win32 handle for clipboard content: " << getErrorString(GetLastError()) << std::endl; CloseClipboard(); return text; } @@ -75,13 +90,13 @@ void ClipboardImpl::setString(const String& text) { if (!OpenClipboard(NULL)) { - err() << "Failed to open the Win32 clipboard." << std::endl; + err() << "Failed to open the Win32 clipboard: " << getErrorString(GetLastError()) << std::endl; return; } if (!EmptyClipboard()) { - err() << "Failed to empty the Win32 clipboard." << std::endl; + err() << "Failed to empty the Win32 clipboard: " << getErrorString(GetLastError()) << std::endl; CloseClipboard(); return; }