HOWTO: Get Message Text from Networking Error Codes

ID: Q149409


The information in this article applies to:


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 query words: error string LanMan


Keywords          : kbnetwork kbAPI kbNTOS351 kbSDKPlatform kbCodeSam kbGrpNet 
Version           : 
Platform          : 
Issue type        : kbhowto 

Last Reviewed: March 11, 1999