Skip to content

Commit

Permalink
add more infos on windows errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Arian8j2 committed Feb 3, 2023
1 parent aac9a60 commit e055349
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion include/detail/windows.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,27 @@ class ClipboardWindows : public ClipboardInterface {

class WindowsException : public exception {
public:
WindowsException(const std::string &reason) : exception(reason + " (" + std::to_string(GetLastError()) + ")"){};
WindowsException(const std::string &reason) : exception(reason + " (" + get_last_windows_error() + ")"){};

private:
std::string get_last_windows_error() const {
DWORD error = GetLastError();
if (error) {
LPVOID buffer = nullptr;
DWORD buffer_len = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&buffer), 0, nullptr);

if (buffer_len) {
LPTSTR buffer_char = reinterpret_cast<LPTSTR>(buffer);
std::string result(buffer_char, buffer_char + buffer_len);
LocalFree(buffer);
return result;
}
}

return std::string("unknown windows error");
}
};

class WindowsPtrDeleter {
Expand Down

0 comments on commit e055349

Please sign in to comment.