Retrieve Windows error message on clipboard failures

This commit is contained in:
Lukas Dürrenberger 2024-05-25 00:37:41 +02:00 committed by Chris Thrasher
parent 658176879a
commit e39f48742b

View File

@ -31,6 +31,21 @@
#include <iostream> #include <iostream>
#include <windows.h> #include <windows.h>
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<PTCHAR>(&buffer), 0, NULL) == 0)
return "Unknown error.";
sf::String message = buffer;
LocalFree(buffer);
return message.toAnsiString();
}
}
namespace sf namespace sf
{ {
@ -43,13 +58,13 @@ String ClipboardImpl::getString()
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) 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; return text;
} }
if (!OpenClipboard(NULL)) 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; return text;
} }
@ -57,7 +72,7 @@ String ClipboardImpl::getString()
if (!clipboard_handle) 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(); CloseClipboard();
return text; return text;
} }
@ -75,13 +90,13 @@ void ClipboardImpl::setString(const String& text)
{ {
if (!OpenClipboard(NULL)) if (!OpenClipboard(NULL))
{ {
err() << "Failed to open the Win32 clipboard." << std::endl; err() << "Failed to open the Win32 clipboard: " << getErrorString(GetLastError()) << std::endl;
return; return;
} }
if (!EmptyClipboard()) if (!EmptyClipboard())
{ {
err() << "Failed to empty the Win32 clipboard." << std::endl; err() << "Failed to empty the Win32 clipboard: " << getErrorString(GetLastError()) << std::endl;
CloseClipboard(); CloseClipboard();
return; return;
} }