📜 ⬆️ ⬇️

List of functions for getting the text of errors from their codes (WinAPI)



Hello!

I present an overview of the functions for obtaining the text of errors from their codes, which is presented in the Error Lookup program.

1. FormatMessage

This function is not so simple as it may seem (most of the functionality in the above program is implemented through it, because this function can be configured to receive NOT only system error codes, see clause 2). By default, the function gives the system error code.
')
Example:
The function gets the error text from the system error code.

//      FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 1337, 0, lpszBuffer, cchBuffer, NULL); printf("Error Code: %d\nError Name: %s", 1337, lpszBuffer); 


Result:
 Error Code: 1337 Error Text:     . 

2. FormatMessage + FORMAT_MESSAGE_FROM_HMODULE

When the FORMAT_MESSAGE_FROM_HMODULE flag is set, you can load a list of errors from the module (DLL) which contains the list of errors:


You can also use this method in your projects; you just need to pack a message table resource inside the library (thanks to ertaquo )

Example:
In this example, the error base is loaded from the ntdll.dll file.

 //  ntdll.dll    NTSTATUS  FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, LoadLibrary("ntdll.dll"), -1072037872, 0, lpszBuffer, cchBuffer, NULL); printf("Error Code: %d\nError Name: %s", -1072037872, lpszBuffer); 


Result:
 Error Code: -1072037872 Error Text:          . 

3. DXGetErrorString & DXGetErrorDescription



Example:
An example of getting a DirectX error:

 //   DirectX  printf("Error Code: %d\nError Name: %s\nError Text: %s", 1337, DXGetErrorString((HRESULT)1337), DXGetErrorDescription((HRESULT)1337)); 


Result:
 Error Code: 1337 Error Name: ERROR_INVALID_SID Error Text: The security ID structure is invalid. 

4. RasGetErrorString

This function receives error text from the RAS function library.

Example:
The function gets the error text from the RAS error code.

 //    RAS  RasGetErrorString(633, lpszBuffer, cchBuffer); printf("Error Code %d\nError Text: %s", 633, lpszBuffer); 


Result:
 Error Code: 633 Error Text:          . 

5. GetIpErrorString

This function is to get the error text from the IP Helper Library function library.

Example:
The function gets the error text

 //    IP Helper Library GetIpErrorString(12, lpszBuffer, cchBuffer); printf("Error Code %d\nError Text: %s", 12, lpszBuffer); 


Result:
 Error Code: 12 Error Text: General failure. 

Bonus

Links to download the program

Source: https://habr.com/ru/post/149116/


All Articles