Win: Add ntstatus_error
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
a0aa5f177d
commit
6874413b65
@ -1994,6 +1994,65 @@ namespace winstd
|
||||
};
|
||||
|
||||
/// @}
|
||||
|
||||
/// \addtogroup WinStdExceptions
|
||||
/// @{
|
||||
|
||||
///
|
||||
/// NTSTATUS error
|
||||
///
|
||||
class ntstatus_error : public num_runtime_error<LONG>
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Constructs an exception
|
||||
///
|
||||
/// \param[in] num NTSTATUS error code
|
||||
///
|
||||
ntstatus_error(_In_ error_type num) : num_runtime_error<LONG>(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<LONG>(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<LONG>(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
|
||||
|
Loading…
x
Reference in New Issue
Block a user