Windows Error code parsing and windows Error Parsing
C or C ++ development will often encounter various error codes. Since each error code is only an enumeration or an integer value, you cannot understand the specific meaning of this error code when debugging or outputting logs, at this time, you need to explain this error code. You can parse your own error codes. For Windows API error codes, you need to call Windows API for parsing. The following describes the specific error code parsing method.
Windows API error code parsing
After a Windows API call fails, you usually need to get the corresponding error code through GetLastError. to parse the error code as the corresponding description, you need to call another Windows APIFormatMessageThe prototype is as follows:
DWORD WINAPI FormatMessage( _In_ DWORD dwFlags, _In_opt_ LPCVOID lpSource, _In_ DWORD dwMessageId, _In_ DWORD dwLanguageId, _Out_ LPTSTR lpBuffer, _In_ DWORD nSize, _In_opt_ va_list *Arguments);
The specific parameter meaning is not described here. You can simply view MSDN. The following describes the specific usage example:
char msg[1024] = { 0 };FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 5, LANG_NEUTRAL, msg, _countof(msg), NULL);cout << msg << endl;
Run the program to see the corresponding error description:Access denied.
Abnormal Value parsing in Windows
When the program is running, it will inevitably produce some exceptions. This exception information can be obtained through the Windows SEH mechanism, which contains an abnormal value, such as 0xC0000008. If the preceding FormatMessage method is used, the obtained message is a string, or the description of this abnormal value cannot be obtained. Then, how can we get the description of this abnormal value? After carefully reading the MSDN document of FormatMessage API, we can see the following mark:
FORMAT_MESSAGE_FROM_HMODULE
This flag indicates that another module can be used to explain the abnormal values or error codes.
So what modules will contain the definition and explanation of Windows abnormal values? Google or MSDN can tell that, yes"Ntdll. dll".
Once you know this, you can write a program to explain Windows abnormal values. The Code is as follows:
char msg[1024] = { 0 };HMODULE hntdll = LoadLibrary("NTDLL.DLL");FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE, hntdll, 0xC0000008, LANG_NEUTRAL, msg, _countof(msg), NULL);FreeLibrary(hntdll);cout << msg << endl;
ErrLook Program
When the program does need to parse Windows Error Codes and Windows abnormal values, you can use the above method for parsing.
But when we simply want to see an explanation of an error code, do we have to write a program to explain it.
Microsoft also considers this point you are thinking about, so after VS is installed, a small tool will be included to explain the error code, called:ErrLookIn general, you can see it in the tool menu of VS, or you can find this program in the "VS directory \ Common7 \ Tools" directory, as shown below:
References
FormatMessage function
How to translate NTSTATUS error codes to message strings