C + + Generate dump file

Source: Internet
Author: User

C + + code, generated using the MiniDump programming of the Dbghelp module

#include "DbgHelp.h" typedef BOOL (winapi* minidumpwritedump) (HANDLE hprocess, DWORD dwpid, HANDLE hfile, Minidump_type Du                                                Mptype, CONST pminidump_exception_information Exceptionparam,                                                CONST pminidump_user_stream_information Userstreamparam, CONST pminidump_callback_information Callbackparam); Long Cse_exception::_dbgdumperror (struct _exception_pointers *excpinfo, lpctstr Szprex) {Long retval = Exception_con      Tinue_search; Hmodule hDLL =:: LoadLibrary (_t ("Dbghelp.     DLL "));     LPCTSTR Szresult = NULL;      MiniDumpWriteDump pdump = NULL;      if (hdll) Pdump = (minidumpwritedump):: GetProcAddress (hDLL, "minidumpwritedump");         if (pdump) {TCHAR Szdumppath [_max_path] = {0};         TCHAR szdumpfile [_max_path] = {0};         GetModuleFileName (NULL, Szdumppath, _max_path);      _TCSRCHR (Szdumppath, _t (' \ \ ')) [1] = 0;   _tcscat (Szdumppath, _t ("...          \\logs\ "));         time_t timecurrent = time (0);         struct tm* TMC = localtime (&timecurrent); if (TMC) _stprintf (Szdumpfile, _t ("%s%d_%d_%d_%d_%d.dmp"), Szprex, tmc->tm_mon+1, Tmc->tm_mday, tmc-&         Gt;tm_hour, Tmc->tm_min, tmc->tm_sec);         else _tcscpy (Szdumpfile, _t ("error.dmp"));          _tcscat (Szdumppath, szdumpfile); Create the file HANDLE hfile =:: CreateFile (Szdumppath, generic_write, 0, NULL, Create_always, FI          Le_attribute_normal, NULL);              if (hfile!=invalid_handle_value) {if (Excpinfo = = NULL)//If there are no incoming exceptions, such as those that are called inside the program, an exception is generated {//Generate exception to get proper context in dump DWORD dwsize                   = 0;                   Char *sz = "_exception_pointers is null";              :: WriteFile (hfile, SZ, strlen (SZ), &dwsize, NULL);   } else           {minidump_exception_information einfo; Einfo.threadid = GetCurrentThreadID ();                   Add the required information einfo.exceptionpointers = Excpinfo;                    Einfo.clientpointers = FALSE; The type of dump is small and space-saving.                   You can refer to MSDN to generate a more detailed dump. Pdump (GetCurrentProcess (), GetCurrentProcessId (), hfile , Minidumpnormal, Excpinfo?              &einfo:null, NULL, NULL);          }:: CloseHandle (hfile);      }} if (hDLL):: FreeLibrary (hDLL);  return retval;} void Cse_exception::initseexception () {_set_se_translator (cse_exception::trans_func);} void Cse_exception::_         Dumpcallstack () {__try {__try {raiseexception (1, 0, 0, NULL); } __finally {}} __EXCept (_dbgdumperror (GetExceptionInformation (), _t ("T")), exception_continue_execution) {}} void Cse_except Ion::trans_func (unsigned int usenum, _exception_pointers* pexp) {#define Se_buf_size TCHAR pszbuf[se_buf_      SIZE+2];     _dbgdumperror (Pexp, _t ("E")); Switch (usenum) {case exception_access_violation: _sntprintf (Pszbuf, Se_buf_size, _t ("ACCESS Violation:ip : 0xX%s address:0xx "), Pexp->exceptionrecord->exceptionaddress, Pexp->exceptionrecord->exceptio         ninformation[0]?_t ("Write"): _t ("Read"), pexp->exceptionrecord->exceptioninformation[1]);     Break         Case Exception_int_divide_by_zero: _sntprintf (Pszbuf, Se_buf_size, _t ("Int_divide_by_zero"));     Break         Case Exception_flt_divide_by_zero: _sntprintf (Pszbuf, Se_buf_size, _t ("Flt_divide_by_zero"));     Break     Case exception_illegal_instruction: _sntprintf (Pszbuf, Se_buf_size, _t ("illegal_instruction"));    Break         Case exception_priv_instruction: _sntprintf (Pszbuf, Se_buf_size, _t ("exception_priv_instruction"));     Break         Case Exception_stack_overflow: _sntprintf (Pszbuf, Se_buf_size, _t ("Exception_stack_overflow"));     Break         Default: _sntprintf (Pszbuf, Se_buf_size, _t ("Unknown se_exception:x"), Usenum);     Break     } cse_exception E (PSZBUF);     E.m_nsenumber = Usenum;     E.m_serecord = * (Pexp->exceptionrecord);      E.m_secontext = * (Pexp->contextrecord); throw e;}

Then add the code in the InitInstance of the program as follows:

LONG WINAPI dbgunhandledexceptionfilter (pexception_pointers pexceptioninfo) {     Cse_exception::_dbgdumperror ( Pexceptioninfo, _t ("E"));     return exception_continue_search;    Program stops running}bool cdcpworkerapp::initinstance () {     //eha     cse_exception::initseexception ();     Catch Unhandled exception     SetUnhandledExceptionFilter (Dbgunhandledexceptionfilter);}

  

C + + Generate dump file

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.