다음을 통해 공유


오류 코드 번호에 대한 텍스트 조회

네트워킹 관련 함수에서 반환된 오류 코드와 관련된 오류 텍스트를 표시해야 하는 경우도 있습니다. 시스템에서 제공하는 네트워크 관리 기능을 사용하여 이 작업을 수행해야 할 수 있습니다.

이러한 메시지에 대한 오류 텍스트는 %systemroot%\system32에 있는 Netmsg.dll 라는 메시지 테이블 파일에 있습니다. 이 파일에는 MAX_NERR(NERR_BASE+899)NERR_BASE(2100) 범위의 오류 메시지가 포함되어 있습니다. 이러한 오류 코드는 SDK 헤더 파일 lmerr.h에 정의되어 있습니다.

LoadLibraryLoadLibraryEx 함수는 Netmsg.dll 로드할 수 있습니다. FormatMessage 함수는 모듈 핸들을 Netmsg.dll 파일에 지정하여 오류 코드를 메시지 텍스트에 매핑합니다.

다음 샘플에서는 시스템 관련 오류 코드와 관련된 오류 텍스트를 표시하는 것 외에도 네트워크 관리 함수와 관련된 오류 텍스트를 표시하는 방법을 보여 줍니다. 제공된 오류 번호가 특정 범위에 있으면 netmsg.dll 메시지 모듈이 로드되고 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);
}

이 프로그램을 컴파일한 후 오류 코드 번호를 인수로 삽입하면 프로그램에 텍스트가 표시됩니다. 예:

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