From 6874413b65dcf6695cde1d343ad4cec4d5b978eb Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Fri, 24 Jan 2025 15:04:35 +0100 Subject: [PATCH] Win: Add ntstatus_error Signed-off-by: Simon Rozman --- include/WinStd/Win.h | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/include/WinStd/Win.h b/include/WinStd/Win.h index 6d9253cc..304ffc1c 100644 --- a/include/WinStd/Win.h +++ b/include/WinStd/Win.h @@ -1994,6 +1994,65 @@ namespace winstd }; /// @} + + /// \addtogroup WinStdExceptions + /// @{ + + /// + /// NTSTATUS error + /// + class ntstatus_error : public num_runtime_error + { + public: + /// + /// Constructs an exception + /// + /// \param[in] num NTSTATUS error code + /// + ntstatus_error(_In_ error_type num) : num_runtime_error(num, message(num)) + {} + + /// + /// Constructs an exception + /// + /// \param[in] num NTSTATUS error code + /// \param[in] msg Error message + /// + ntstatus_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error(num, msg + ": " + message(num)) + {} + + /// + /// Constructs an exception + /// + /// \param[in] num NTSTATUS error code + /// \param[in] msg Error message + /// + ntstatus_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error(num, std::string(msg) + ": " + message(num)) + {} + + protected: + /// + /// Returns a user-readable NTSTATUS error message. + /// As std::exception messages may only be char*, we use UTF-8 by convention. + /// + /// \sa [FormatMessage function](https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-formatmessage) + /// + static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0) + { + std::wstring wstr; + winstd::library ntdll(LoadLibraryW(L"NTDLL.DLL")); + if (ntdll && FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, (HMODULE)ntdll, num, dwLanguageId, wstr, NULL)) { + // Stock Windows error messages contain CRLF. Well... Trim all the trailing white space. + wstr.erase(wstr.find_last_not_of(L" \t\n\r\f\v") + 1); + } else + sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num); + std::string str; + WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL); + return str; + } + }; + + /// @} } /// \addtogroup WinStdWinAPI