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