Delen via


Tekst opzoeken voor foutcodenummers

Soms is het nodig om fouttekst weer te geven die is gekoppeld aan foutcodes die zijn geretourneerd door netwerkfuncties. Mogelijk moet u deze taak uitvoeren met de netwerkbeheerfuncties van het systeem.

De fouttekst voor deze berichten is te vinden in het berichttabelbestand met de naam Netmsg.dll, die wordt gevonden in %systemroot%\system32. Dit bestand bevat foutberichten in het bereik NERR_BASE (2100) tot en met MAX_NERR(NERR_BASE+899). Deze foutcodes worden gedefinieerd in het SDK-headerbestand lmerr.h.

De functies LoadLibrary en LoadLibraryEx kunnen Netmsg.dllladen. De functie FormatMessage koppelt een foutcode aan berichttekst, gegeven een modulegreep voor het Netmsg.dll bestand.

In het volgende voorbeeld ziet u hoe u fouttekst weergeeft die is gekoppeld aan netwerkbeheerfuncties, naast het weergeven van fouttekst die is gekoppeld aan systeemgerelateerde foutcodes. Als het opgegeven foutnummer zich in een specifiek bereik bevindt, wordt de netmsg.dll berichtmodule geladen en gebruikt om het opgegeven foutnummer op te zoeken met de functie FormatMessage.

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

#include <lmerr.h>

void
DisplayErrorText(
    DWORD dwLastError
    );

#define RTN_OK 0
#define RTN_USAGE 1
#define RTN_ERROR 13

int
__cdecl
main(
    int argc,
    char *argv[]
    )
{
    if(argc != 2) {
        fprintf(stderr,"Usage: %s <error number>\n", argv[0]);
        return RTN_USAGE;
    }

    DisplayErrorText( atoi(argv[1]) );

    return RTN_OK;
}

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

    DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_IGNORE_INSERTS |
        FORMAT_MESSAGE_FROM_SYSTEM ;

    //
    // 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
            );

        if(hModule != NULL)
            dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;
    }

    //
    // Call FormatMessage() to allow for message 
    //  text to be acquired from the system 
    //  or from the supplied module handle.
    //

    if(dwBufferLength = FormatMessageA(
        dwFormatFlags,
        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 we loaded a message source, unload it.
    //
    if(hModule != NULL)
        FreeLibrary(hModule);
}

Nadat u dit programma hebt gecompileerd, kunt u het foutcodenummer als argument invoegen en wordt de tekst weergegeven in het programma. Bijvoorbeeld:

C:\> netmsg 2453
Could not find domain controller for this domain