[Windows programming] how to capture program exceptions/crash and generate dump files

Source: Internet
Author: User

This section describes how to use windbg to generate a crash dump. windbg tips: how to generate a dump file (. dump command), but windbg is usually not installed on the user machine, and most users do not know how to use windbg. Therefore, it is best to capture exception/crash in your own program, generate a crash dump, and then transmit it back to your server through the network.

 

You can use the API function setunhandledexceptionfilter to capture exceptions. You can use the minidumpwritedump function in dbghelp. DLL to generate a crash dump.

 

Lptop_level_exception_filter winapiSetunhandledexceptionfilter(_ In lptop_level_exception_filterLptoplevelexceptionfilter);

 

Bool winapiMinidumpwritedump(_ In handleHprocess, _ In DWORDProcessid, _ In handleHfile, _ In minidump_typeDumptype, _ In pminidump_exception_informationPredictionparam, _ In pminidump_user_stream_informationUserstreamparam, _ In pminidump_callback_informationCallbackparam);

 

 

Sample Code:

 

# Include <dbghelp. h> <br/> # include <shellapi. h> <br/> # include <shlobj. h> </P> <p> // custom exectpion filter <br/> long winapi myunhandledexceptionfilter (struct _ exception_pointers * pexceptionpointers) <br/>{</P> <p> seterrormode (sem_nogpfaulterrorbox); </P> <p> // collect information <br/> cstringw strbuild; <br/> strbuild. format (L "Build: % S % s", _ date __, _ time _); <br/> cstringw strerror; <br/> hmodule; <br/> wchar szmodulename [max_path] = l ""; <br/> getmodulehandleex (handler, (lpcwstr) pexceptionpointers-> exceptionrecord-> exceptionaddress, & hmodule ); <br/> getmodulefilename (hmodule, szmodulename, arraysize (szmodulename); <br/> strerror. appenedformat (L "% S % d, % d, % d. ", szmodulename, pexceptionpointers-> predictionrecord-> predictioncode, pexceptionpointers-> predictionrecord-> predictionflags, pexceptionpointers-> predictionrecord-> predictionaddress ); </P> <p> // generate mini crash dump <br/> bool bminidumpsuccessful; <br/> wchar szpath [max_path]; <br/> wchar szfilename [max_path]; <br/> wchar * szappname = l "appname"; <br/> wchar * szversion = l "V1.0"; <br/> DWORD dwbuffersize = max_path; <br/> handle hdumpfile; <br/> systemtime stlocaltime; <br/> minidump_exception_information expparam; <br/> getlocaltime (& stlocaltime); <br/> gettemppath (dwbuffersize, szpath); <br/> stringcchprintf (szfilename, max_path, l "% S % s", szpath, szappname); <br/> createdirectory (szfilename, null ); <br/> stringcchprintf (szfilename, max_path, L "% S // % s-% 04d % 02d % 02d-% 02d % 02d % 02d-% LD-% lD. DMP ", <br/> szpath, szappname, szversion, <br/> stlocaltime. wyear, stlocaltime. wmonth, stlocaltime. wday, <br/> stlocaltime. whour, stlocaltime. wminute, stlocaltime. wsecond, <br/> getcurrentprocessid (), getcurrentthreadid (); <br/> hdumpfile = createfile (szfilename, generic_read | generic_write, <br/> file_cmd_write | file_cmd_read, 0, create_always, 0, 0); </P> <p> minidump_user_stream userstream [2]; <br/> minidump_user_stream_information userinfo; <br/> userinfo. userstreamcount = 1; <br/> userinfo. userstreamarray = userstream; <br/> userstream [0]. type = commentstreamw; <br/> userstream [0]. buffersize = strbuild. getlength () * sizeof (wchar); <br/> userstream [0]. buffer = strbuild. getbuffer (); <br/> userstream [1]. type = commentstreamw; <br/> userstream [1]. buffersize = strerror. getlength () * sizeof (wchar); <br/> userstream [1]. buffer = strerror. getbuffer (); </P> <p> expparam. threadid = getcurrentthreadid (); <br/> expparam. predictionpointers = pexceptionpointers; <br/> expparam. clientpointers = true; </P> <p> minidump_type minidumpwithdatasegs = minidumpnormal <br/> | minidumpwithhandledata <br/> | average <br/> | minidumpscanmemory <br/> | minidumpwithprocessthreaddata <br/> | minidumpwiththreadinfo; <br/> bminidumpsuccessful = minidumpwritedump (getcurrentprocess (), getcurrentprocessid (), <br/> hdumpfile, minidumpwithdatasegs, & expparam, null, null ); <br/> // upload the mini dump to your server (Omitted) <br/>... </P> <p> return exception_continue_search; // or you can use prediction_execute_handler to close the program. <br/>}</P> <p> int _ tmain () <br/>{< br/> // set execption filter <br/> setunhandledexceptionfilter (myunhandledexceptionfilter); <br/> .... <br/> return 0; <br/>}< br/>

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.