다음을 통해 공유


오류 메시지 검색

메서드 호출에서 오류가 발생하면 많은 함수가 오류 코드를 반환합니다. 오류 코드를 반환하는 대부분의 Certificate Services 인터페이스 및 API 요소의 경우 NULL 모듈 핸들을 사용하여 FormatMessage를 호출하여 오류 메시지 텍스트를 검색할 수 있습니다. FormatMessage가 성공하지 못하면 백업 API 요소 또는 데이터베이스 관련 오류로 인해 오류 코드가 발생할 가능성이 큽니다. Ntdsbmsg.dll 라이브러리에 해당하는 모듈 핸들을 사용하여 FormatMessage를 호출하면 오류 메시지 텍스트가 검색됩니다. 다음 예제에서는 Certificate Services 애플리케이션에서 오류 메시지 텍스트를 검색하는 방법을 보여 줍니다.

#include <windows.h>
#include <stdio.h>
// Display error message text, given an error code.
// Typically, the parameter passed to this function is retrieved
// from GetLastError().
void PrintCSBackupAPIErrorMessage(DWORD dwErr)
{

    WCHAR   wszMsgBuff[512];  // Buffer for text.

    DWORD   dwChars;  // Number of chars returned.

    // Try to get the message from the system errors.
    dwChars = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |
                             FORMAT_MESSAGE_IGNORE_INSERTS,
                             NULL,
                             dwErr,
                             0,
                             wszMsgBuff,
                             512,
                             NULL );

    if (0 == dwChars)
    {
        // The error code did not exist in the system errors.
        // Try Ntdsbmsg.dll for the error code.

        HINSTANCE hInst;

        // Load the library.
        hInst = LoadLibrary(L"Ntdsbmsg.dll");
        if ( NULL == hInst )
        {
            printf("cannot load Ntdsbmsg.dll\n");
            exit(1);  // Could 'return' instead of 'exit'.
        }

        // Try getting message text from ntdsbmsg.
        dwChars = FormatMessage( FORMAT_MESSAGE_FROM_HMODULE |
                                 FORMAT_MESSAGE_IGNORE_INSERTS,
                                 hInst,
                                 dwErr,
                                 0,
                                 wszMsgBuff,
                                 512,
                                 NULL );

        // Free the library.
        FreeLibrary( hInst );

    }

    // Display the error message, or generic text if not found.
    printf("Error value: %d Message: %ws\n",
            dwErr,
            dwChars ? wszMsgBuff : L"Error message not found." );

}