Bagikan melalui


Menulis Fungsi Pelaporan Kesalahan Run-Time (C++)

Fungsi pelaporan kustom untuk kesalahan run-time harus memiliki deklarasi yang sama dengan _CrtDbgReportW. Ini harus mengembalikan nilai 1 ke penelusur kesalahan.

Contoh berikut menunjukkan cara menentukan fungsi pelaporan kustom.

Contoh 1

#include <stdio.h>
int errorhandler = 0;
void configureMyErrorFunc(int i)
{
    errorhandler = i;
}

int MyErrorFunc(int errorType, const wchar_t *filename,
                int linenumber, const wchar_t *moduleName,
                const wchar_t *format, ...)
{
    switch (errorhandler)
    {
    case 0:
    case 1:
        wprintf(L"Error type %d at %s line %d in %s",
                errorType, filename, linenumber, moduleName);
        break;
    case 2:
    case 3:
        fprintf(stderr, "Error type");
        break;
    }

    return 1;
}

Contoh 2

Contoh berikut menunjukkan fungsi pelaporan kustom yang lebih kompleks. Dalam contoh ini, pernyataan pengalih menangani berbagai jenis kesalahan, seperti reportType yang ditentukan oleh parameter _CrtDbgReportW. Karena Anda mengganti _CrtDbgReportW, Anda tidak dapat menggunakan _CrtSetReportMode. Fungsi Anda harus menghandel output. Argumen variabel pertama dalam fungsi ini mengambil nomor kesalahan run-time. Untuk informasi selengkapnya, lihat _RTC_SetErrorType.

#include <windows.h>
#include <stdarg.h>
#include <rtcapi.h>
#include <malloc.h>
#pragma runtime_checks("", off)
int Catch_RTC_Failure(int errType, const wchar_t *file, int line,
                      const wchar_t *module, const wchar_t *format, ...)
{
    // Prevent re-entrance.
    static long running = 0;
    while (InterlockedExchange(&running, 1))
        Sleep(0);
    // Now, disable all RTC failures.
    int numErrors = _RTC_NumErrors();
    int *errors=(int*)_alloca(numErrors);
    for (int i = 0; i < numErrors; i++)
        errors[i] = _RTC_SetErrorType((_RTC_ErrorNumber)i, _RTC_ERRTYPE_IGNORE);

    // First, get the rtc error number from the var-arg list.
    va_list vl;
    va_start(vl, format);
    _RTC_ErrorNumber rtc_errnum = va_arg(vl, _RTC_ErrorNumber);
    va_end(vl);

    wchar_t buf[512];
    const char *err = _RTC_GetErrDesc(rtc_errnum);
    swprintf_s(buf, 512, L"%S\nLine #%d\nFile:%s\nModule:%s",
        err,
        line,
        file ? file : L"Unknown",
        module ? module : L"Unknown");
    int res = (MessageBox(NULL, buf, L"RTC Failed...", MB_YESNO) == IDYES) ? 1 : 0;
    // Now, restore the RTC errortypes.
    for(int i = 0; i < numErrors; i++)
        _RTC_SetErrorType((_RTC_ErrorNumber)i, errors[i]);
    running = 0;
    return res;
}
#pragma runtime_checks("", restore)

Contoh 3

Gunakan _RTC_SetErrorFuncW untuk menginstal fungsi kustom Anda sebagai pengganti _CrtDbgReportW. Untuk informasi selengkapnya, lihat _RTC_SetErrorFuncW. Nilai _RTC_SetErrorFuncW yang dikembalikan adalah fungsi pelaporan sebelumnya, yang dapat Anda simpan dan pulihkan jika perlu.

#include <rtcapi.h>
int main()
{
    _RTC_error_fnW oldfunction, newfunc;
    oldfunction = _RTC_SetErrorFuncW(&MyErrorFunc);
    // Run some code.
    newfunc = _RTC_SetErrorFuncW(oldfunction);
    // newfunc == &MyErrorFunc;
    // Run some more code.
}