_CrtSetReportHook2, _CrtSetReportHookW2

Installa o disinstalla la funzione per la creazione di report definita dal client per eseguire l'hook nel processo di creazione di report sul debug del runtime di C (solo versione di debug).

Sintassi

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

Parametri

mode
Azione da eseguire: _CRT_RPTHOOK_INSTALL o _CRT_RPTHOOK_REMOVE.

pfnNewHook
Hook del report per installare o rimuovere nella versione a caratteri stretti o wide di questa funzione.

Valore restituito

-1 se si è verificato un errore, con l'impostazione di EINVAL o ENOMEM; in caso contrario restituisce il conteggio dei riferimenti di pfnNewHook dopo la chiamata.

Osservazioni:

_CrtSetReportHook2 e _CrtSetReportHookW2 consente di associare o scollegare una funzione, mentre _CrtSetReportHook consente solo di associare una funzione.

_CrtSetReportHook2 o _CrtSetReportHookW2 deve essere usata al posto di _CrtSetReportHook quando la chiamata all'hook viene fatta in una DLL e quando più DLL vengono caricate e impostano le loro funzioni di hook. In questo caso, le DLL possono essere scaricate in un ordine diverso rispetto all'ordine di caricamento e la funzione di hook può sempre puntare a una DLL scaricata. Ogni output di debug arresta il processo se le funzioni di hook sono state aggiunte con _CrtSetReportHook.

Ogni funzione di hook aggiunta con _CrtSetReportHook viene chiamata se non ci sono funzioni di hook aggiunte con _CrtSetReportHook2 o _CrtSetReportHookW2 oppure se tutte le funzioni di hook aggiunte con _CrtSetReportHook2 e _CrtSetReportHookW2 restituiscono FALSE.

La versione a caratteri wide di questa funzione è disponibile. Le funzioni di hook per i report accettano una stringa il cui tipo (caratteri wide o narrow) deve corrispondere alla versione di questa funzione usata. Usare il prototipo seguente di funzione di hook per i report usati con la versione a caratteri wide di questa funzione:

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

Usare il prototipo seguente per gli hook di report a caratteri narrow:

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

Queste funzioni convalidano i relativi parametri. Se mode o pfnNewHook non è valido, queste funzioni richiamano il gestore di parametri non validi, come descritto in Convalida dei parametri. Se l'esecuzione può continuare, queste funzioni impostano errno su EINVAL e restituiscono -1.

Nota

Se l'applicazione viene compilata con /clr e la funzione di creazione di report viene chiamata dopo la chiusura dell'applicazione main, CLR genererà un'eccezione se la funzione di creazione di report chiama funzioni CRT.

Requisiti

Ciclo Intestazione obbligatoria Intestazione facoltativa
_CrtSetReportHook2 <crtdbg.h> <errno.h>
_CrtSetReportHookW2 <crtdbg.h> <errno.h>

Per altre informazioni sulla compatibilità, vedere Compatibility (Compatibilità).

Librerie

Solo le versioni di debug delle librerie di runtime di C.

Esempio

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

Vedi anche

Routine di debug