Generates a report with a debugging message and sends the report to three possible destinations (debug version only).
int _CrtDbgReport( int reportType, const char *filename, int linenumber, const char *moduleName, const char *format [, argument] ... ); int _CrtDbgReportW( int reportType, const wchar_t *filename, int linenumber, const wchar_t *moduleName, const wchar_t *format [, argument] ... );
For all report destinations, _CrtDbgReport and _CrtDbgReportW return –1 if an error occurs and 0 if no errors are encountered. However, when the report destination is a debug message window and the user clicks the Retry button, these functions return 1. If the user clicks the Abort button in the Debug Message window, these functions immediately abort and do not return a value.
The _RPT, _RPTF debug macros call _CrtDbgReport to generate their debug reports. The wide-character versions of these macros as well as _ASSERT[E], _RPTWn and _RPTFWn, use _CrtDbgReportW to generate their debug reports. When _CrtDbgReport or _CrtDbgReportW return 1, these macros start the debugger, provided that just-in-time (JIT) debugging is enabled.
_CrtDbgReport and _CrtDbgReportW can send the debug report to three different destinations: a debug report file, a debug monitor (the Visual Studio debugger), or a debug message window. Two configuration functions, _CrtSetReportMode and _CrtSetReportFile, are used to specify the destination or destinations for each report type. These functions allow the reporting destination or destinations for each report type to be separately controlled. For example, it is possible to specify that a reportType of _CRT_WARN only be sent to the debug monitor, while a reportType of _CRT_ASSERT be sent to a debug message window and a user-defined report file.
In Visual C++ 2005, _CrtDbgReportW is the wide-character version of _CrtDbgReport. All its output and string parameters are in wide-character strings; otherwise it is identical to the single-byte character version.
_CrtDbgReport and _CrtDbgReportW create the user message for the debug report by substituting the argument[n] arguments into the format string, using the same rules defined by the printf or wprintf functions. These functions then generate the debug report and determine the destination or destinations, based on the current report modes and file defined for reportType. When the report is sent to a debug message window, the filename, lineNumber, and moduleName are included in the information displayed in the window.
The following table lists the available choices for the report mode or modes and file and the resulting behavior of _CrtDbgReport and _CrtDbgReportW. These options are defined as bit flags in Crtdbg.h.
_CrtDbgReport, _CrtDbgReportW behavior
Writes message to Windows OutputDebugString API.
Calls Windows MessageBox API to create message box to display the message along with Abort, Retry, and Ignore buttons. If a user clicks Abort, _CrtDbgReport or _CrtDbgReport immediately aborts. If a user clicks Retry, it returns 1. If a user clicks Ignore, execution continues and _CrtDbgReport and _CrtDbgReportW return 0. Note that clicking Ignore when an error condition exists often results in "undefined behavior."
Writes message to user-supplied HANDLE, using the Windows WriteFile API and does not verify validity of file handle; the application is responsible for opening the report file and passing a valid file handle.
Writes message to stderr.
Writes message to stdout.
The report can be sent to one, two, or three destinations or to no destination at all. For more information about specifying the report mode or modes and report file, see the _CrtSetReportMode and _CrtSetReportFile functions. For more information about using the debug macros and reporting functions, see Using Macros for Verification and Reporting.
If your application needs more flexibility than that provided by _CrtDbgReport and _CrtDbgReportW, you can write your own reporting function and hook it into the C run-time library reporting mechanism by using the _CrtSetReportHook function.