Use setunhandledexceptionfilter to gracefully crash the program

Source: Internet
Author: User

Although QQ is a product of a large company, it will crash in our ways, but it is always very elegant to crash, and it will pop up its own dialog box to end. The system removes the default report sending dialog box.

So I want to make my program crash more decent.

I thought about the general idea and thought I could use a process to monitor the target program. You can also get the target program crash information, know when it crashes, and perform additional operations. However, you cannot remove the default send error dialog box.

Then someone said whether a hook-like method was used to hook up the object.

Finally, I checked it online and found that the setunhandledexceptionfilter function solved everything.

Summary of the information found below, there are three types of return values of this function:

 

Prediction_execute_handler equ 1 indicates that I have handled the exception and can end it elegantly.
Prediction_continue_search equ 0 indicates that I will not process it. If someone else comes, Windows will call the default handler to display an error box and end it.
Prediction_continue_execution equ-1 indicates that the error has been fixed. proceed from the exception

 

The usage is as follows:

 

# Include <windows. h>

Long _ stdcall callback (_ exception_pointers * excp)
{
MessageBox (0, "error", "error", mb_ OK );
Printf ("error address % x/N", excp-> exceptionrecord-> exceptionaddress );
Printf ("CPU register:/N ");
Printf ("eax % x EBX % x ECx % x edX % x/N", excp-> contextrecord-> eax,
Excp-> contextrecord-> EBX, excp-> contextrecord-> ECx,
Excp-> contextrecord-> EDX );
Return exception_execute_handler;
}

Int main (INT argc, char * argv [])
{
Setunhandledexceptionfilter (callback );
_ Asm int 3 // only for program crash
Return 0;
}

After the success, I went through the windows core programming and found that this function was already mentioned. I did not read such a profound book for the time being because of poor learning skills. So I never knew it.

I asked my colleague and found that he immediately said that this function (probably a function name) can be used for implementation, and proposed that it can be implemented under Win32, however, this exception may not be caught in MFC. We think that there is a set of mechanisms to solve the problem in MFC.

Later, I was skeptical. In the next test of MFC, I could try o (∩ _ ∩) o...

In addition, I encountered an exception in a DLL in an MDI program, and even got close to the callback function.

Then I tried an error in the control, and even the control error could be captured. What a cool word!

This article references the following two articles. The red part references the following articles:

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.