How to Get Message Text from Networking Error Codes

Last reviewed: October 21, 1996
Article ID: Q149409
The information in this article applies to:
  • Microsoft Win32 Software Development Kit (SDK) for Windows NT version 3.51

SUMMARY

In Windows NT, it is sometimes necessary to display error text associated with error return codes returned from networking related API functions. For example, this is needed with the family of functions that may return networking specific error codes in the Windows NT Net API set.

The error text for these messages is found in the message table file named Netmsg.dll, which is found in %systemroot%\system32. This file contains error messages in the range NERR_BASE (2100) through MAX_NERR (NERR_BASE+899). These error codes are defined in the Windows NT Lan Manager header file Lmerr.h.

MORE INFORMATION

The LoadLibrary() and LoadLibraryEx() Win32 API functions can be used to load Netmsg.dll. The FormatMessage() Win32 API can be used to map an error code to message text given a module handle to the Netmsg.dll file.

Sample Code

/*
    The following sample illustrates how to display error text
    associated with Networking related error codes, in addition
    to displaying error text associated with system related error
    codes.

    This sample relies on the following import library:
    User32.lib

*/

#include <windows.h>
#include <stdio.h>

#include <lmerr.h>

void DisplayErrorText(
    DWORD dwLastError
    );

int __cdecl main(
    void
    )
{
    //
    // display a networking related error string
    //
    printf("Network related error string follows:\n");
    DisplayErrorText(2226);

    //
    // display a system related error string
    //
    printf("\nSystem related error string follows:\n");
    SetLastError(ERROR_FILE_NOT_FOUND);
    DisplayErrorText(GetLastError());

    return 0;
}

void DisplayErrorText(

    DWORD dwLastError
    )
{
    HMODULE hModule = NULL; // default to system source
    LPSTR MessageBuffer;
    DWORD dwBufferLength;

    //
    // if dwLastError is in the network range, load the message source
    //
    if(dwLastError >= NERR_BASE && dwLastError <= MAX_NERR) {
        hModule = LoadLibraryEx(
            TEXT("netmsg.dll"),
            NULL,
            LOAD_LIBRARY_AS_DATAFILE
            );
    }

    //
    // call FormatMessage() to allow for message text to be acquired
    // from the system or the supplied module handle
    //
    if(dwBufferLength = FormatMessageA(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_IGNORE_INSERTS |
        FORMAT_MESSAGE_FROM_SYSTEM | // always consider system table
        ((hModule != NULL) ? FORMAT_MESSAGE_FROM_HMODULE : 0),
        hModule, // module to get message from (NULL == system)
        dwLastError,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
        (LPSTR) &MessageBuffer,
        0,
        NULL
        ))
    {
        DWORD dwBytesWritten;

        //
        // Output message string on stderr
        //
        WriteFile(
            GetStdHandle(STD_ERROR_HANDLE),
            MessageBuffer,
            dwBufferLength,
            &dwBytesWritten,
            NULL
            );

        //
        // free the buffer allocated by the system
        //
        LocalFree(MessageBuffer);
    }

    //
    // if you loaded a message source, unload it
    //
    if(hModule != NULL)
        FreeLibrary(hModule);
}


Additional reference words: error string LanMan
KBCategory: kbprg kbhowto kbcode
KBSubcategory: NtwkMisc BseMisc CodeSam



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 21, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.