|
crashrpt
|
The following example shows how to use CrashRpt API functions and structures to enable crash reporting support in a console C++ application. We use console application for simplicity, but in general the application may be WinAPI/MFC/ATL/WTL based, as well. For additional information on CrashRpt API functions and structures, please refer to Using CrashRpt API.
First create a console Win32 application and call it MyApp. Then configure the MyApp application as described in Configuring Your Project's Build Settings.
Let's assume the case when MyApp application has two threads. The first thread, the application thread, will be the main one. The main() function will be executed in this thread and interaction with user will also be performed in this thread. The second thread, the worker thread, is typically used when some time-consuming computational work is to be done without blocking the application thread.
The MyApp application will create a log file that will be included in error report on crash. The log file is typically helpful when debugging a crash.
Let's create the code template.
We intentionally inserted the code that would cause an exception in both threads. In real-life programs such a code always exists, even when you test your application very carefully.
To enable crash reporting support in the application, we need to include CrashRpt header in the beginning of the code:
Next, we define the crash callback function and call it CrashCallback(). The crash callback function will be called by CrashRpt when crash occurs, so we will be able to close the handle to log file. For more information on crash callback, see the the PFNCRASHCALLBACK() prototype.
Because we have a multi-threaded application, we need to use some CrashRpt Functions to set exception handlers for the worker thread. In this example, we use the crInstallToCurrentThread2() and crUninstallFromCurrentThread() functions to set exception handlers in the beginning of the thread procedure and unset them in the end of the thread procedure, respectively.
Next, in the beginning of the main() function, we initialize the CrashRpt library and install the exception handlers for the entire process with the help of crInstall() function and pass configuration information to it through the CR_INSTALL_INFO structure. If something goes wrong, we can check the last error message with the crGetLastErrorMsg() function.
Next, we want to set the crash callback function by calling crSetCrashCallback().
Next, we want to add our error log file to the crash report. We do this with the crAddFile2() function.
When the app crashes, we can include the screen shot to the crash report. We do this with the crAddScreenshot2() function.
The following code adds a named property to the crash description XML file (see the crAddProperty() function). The property tells what graphics adapter is installed on end user's computer (for simplicity, it is hardcoded, but you usually determine adapter's model dynamically using Windows Management Instrumentation, shortly WMI).
In the end of the main() function, we uninitialize CrashRpt and unset the exception handlers using the crUninstall() function.
Below we have it all in one piece of code:
Do not forget to add CrashRptXXXX.lib file to the list of input libraries of your project (XXXX is the placeholder for the version of CrashRpt). For additional info, see Configuring Your Project's Build Settings.
Before running the application, you should place the following files to the directory where your application executable file is located (for additional information, see Preparing to Software Release):
When files have been copied, run the application. As error occurs, you should be able to see an error report window (for additional information, see About an Error Report)
Further reading: Internationalization Support.
1.8.12