1 ///////////////////////////////////////////////////////////////////////////////
3 // Module: CrashRptDL.h
5 // Desc: Defines the interface for the CrashRpt.DLL, using a dynamically
6 // loaded instance of the library. Can be used when CrashRpt.dll may
7 // not be present on the system.
9 // Note that all functions except GetInstanceDL() and
10 // ReleaseInstanceDL() return an integer, which is non-zero ('true')
11 // when the real DLL function was located and called. This does not
12 // mean the actual DLL function succeeded, however.
14 // Please don't get confused by the macro usage; all functions do end
15 // in DL, even though the name supplied to CRASHRPT_DECLARE does not.
17 // Copyright (c) 2003 Michael Carruth
18 // Copyright (c) 2003 Grant McDorman
19 // This software is provided 'as-is', without any express or implied
20 // warranty. In no event will the authors be held liable for any damages
21 // arising from the use of this software.
23 // Permission is granted to anyone to use this software for any purpose,
24 // including commercial applications, and to alter it and redistribute it
25 // freely, subject to the following restrictions:
27 // 1. The origin of this software must not be misrepresented; you must not
28 // claim that you wrote the original software. If you use this software
29 // in a product, an acknowledgment in the product documentation would be
30 // appreciated but is not required.
31 // 2. Altered source versions must be plainly marked as such, and must not be
32 // misrepresented as being the original software.
33 // 3. This notice may not be removed or altered from any source distribution.
35 ///////////////////////////////////////////////////////////////////////////////
42 #endif // _MSC_VER >= 1000
45 #include <wtypes.h> // BSTR
47 // Client crash callback
48 typedef BOOL (CALLBACK
*LPGETLOGFILE
) (LPVOID lpvState
);
49 // Stack trace callback
50 typedef void (*TraceCallbackFunction
)(DWORD address
, const char *ImageName
,
51 const char *FunctionName
, DWORD functionDisp
,
52 const char *Filename
, DWORD LineNumber
, DWORD lineDisp
,
55 // macro to create the inline forwarding function
56 #define CRASHRPT_DECLARE(function, declare1, declare2, arguments) \
57 __inline int function##DL declare1 \
59 typedef void (*function##_t) declare2; \
60 function##_t p##function; \
61 p##function = (function##_t) GetProcAddress(hModule, #function); \
62 if (p##function != NULL) { \
63 p##function arguments; \
71 //-----------------------------------------------------------------------------
73 // Returns the instance (module handle) for the CrashRpt DLL.
79 // If the function succeeds, the return value is a module handle for the CrashRpt
80 // shared library (CrashRpt.dll). This handle is required for all the other functions.
89 return LoadLibrary("CrashRpt");
92 //-----------------------------------------------------------------------------
94 // Initializes the library and optionally set the client crash callback and
95 // sets up the email details.
98 // hModule State information returned from GetInstanceDL() (must not be NULL)
99 // pfn Client crash callback
100 // lpTo Email address to send crash report
101 // lpSubject Subject line to be used with email
104 // non-zero if successful
107 // Passing NULL for lpTo will disable the email feature and cause the crash
108 // report to be saved to disk.
110 CRASHRPT_DECLARE(Install
, (IN HMODULE hModule
, IN LPGETLOGFILE pfn
, IN LPCTSTR lpTo OPTIONAL
, IN LPCTSTR lpSubject OPTIONAL
), \
111 (LPGETLOGFILE pfn
, LPCTSTR lpTo
, LPCTSTR lpSubject
), \
112 (pfn
, lpTo
, lpSubject
))
114 //-----------------------------------------------------------------------------
116 // Uninstalls the unhandled exception filter set up in InstallDL().
119 // hModule Module handle returned from GetInstanceDL()
122 // non-zero if successful
125 // This call is optional. The crash report library will automatically
126 // deinitialize when the library is unloaded. Call this function to
127 // unhook the exception filter manually.
129 CRASHRPT_DECLARE(Uninstall
, (IN HMODULE hModule
), \
132 //-----------------------------------------------------------------------------
134 // Releases the library.
137 // hModule Module handle returned from GetInstanceDL()
143 // This will call UninstallDL before releasing the library.
147 ReleaseInstanceDL(IN HMODULE hModule
)
149 UninstallDL(hModule
);
150 FreeLibrary(hModule
);
153 //-----------------------------------------------------------------------------
155 // Adds a file to the crash report.
158 // hModule Module handle returned from GetInstanceDL()
159 // lpFile Fully qualified file name
160 // lpDesc Description of file, used by details dialog
163 // non-zero if successful
166 // This function can be called anytime after Install() to add one or more
167 // files to the generated crash report. If lpFile exactly matches
168 // a previously added file, it is not added again.
170 CRASHRPT_DECLARE(AddFile
, (IN HMODULE hModule
, IN LPCTSTR lpFile
, IN LPCTSTR lpDesc
), \
171 (LPCTSTR lpFile
, LPCTSTR lpDesc
), \
174 //-----------------------------------------------------------------------------
176 // Removes a file from the crash report.
179 // hModule Module handle returned from GetInstanceDL()
180 // lpFile Fully qualified file name
183 // non-zero if successful
186 // The filename must exactly match that provided to AddFile.
188 CRASHRPT_DECLARE(RemoveFile
, (IN HMODULE hModule
, IN LPCTSTR lpFile
), \
192 //-----------------------------------------------------------------------------
194 // Adds a RegistryHive to the crash report.
197 // hModule Module handle returned from GetInstanceDL()
198 // lpRegistryHive Fully qualified RegistryHive name
199 // lpDesc Description of RegistryHive, used by details dialog
202 // non-zero if successful
205 // This function can be called anytime after Install() to add one or more
206 // RegistryHives to the generated crash report. If lpRegistryHive exactly matches
207 // a previously added file, it is not added again.
209 CRASHRPT_DECLARE(AddRegistryHive
, (IN HMODULE hModule
, IN LPCTSTR lpRegistryHive
, IN LPCTSTR lpDesc
), \
210 (LPCTSTR lpRegistryHive
, LPCTSTR lpDesc
), \
211 (lpRegistryHive
, lpDesc
))
213 //-----------------------------------------------------------------------------
214 // RemoveRegistryHiveDL
215 // Removes a RegistryHive from the crash report.
218 // hModule Module handle returned from GetInstanceDL()
219 // lpRegistryHive Fully qualified RegistryHive name
222 // non-zero if successful
225 // The RegistryHive name must exactly match that provided to AddRegistryHive.
227 CRASHRPT_DECLARE(RemoveRegistryHive
, (IN HMODULE hModule
, IN LPCTSTR lpRegistryHive
), \
228 (LPCTSTR lpRegistryHive
), \
231 //-----------------------------------------------------------------------------
233 // Adds an event log to the crash report.
236 // hModule Module handle returned from GetInstanceDL()
237 // lpEventLog Event log name ("Application", "Security", "System" or any other known to your system)
238 // lpDesc Description of event log, used by details dialog
241 // non-zero if successful
244 // This function can be called anytime after Install() to add one or more
245 // event logs to the generated crash report. If lpEventLog exactly matches
246 // a previously added file, it is not added again.
248 CRASHRPT_DECLARE(AddEventLog
, (IN HMODULE hModule
, IN LPCTSTR lpEventLog
, IN LPCTSTR lpDesc
), \
249 (LPCTSTR lpEventLog
, LPCTSTR lpDesc
), \
250 (lpEventLog
, lpDesc
))
252 //-----------------------------------------------------------------------------
254 // Removes a EventLog from the crash report.
257 // hModule Module handle returned from GetInstanceDL()
258 // lpEventLog Fully qualified EventLog name
261 // non-zero if successful
264 // The EventLog name must exactly match that provided to AddEventLog.
266 CRASHRPT_DECLARE(RemoveEventLog
, (IN HMODULE hModule
, IN LPCTSTR lpEventLog
), \
267 (LPCTSTR lpEventLog
), \
270 //-----------------------------------------------------------------------------
271 // GenerateErrorReportDL
272 // Generates the crash report.
275 // hModule Module handle returned from GetInstanceDL()
276 // pExInfo Optional; exception information
277 // message Optional; message to include in report
280 // non-zero if successful
283 // Call this function to manually generate a crash report.
287 GenerateErrorReportDL(
289 IN PEXCEPTION_POINTERS pExInfo OPTIONAL
,
290 IN BSTR message OPTIONAL
293 // we can't use GenerateErrorReport(), because that is lacking the PEXCEPTION_POINTERS parameter
295 typedef LPVOID (*GetInstance_t
) ();
296 GetInstance_t pGetInstance
;
297 pGetInstance
= (GetInstance_t
) GetProcAddress(hModule
, "GetInstance");
298 if (pGetInstance
!= NULL
) {
299 typedef void (*GenerateErrorReportEx_t
)(LPVOID lpState
, PEXCEPTION_POINTERS pExInfo
, BSTR message
);
300 GenerateErrorReportEx_t pGenerateErrorReportEx
;
302 instance
= pGetInstance();
303 pGenerateErrorReportEx
= (GenerateErrorReportEx_t
) GetProcAddress(hModule
, "GenerateErrorReportEx");
304 if (pGenerateErrorReportEx
!= NULL
) {
305 pGenerateErrorReportEx(instance
, pExInfo
, message
);
313 //-----------------------------------------------------------------------------
315 // Creates a stack trace.
318 // hModule Module handle returned from GetInstanceDL()
319 // numSkip Number of initial stack frames to skip
320 // depth Number of stack frames to process
321 // pFunction Optional; function to call for each frame
322 // pContext Optional; stack context to trace
328 // Call this function to manually generate a stack trace. If
329 // pFunction is not supplied, stack trace frames are output using
330 // OutputDebugString. Can be called without installing (InstallDL)
333 CRASHRPT_DECLARE(StackTrace
, (IN HMODULE hModule
, IN
int numSkip
, IN
int depth
, IN TraceCallbackFunction pFunction OPTIONAL
, IN CONTEXT
* pContext OPTIONAL
, IN LPVOID data OPTIONAL
), \
334 (int numSkip
, int depth
, TraceCallbackFunction pFunction
, CONTEXT
* pContext
, LPVOID data
), \
335 (numSkip
, depth
, pFunction
, pContext
, data
))
337 #undef CRASHRPT_DECLARE