How do I convert an error code to text when FORMAT_MESSAGE_FROM_SYSTEM doesn’t work?


For the same reason that not all error codes are defined in winerror.h, not all error strings are defined in the system message table. If you’ve ever played with the message compiler, you’d quickly have recognized the winerror.h file as the header file associated with a message resource. In other words, there’s a .mc file that gets processed by the message compiler, and out pops the message resource (which becomes the system message table) and the winerror.h header file.

As we saw a few years ago, not all error messages live in winerror.h, which means that not all error messages live in the system message table. Each component that provides error codes outside of winerror.h needs to provide its own message table. For example, messages for network errors can be found in netmsg.dll. Similarly, winhttp error message text can be found in winhttp.dll. I don’t know about other message ranges; hopefully the documentation for the component that generates those errors also mentions where you can find the text.

Comments (9)
  1. Koro says:

    Funny thing, in Windows NT 4.0, trying to FormatMessage a 10xxx error message failed, but then, past Windows 2000, it works.

  2. Mike Dimmick says:

    @Koro: not surprising, WinError.h only goes up to 6008 for NT 4.0, but later versions do include WinSock errors.

    Windows Sockets has a history of being nearly part of Windows – the API was defined by Microsoft but for earlier releases, the actual implementation came from other vendors. WinSock 2 was the native API on Windows 98 and NT 4.0 and an installable update was made available for Windows 95. I’m not sure if earlier versions of Windows NT and Windows 95 installed Microsoft’s WinSock unless you installed the TCP/IP stack (not installed by default).

    By Windows 2000 TCP/IP wasn’t just installed by default, it was mandatory, so it makes sense that the error messages made it into the system message table.

  3. Miral says:

    It’d be nice if there were some centralised listing for that sort of thing (at least for MS products).  I’ve lost count of the number of times I’ve tried to use Error Lookup or a similar tool to try and work out what an obscure HRESULT means and it’s reported that it has no idea.

    (This is more often annoying at the user level, though, where an application blindly reports an HRESULT error code in a message box or similar.  I have no idea what components it’s using under the covers, so I can’t look at the component-specific docs.)

    Of course, Google is often sufficient to that task — though for some problems it’s too easy to get drowned in the noise without finding a solution.

  4. Alexander Grigoriev says:

    Mike: "Windows Sockets has a history of being nearly part of Windows – the API was defined by Microsoft but for earlier releases, the actual implementation came from other vendors."

    I can almost see Korean Windows version "KT" without TCP/IP stack, to allow the poor users get one of their choosing.

  5. Worf says:

    The reason you tend to get error numbers than messages is because of the lack of a nice strerror() type function. Instead you get this monstrosity FormatMessage that takes a zillion parameters, and spits out a result, so to use it you have to allocate a buffer and all that nice stuff. And FormatMessage itself requires arcane arts to make messages for. There’s probably a great reason for it, but it certainly isn’t as easy to use as say strerror() or GetLastError().

    Even something that returns the short form message would make everyone’s lives easier…

    Hrm… a possible opportunity here… and to make it GPL to mess with people, too.

  6. Fowl says:

    Error Code Lookup tool

    "Use the Error Code Lookup tool to determine error values from decimal and hexadecimal error codes in Microsoft Windows® operating systems. The tool can look up one or more values at a time. All values on the command line will be looked up in Exchange’s internal tables and presented to you. If available, informational data associated with the value or values will also be shown."

    http://www.microsoft.com/downloads/details.aspx?familyid=be596899-7bb8-4208-b7fc-09e02a13696c&displaylang=en

    Says it’s for exchange but it used to be part of the psdk – and still contains error messages for many things; mine says "There are currently 25553 return codes registered from 175 sources.".

    This is an oldish version a link to a newer version: http://kernelmustard.com/2007/10/09/the-return-of-errexe/

  7. Yuhong Bao says:

    "The reason you tend to get error numbers than messages is because of the lack of a nice strerror() type function."

    Well, what about the quality of the error messages? I looked at them and at least some of them are too technical for the average user.

  8. Michael Puff says:

    You don’t need an extra tool.

    Just type:

    net helpmsg <errornumber>

    and you get the formatted error message.

Comments are closed.