-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @file error_handling.h | ||
* @brief Error handling. | ||
* @author Chen Zhenshuo (chenzs108@outlook.com) | ||
* @version 1.0 | ||
* @date 2020-01-09 | ||
* | ||
* @par GitHub | ||
* https://github.com/czs108/ | ||
* | ||
* @see https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes/ | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <windows.h> | ||
|
||
/** | ||
* @brief Get the last error code. | ||
* | ||
* @return The error code. | ||
*/ | ||
DWORD GetLastErrorCode(); | ||
|
||
|
||
/** | ||
* @brief Set the last error code got from `GetLastError` API. | ||
* | ||
* @param error_code The error code. | ||
* | ||
* @warning This method can only and must be called after a direct Windows API call failed. | ||
* | ||
* @attention | ||
* Do not recommend using this method when freeing resources like memory and handles. | ||
* The failure of related APIs may cover the error during initialization. | ||
*/ | ||
void SetLastErrorCode(); | ||
|
||
|
||
/** | ||
* @brief Reset the last error code to `ERROR_SUCCESS`. | ||
*/ | ||
void ResetLastErrorCode(); | ||
|
||
|
||
/** | ||
* @brief Get the message from the error code. | ||
* | ||
* @param error_code The error code got from `GetLastError` API. | ||
* @return The message or `NULL` if the method failed. | ||
* | ||
* @warning The reture value must be free by `FreeErrorMessage()` method if it is not `NULL`. | ||
*/ | ||
TCHAR *FormatErrorMessage( | ||
const DWORD error_code); | ||
|
||
|
||
/** | ||
* @brief Free the memory of message. | ||
* | ||
* @param message The message got from `FormatErrorMessage()` method. | ||
*/ | ||
void FreeErrorMessage( | ||
TCHAR *const message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @file error_handling.c | ||
* @brief Error handling. | ||
* @author Chen Zhenshuo (chenzs108@outlook.com) | ||
* @version 1.0 | ||
* @date 2020-01-09 | ||
* | ||
* @par GitHub | ||
* https://github.com/czs108/ | ||
* | ||
* @see https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes/ | ||
*/ | ||
|
||
#include "error_handling.h" | ||
|
||
static DWORD last_error_code = ERROR_SUCCESS; | ||
|
||
|
||
DWORD GetLastErrorCode() | ||
{ | ||
return last_error_code; | ||
} | ||
|
||
|
||
void SetLastErrorCode() | ||
{ | ||
last_error_code = GetLastError(); | ||
} | ||
|
||
|
||
void ResetLastErrorCode() | ||
{ | ||
last_error_code = ERROR_SUCCESS; | ||
} | ||
|
||
|
||
TCHAR *FormatErrorMessage( | ||
const DWORD error_code) | ||
{ | ||
TCHAR *const message = NULL; | ||
FormatMessage( | ||
FORMAT_MESSAGE_ALLOCATE_BUFFER | ||
| FORMAT_MESSAGE_FROM_SYSTEM | ||
| FORMAT_MESSAGE_IGNORE_INSERTS, | ||
NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), | ||
(LPTSTR)&message, 0, NULL); | ||
return message; | ||
} | ||
|
||
|
||
void FreeErrorMessage( | ||
TCHAR *const message) | ||
{ | ||
if (message != NULL) | ||
{ | ||
LocalFree(message); | ||
} | ||
} |