_CrtSetReportHook2, _CrtSetReportHookW2

通过将它安装或卸载一个客户端定义的报告功能添加到 C 运行时调试报告进程 (请只调试版本)。

int _CrtSetReportHook2(
   int mode,
   _CRT_REPORT_HOOK pfnNewHook
);
int _CrtSetReportHookW2(
   int mode,
   _CRT_REPORT_HOOKW pfnNewHook
);

参数

  • mode
    采取的操作: _CRT_RPTHOOK_INSTALL_CRT_RPTHOOK_REMOVE

  • pfnNewHook
    报告挂钩安装或移除此函数的输出字符版本。

  • pfnNewHook
    报告挂钩安装或移除此函数的宽字符版本。

返回值

-1,如果遇到错误,与 EINVALENOMEM 设置;否则在调用后返回引用计数 pfnNewHook 。

备注

_CrtSetReportHook2_CrtSetReportHookW2可以挂钩或取消功能, _CrtSetReportHook ,而只可以挂钩函数。

应使用**_CrtSetReportHook2** 或 _CrtSetReportHookW2而不是 _CrtSetReportHook ,当挂钩在 DLL 时调用,所以,当多个 DLL 可能是加载并设置它们的挂钩函数。 在这种情况下, DLL 比其加载了可能以不同顺序卸载,并且挂钩函数可保留点在卸载的 DLL。 ,如果挂钩函数增加了 _CrtSetReportHook,任何调试输出故障进程。

所有挂钩函数添加与 _CrtSetReportHook 调用,如果没有挂钩函数添加与 _CrtSetReportHook2_CrtSetReportHookW2或者,如果所有挂钩函数添加与 _CrtSetReportHook2_CrtSetReportHookW2返回 FALSE

此函数的宽字符版本可用。 报告挂钩函数采用类型的字符串 (宽或收缩字符) 必须与使用的此功能的版本。 用于报告挂钩使用以下函数原型使用此功能的宽字符版本:

int YourReportHook( int reportType, wchar_t *message, int *returnValue );

对于窄字符报告挂钩使用以下原型:

int YourReportHook( int reportType, char *message, int *returnValue );

这些功能验证其参数。 如果 mode 或 pfnNewNook 无效,这些函数调用的参数无效处理程序,如 参数验证所述。 如果执行允许继续,对 EINVAL 的这些功能集 errno 并且返回 -1。

备注

如果应用程序编译 /clr ,并报告函数时,在应用程序退出主要后, CLR 将引发异常,如果报告函数调用任何 CRT 函数。

要求

实例

必需的头

可选标头

_CrtSetReportHook2

crtdbg.h

errno.h

_CrtSetReportHookW2

crtdbg.h

errno.h

有关更多兼容性信息,请参见中介绍的 兼容性

只调试 C 运行库 的版本。

示例

// crt_setreporthook2.c
#include <windows.h>
#include <stdio.h>
#include <crtdbg.h>
#include <assert.h>

int __cdecl TestHook1(int nReportType, char* szMsg, int* pnRet)
{
   int nRet = FALSE;

   printf("CRT report hook 1.\n");
   printf("CRT report type is \"");
   switch (nReportType)
   {
      case _CRT_ASSERT:
      {
         printf("_CRT_ASSERT");
         // nRet = TRUE;   // Always stop for this type of report
         break;
      }

      case _CRT_WARN:
      {
         printf("_CRT_WARN");
         break;
      }

      case _CRT_ERROR:
      {
         printf("_CRT_ERROR");
         break;
      }

      default:
      {
         printf("???Unknown???");
         break;
      }
   }

   printf("\".\nCRT report message is:\n\t");
   printf(szMsg);

   if   (pnRet)
      *pnRet = 0;

   return   nRet;
}

int __cdecl   TestHook2(int nReportType, char* szMsg, int* pnRet)
{
   int   nRet = FALSE;

   printf("CRT report hook 2.\n");
   printf("CRT report type is \"");
   switch   (nReportType)
   {
      case _CRT_WARN:
      {
         printf("_CRT_WARN");
         break;
      }

      case _CRT_ERROR:
      {
         printf("_CRT_ERROR");
         break;
      }

      case _CRT_ASSERT:
      {
         printf("_CRT_ASSERT");
         nRet = TRUE;   // Always stop for this type of report
         break;
      }

      default:
      {
         printf("???Unknown???");
         break;
      }
   }

   printf("\".\nCRT report message is: \t");
   printf(szMsg);

   if   (pnRet)
      *pnRet = 0;
   // printf("CRT report code is %d.\n", *pnRet);
   return   nRet;
}

int   main(int argc, char* argv[])
{
   int   nRet = 0;

   nRet = _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook1);
   printf("_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook1)"
          " returned %d\n", nRet);
   nRet = _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook2);
   printf("_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook2)"
          " returned %d\n", nRet);
   nRet = _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook2);
   printf("_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook2)"
          " returned %d\n", nRet);
   nRet = _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook1);
   printf("_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook1)"
          " returned %d\n", nRet);
   nRet = _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook1);
   printf("_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook1)"
          " returned %d\n", nRet);

   _ASSERT(0);

   nRet = _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook2);
   printf("_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook2)"
          " returned %d\n", nRet);
   nRet = _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook2);
   printf("_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook2)"
          " returned %d\n", nRet);
   nRet = _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook2);
   printf("_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook2)"
          " returned %d\n", nRet);
   nRet = _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook1);
   printf("_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook1)"
          " returned %d\n", nRet);

   return   nRet;
}

Output

_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook1) returned 0
_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook2) returned 0
_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook2) returned 0
_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook1) returned 0
_CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, TestHook1) returned 0
_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook2) returned 0
_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook2) returned 0
_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook2) returned 0
_CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, TestHook1) returned 0

请参见

参考

调试实例