Back to Home

List of functions for obtaining error text from their codes (WinAPI)

formatmessage · error · winapi · c · c ++ · msdn

List of functions for obtaining error text from their codes (WinAPI)



Hello!

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

1. FormatMessage

This function is not as 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 point 2). By default, the function returns a system error code.

Example:
A function gets the error text from a 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 the list of errors from the module (DLL) in which the list of errors is located:


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

Example:
In this example, the error database 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

  • DXGetErrorString - function to get the error name (for example, ERROR_INVALID_SID )
  • DXGetErrorDescription - function to get the error text (for example, The security ID structure is invalid. )


Example:
Example of receiving 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 for gets the error text from the RAS function library

Example:
The function receives 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 to get the error text from the IP Helper Library.

Example:
The function receives 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

Program Download Links

Read Next