Automatic recording of MiniDump C + + classes when the "Go" program crashes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.