1 ///////////////////////////////////////////////////////////////////////////////
3 // Module: CrashHandler.h
5 // Desc: CCrashHandler is the main class used by crashrpt to manage all
6 // of the details associated with handling the exception, generating
7 // the report, gathering client input, and sending the report.
9 // Copyright (c) 2003 Michael Carruth
11 ///////////////////////////////////////////////////////////////////////////////
15 #include "crashrpt.h" // defines LPGETLOGFILE callback
16 #include "excprpt.h" // bulk of crash report generation
21 typedef std::pair
<string
,string
> TStrStrPair
;
22 typedef std::vector
<TStrStrPair
> TStrStrVector
;
23 #endif // !defined TStrStrVector
26 extern BOOL g_bNoCrashHandler
;// don't use the crash handler but let the system handle it
28 ////////////////////////////// Class Definitions /////////////////////////////
30 // ===========================================================================
33 // See the module comment at top of file.
39 //-----------------------------------------------------------------------------
40 // GetInstance (static)
41 // Returns the instance for the current process. Creates one if necessary.
52 static CCrashHandler
* GetInstance();
55 //-----------------------------------------------------------------------------
57 // Installs the crash handler..
60 // lpfn Client crash callback
61 // lpcszTo Email address to send crash report
62 // lpczSubject Subject line to be used with email
68 // Passing NULL for lpTo will disable the email feature and cause the crash
69 // report to be saved to disk.
72 LPGETLOGFILE lpfn
= NULL
, // Client crash callback
73 LPCTSTR lpcszTo
= NULL
, // EMail:To
74 LPCTSTR lpcszSubject
= NULL
, // EMail:Subject
78 //-----------------------------------------------------------------------------
80 // Removes the crash handler..
93 //-----------------------------------------------------------------------------
95 // Enables the UI part
108 //-----------------------------------------------------------------------------
110 // Disables the UI part
123 //-----------------------------------------------------------------------------
125 // Disables the exception handler
136 void DisableHandler();
138 //-----------------------------------------------------------------------------
140 // Enables the custom exception handler
151 void EnableHandler();
153 //-----------------------------------------------------------------------------
155 // Uninitializes the crashrpt library.
169 //-----------------------------------------------------------------------------
171 // Adds a file to the crash report.
174 // lpFile Fully qualified file name
175 // lpDesc File description
181 // Call this function to include application specific file(s) in the crash
182 // report. For example, application logs, initialization files, etc.
186 LPCTSTR lpFile
, // File nae
187 LPCTSTR lpDesc
// File description
190 //-----------------------------------------------------------------------------
192 // Removes a file from the crash report.
195 // lpFile Fully qualified file name
201 // lpFile must exactly match that passed to AddFile.
205 LPCTSTR lpFile
// File nae
208 //-----------------------------------------------------------------------------
210 // Adds a registry hive to the crash report.
213 // lpKey Fully registry eky
214 // lpDesc Description
220 // Call this function to include application specific registry hive(s) in the crash
225 LPCTSTR lpKey
, // Registry key
226 LPCTSTR lpDesc
// description
229 //-----------------------------------------------------------------------------
230 // RemoveRegistryHive
231 // Removes a registry hive from the crash report.
234 // lpKey Full registry key
240 // lpKey must exactly match that passed to AddRegistryHive.
244 LPCTSTR lpKey
// Registry key
247 //-----------------------------------------------------------------------------
249 // Adds an event log to the crash report.
252 // lpKey Event log name ("Application", "System", "Security")
253 // lpDesc Description
259 // Call this function to include application specific registry hive(s) in the crash
264 LPCTSTR lpKey
, // Event log name
265 LPCTSTR lpDesc
// description
268 //-----------------------------------------------------------------------------
270 // Removes an event log from the crash report.
273 // lpKey Event log name
279 // lpKey must exactly match that passed to AddEventLog.
283 LPCTSTR lpKey
// Registry key
286 //-----------------------------------------------------------------------------
287 // GenerateErrorReport
288 // Produces a crash report.
291 // pExInfo Pointer to an EXCEPTION_POINTERS structure
294 // BOOL TRUE if exception to be executed; FALSE
295 // if to search for another handler. This
296 // should be used to allow breaking into
297 // the debugger, where appropriate.
300 // Call this function to manually generate a crash report.
304 PEXCEPTION_POINTERS pExInfo
, // Exception pointers (see MSDN)
311 //-----------------------------------------------------------------------------
313 // Initializes the library and optionally set the client crash callback and
314 // sets up the email details.
323 // Passing NULL for lpTo will disable the email feature and cause the crash
324 // report to be saved to disk.
329 //-----------------------------------------------------------------------------
331 // Presents the user with a file save dialog and saves the crash report
332 // file to disk. This function is called if an Email:To was not provided
333 // in the constructor.
336 // rpt The report details
337 // lpcszFile The zipped crash report
340 // True is successful.
347 CExceptionReport
&rpt
,
351 //-----------------------------------------------------------------------------
353 // Mails the zipped crash report to the address specified.
356 // rpt The report details
357 // lpcszFile The zipped crash report
358 // lpcszEmail The Email:To
362 // TRUE is successful.
365 // MAPI is used to send the report.
369 CExceptionReport
&rpt
,
375 //-----------------------------------------------------------------------------
376 // DialogThreadExecute
377 // Displays the dialog and handles the user's reply. Executed as a separate
381 // pParam Standard CreateThreadParameter; set to pointer to CCrashHandler
387 // Started from GenerateErrorReport via CreateThread. This ensures the caller
388 // is stopped (and will not confuse state by dispatching messages).
390 static DWORD WINAPI
CCrashHandler::DialogThreadExecute(LPVOID pParam
);
392 string
LoadResourceString(UINT id
);
393 LPTOP_LEVEL_EXCEPTION_FILTER m_oldFilter
; // previous exception filter
394 LPGETLOGFILE m_lpfnCallback
; // client crash callback
395 int m_pid
; // process id
396 TStrStrVector m_files
; // custom files to add
397 TStrStrVector m_registryHives
; // custom registry hives to save
398 TStrStrVector m_eventLogs
; // custom event logs to save
399 string m_sTo
; // Email:To
400 string m_sSubject
; // Email:Subject
401 HANDLE m_ipc_event
; // Event for dialog thread synchronization
402 CExceptionReport
*m_rpt
; // Exception report for dialog
403 bool m_installed
; // True if already installed
404 HMODULE m_hModule
; // Module handle for loading resource strings
405 string m_userDataFile
; // file to save user input when m_sTo is empty
406 bool m_wantDebug
; // user pushed Debug button
407 BOOL m_bUseUI
; // use an UI or print to the error output