Introduction
Developers usually use Error Lookup Application provided along with the Visual Studio to convert the GetLastError()
to user-friendly error strings with detailed description. Well, at times, we also need to query NTSTATUS
code lookup through FormatMessage
function, this is not so handy. So I decided to write a NTSTATUS
error code lookup application.
Background
This is very simple in the sense that just change the Messagesource
in FormatMessage
function to the handle of NTDLL.DLL.
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|
FORMAT_MESSAGE_FROM_HMODULE,Hand,ntStatusMsg,
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
(LPTSTR)&lpMessageBuffer,0,NULL);
Detailed Explanation
�The FormatMessage
function formats a message string. The function requires a message definition as input. The message definition can come from a buffer passed into the function. It can come from a message table resource in an already-loaded module. Or the caller can ask the function to search the system's message table resource(s) for the message definition. The function finds the message definition in a message table resource based on a message identifier and a language identifier. The function copies the formatted message text to an output buffer, processing any embedded insert sequences if requested.� The syntax of FormatMessage
is:
DWORD FormatMessage(DWORD dwFlags,LPCVOID lpSource,
DWORD dwMessageID,DWORD dwLanguageID,LPTSTR lpBuffer,
DWORD nSize,va_list* Arguments);
The first parameter dwFlags
contains the formatting options which are important to us. The full application is included along with. The whole trick is get the handle to the Error Code resource by LoadLibrary
.
HMODULE Hand = LoadLibrary("NTDLL.DLL");
Then simply format the message and pass the ntStatus
code to get the corresponding string. Simple !! Isn�t it? But an application would come very handy as error lookup.
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_FROM_HMODULE,
Hand,ntStatusMsg,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
(LPTSTR)&lpMessageBuffer,0,NULL);
That's it! So simple. I have made a sample application. You are free to do whatever you want to do with that.
Future Releases
- To make it as a Visual Studio add-in.
- To port it to Visual Studio .NET 2003 and make it as add-in for that also.
That's it !!
I know it is too simple, but I found it an interesting way to make a small application that I need for myself and share with my fellow developers.