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;
}