Add last_error_saver
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
parent
9da457ab2b
commit
0559794633
@ -1517,6 +1517,35 @@ namespace winstd
|
||||
error_type m_num; ///< Numeric error code
|
||||
};
|
||||
|
||||
///
|
||||
/// Saves GetLastError and restores SetLastError when going out of scope
|
||||
///
|
||||
class last_error_saver
|
||||
{
|
||||
public:
|
||||
///
|
||||
/// Saves the calling thread's last-error code value.
|
||||
///
|
||||
/// \sa [GetLastError function](https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror)
|
||||
///
|
||||
last_error_saver(_In_ DWORD error = GetLastError()) :
|
||||
m_error(error)
|
||||
{}
|
||||
|
||||
///
|
||||
/// Sets the last-error code for the calling thread.
|
||||
///
|
||||
/// \sa [SetLastError function](https://learn.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-setlasterror)
|
||||
///
|
||||
~last_error_saver()
|
||||
{
|
||||
SetLastError(m_error);
|
||||
}
|
||||
|
||||
protected:
|
||||
DWORD m_error;
|
||||
};
|
||||
|
||||
///
|
||||
/// Windows runtime error
|
||||
///
|
||||
@ -1580,7 +1609,7 @@ namespace winstd
|
||||
///
|
||||
static std::string message(_In_ error_type num, _In_opt_ DWORD dwLanguageId = 0)
|
||||
{
|
||||
error_type runtime_num = GetLastError();
|
||||
last_error_saver last_error_save;
|
||||
std::wstring wstr;
|
||||
if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, num, dwLanguageId, wstr, NULL)) {
|
||||
// Stock Windows error messages contain CRLF. Well... Trim all the trailing white space.
|
||||
@ -1589,7 +1618,6 @@ namespace winstd
|
||||
sprintf(wstr, num >= 0x10000 ? L"Error 0x%X" : L"Error %u", num);
|
||||
std::string str;
|
||||
WideCharToMultiByte(CP_UTF8, 0, wstr, str, NULL, NULL);
|
||||
SetLastError(runtime_num);
|
||||
return str;
|
||||
}
|
||||
};
|
||||
|
@ -1590,9 +1590,8 @@ namespace winstd
|
||||
return;
|
||||
|
||||
revert:
|
||||
DWORD dwResult = GetLastError();
|
||||
last_error_saver last_error_save;
|
||||
RevertToSelf();
|
||||
SetLastError(dwResult);
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user