Original: C + + class that automatically records minidump when a program crashes
Encapsulates a C + + class that can generate a dump file when the program crashes unexpectedly to determine the cause of the error.
Header file:
//crash_dumper_w32.h#ifndef _crash_dumper_h_#define_crash_dumper_h_#include<windows.h>classcrashdumper{ Public: Crashdumper (); ~Crashdumper (); Static BOOL_placeholder ();Private: Lptop_level_exception_filter m_originalfilter; StaticLONG WINAPI Exceptionfilter (struct_exception_pointers*exceptioninfo);};namespace{ Const BOOLBplaceholder =Crashdumper::_placeholder ();}#endif
Implementation file:
crash_dumper_w32.cpp #include<windows.h>#include<tchar.h>#include<dbghelp.h>#include<string>#include"crash_dumper_w32.h"#ifdef UNICODE # define tstring wstring#else # define Tstringstring #endif #pragmaComment (lib, "Dbghelp.lib")Crashdumper dumper; Crashdumper::crashdumper () {M_originalfilter=SetUnhandledExceptionFilter (exceptionfilter);} Crashdumper::~Crashdumper () {setunhandledexceptionfilter (m_originalfilter);} LONG WINAPI Crashdumper::exceptionfilter (struct_exception_pointers*exceptioninfo) { BOOLBdumpok =false; DWORD dwprocess=GetCurrentProcessId (); HANDLE hprocess=openprocess (process_all_access, FALSE, dwprocess); if(Hprocess! =Invalid_handle_value) {TCHAR Szpath[max_path]; if(GetModuleFileName (NULL, szpath,sizeof(szpath))) {std::tstring Strdumpfilename=szpath; Strdumpfilename+ = TEXT (". DMP"); HANDLE hfile= CreateFile (Strdumpfilename.c_str (), file_all_access,0, NULL, create_always, NULL, or NULL); if(hfile! =Invalid_handle_value) {minidump_exception_information exception_information; Exception_information. ThreadId=GetCurrentThreadID (); Exception_information. Exceptionpointers=Exceptioninfo; Exception_information. Clientpointers=TRUE; if(MiniDumpWriteDump (hprocess, dwprocess, hfile, Minidumpnormal, &exception_information, NULL, NULL)) {Bdumpok=true; } closehandle (hfile); }} CloseHandle (hprocess); } if(Bdumpok) MessageBox (NULL, TEXT ("This program encountered an unhandled exception, the MiniDump file has been generated in the program's running directory. "), TEXT ("Tips"), MB_OK); ElseMessageBox (NULL, TEXT ("The program encountered an unhandled exception and failed to generate the MiniDump file. "), TEXT ("Tips"), MB_OK); returnException_execute_handler;} BOOLCrashdumper::_placeholder () {return true;}
The code is simple, the only thing you need to mention is the following code, the trick is to solve the problem when the Crash_dumper_w32.cpp file is compiled into a separate static library that is not working in the program.
Namespace
{
const BOOL Bplaceholder = Crashdumper::_placeholder ();
}
The reason in the static library is that the code in the CPP does not work because there is no code to invoke the Crash_dumper_w32.cpp code, and the link is discarded by the compiler. The above statement defines a variable in the anonymous space so that each. cpp file containing it is "forced" to create an inaccessible Bplaceholder variable that must be initialized with the Crashdumper::_placeholder () function. The code for the Crash_dumper_w32.cpp file is forced to be linked in.
In addition, if you are a service-type program, you can also increase the ability to automatically start a new instance in the exception handler to ensure uninterrupted service.
Automatic recording of MiniDump C + + classes when the "Go" program crashes