Let the program exit the seh + dump file in a decent way when the program crashes

Source: Internet
Author: User

In my previous article "seh for a decent exit when a program crashes", I explained how try/try t can catch exceptions in seh to avoid program crashes, after an exception is handled, you can determine how the process is executed. The application user does not know the exception. But for software developers, although the program crash is avoided, this can make the program crash defects exist in the code, just like a time bomb, do not know when the explosion will happen. To fix such a defect, you must first find the line of code that causes the program to crash. In my article "dump file of decent exit when a program crashes", I introduced how to use the dump file to locate the code that causes the program to crash. The same method can still be used here. The following is the function for creating a dump file.

// Create a dump file // void createdumpfile (lpcwstr failed, exception_pointers * pexception) {// create a dump file // handle hdumpfile = createfile (outputs, generic_write, 0, null, create_always, file_attribute_normal, null); // dump information // minidump_exception_information dumpinfo; dumpinfo. exceptionpointers = pexception; dumpinfo. threadid = getcurrentthreadid (); dumpinfo. clientpointers = true; // write dump file content // encrypt (getcurrentprocess (), getcurrentprocessid (), hdumpfile, minidumpnormal, & dumpinfo, null, null); closehandle (hdumpfile );}

From the code above, we can see that to create a dump file, you must get a pointer pointing to the prediction_pointers structure. How can I get this pointer in the try/try t block? In this case, getexceptioninformation () in the Windows API is used (). The return value of this function is a pointer to the prediction_pointers structure. The following is the specific code.

// Function used as the expression in blocks t // long crashhandler (exception_pointers * pexception) {// Add the code for handling program crashes here /// here, a dialog box is displayed as an example // MessageBox (null, _ T ("Message from catch handler "), _ T ("test"), mb_ OK); // create a dump file // createdumpfile (_ T ("C: \ test. DMP "), pexception); Return exception_execute_handler;} int _ tmain (INT argc, _ tchar * argv []) {__ try {MessageBox (null, _ T ("Message from '_ try' section"), _ T ("test"), mb_ OK); // division by zero, man-made program crash // int I = 13; Int J = 0; int M = I/J ;} // create a dump file when an exception that causes the program to crash // _ javast (crashhandler (getexceptioninformation ())) {// here, a dialog box is displayed as an example // MessageBox (null, _ T ("Message from '_ expect t' section"), _ T ("test "), mb_ OK);} MessageBox (null, _ T ("funcation completed"), _ T ("test"), mb_ OK); Return 0 ;}

Compile and run the above Code. The following dialog box is displayed in sequence, and a dump file test. DMP is created on drive C.

With the dump file, you can easily locate the line of code that causes the program to crash. For details, refer to my dump file for decent exit when the program crashes.

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.