Click here to Skip to main content
15,892,674 members
Articles / Desktop Programming / MFC
Article

An NTSTATUS lookup application

Rate me:
Please Sign up or sign in to vote.
1.65/5 (9 votes)
23 Mar 20042 min read 58.9K   1.1K   12   6
At times, we 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.

Sample Image - NtStatus.gif

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Tarun started programming with BASIC,FORTRAN while in school and then gradually became intrested in c,c++ He is now a professional programmer. He is mechanical engineer by qualifications but loves to code and work on latest technologies.
He has worked on various technologies and platforms - Wi-Fi, ISA Servers,Sendmail,VOIP,NDIS Drivers,VLANs etc.He likes to help and mentor people.So if you need any sort of technical help just mail him.

visit him at : www.tarunsadhana.com

Comments and Discussions

 
GeneralAnother alternative: Error Lookup enhanced Pin
Antti Keskinen14-Feb-07 3:02
Antti Keskinen14-Feb-07 3:02 
GeneralAlternative: ntstatus.h Pin
zyklopen18-Jan-07 5:02
zyklopen18-Jan-07 5:02 
ntstatus.h from the DDK is good way to find error codes. It contains Message IDs, ntstatus codes and english message text.
English messages are particularly useful for people who have a localized version of Windows and receive weird translations of error messages that most of the time do not help in the slightest way.

Regards,
Holger
GeneralTools | Error Lookup Pin
Ravi Bhavnani24-Mar-04 4:06
professionalRavi Bhavnani24-Mar-04 4:06 
GeneralRe: Tools | Error Lookup Pin
Toby Opferman24-Mar-04 6:38
Toby Opferman24-Mar-04 6:38 
GeneralRe: Tools | Error Lookup Pin
Tarundeep Singh Kalra24-Mar-04 17:19
Tarundeep Singh Kalra24-Mar-04 17:19 
GeneralRe: Tools | Error Lookup Pin
Tarundeep Singh Kalra24-Mar-04 17:20
Tarundeep Singh Kalra24-Mar-04 17:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.