Use setunhandledexceptionfilter to obtain the dump file when a program exception occurs.

Source: Internet
Author: User

Setunhandledexceptionfilter is used to obtain the dump file when a program exception occurs to analyze the cause of the program exception. Many software programs use this method to collect exceptions, here, we only need to transmit the dump file generated by our program to the server. After the dump file is generated, it will actually be transmitted to the specified server through the network. This facilitates the development of collection of current bugs in the program.

The following is a console program that directly copies the code and compiles and runs it. Here I use two methods to cause the program to crash and generate a dump file, we can use windbg to analyze the generated dump files.

# Include <windows. h>

# Include <dbghelp. h>

# Pragma comment (Lib, "dbghelp. lib ")

Long myunhandledexceptionfilter (_ exception_pointers * exceptioninfo)
{
Char strdumpfile [max_path] = {0 };
Systemtime TM;

Handle hfile = NULL;

Getsystemtime (& TM );
Sprintf (strdumpfile, "% 04d % 02d % 02d % 02d % 02d % 02d. DMP ", TM. wyear, TM. wmonth, TM. wday, TM. whour, TM. wminute, TM. wsecond );

Hfile = createfilea (strdumpfile,
Generic_write,
File_pai_write,
Null,
Create_always,
File_attribute_normal,
Null );
If (hfile! = Invalid_handle_value)
{
Minidump_exception_information exinfo;
Exinfo. threadid = getcurrentthreadid ();
Exinfo. exceptionpointers = exceptioninfo;
Exinfo. clientpointers = NULL;
Bool Bok = minidumpwritedump (
Getcurrentprocess (),
Getcurrentprocessid (),
Hfile,
Minidumpnormal,
& Exinfo,
Null,
Null );

Closehandle (hfile );
}

Return prediction_continue_search;
}

Int _ tmain (INT argc, _ tchar * argv [])
{

Setunhandledexceptionfilter (lptop_level_exception_filter) myunhandledexceptionfilter );

// The first method to generate an exception
// Char * STR = NULL;
// Memcpy (STR, "hello", 5 );


// Method 2 for exception generation
Int I = 10;
While (1)
{
Printf ("% d \ n", 10/I );
I --;
}

Return 0;
}

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.