In my "Let the program in the collapse of the graceful exit of the CallStack" provides a method of CallStack when the program crashes. But to get callstack, there must be support for PDB files. However, under normal circumstances, the published program is release version number, will not be accompanied by PDB files. So how can we find the exact location of the error when the program crashes? This time the dump file is out! The dump file is a memory image of the process and is able to keep the state of the program in its entirety when it executes.
To create a dump file when the program crashes, you need to use the MiniDumpWriteDump () function of the Windows API in DbgHelp.dll. The function declaration is for example the following:
BOOL WINAPI minidumpwritedump ( __in HANDLE hprocess, __in DWORD ProcessId, __in HANDLE hfile, __in minidump_type dumptype, __in pminidump_exception_information Exceptionparam, __ in Pminidump_user_stream_information userstreamparam, __in pminidump_callback_information Callbackparam);
Explanations of specific parameters and return values can be found on MSDN, with very specific instructions. The sample code in the previous article is still used to illustrate how to create a dump file when the program crashes.
Handle unhandled EXCEPTION callback function//long Applicationcrashhandler (exception_pointers *pexception) {//Here an error dialog box pops up and exits the program/ Fatalappexit ( -1, _t ("* * * unhandled exception! "); return exception_execute_handler;} A class with function call//class Crashtest{public:void Test () {Crash ();} Private:void Crash () {///except 0, man-made program crashes//int i = 13;int j = 0;int m = i/j;}}; int _tmain (int argc, _tchar* argv[]) {//Set callback function for handling unhandled exception//SetUnhandledExceptionFilter ((lptop_level_ Exception_filter) Applicationcrashhandler); Crashtest Test;test. Test (); return 0;}
When the above program crashes, the function Applicationcrashhandler () is called. The code that creates the dump file needs to be added to the function. Here is a function to create a dump file.
Create dump File//void Createdumpfile (LPCWSTR lpstrdumpfilepathname, exception_pointers *pexception) {//create dump file//handle Hdumpfile = CreateFile (lpstrdumpfilepathname, generic_write, 0, NULL, create_always, file_attribute_normal, NULL);// Dump information//minidump_exception_information dumpinfo;dumpinfo.exceptionpointers = Pexception;dumpinfo.threadid = GetCurrentThreadID ();d umpinfo.clientpointers = true;//Write dump file contents//minidumpwritedump (GetCurrentProcess (), GetCurrentProcessId (), Hdumpfile, minidumpnormal, &dumpinfo, NULL, NULL); CloseHandle (hdumpfile);}
The function Applicationcrashhandler () uses the following code to invoke the above function to create a dump file when the program crashes.
Createdumpfile (_t ("C:\\test.dmp"), pexception);
Here's a quick way to use the dump file. Copy the dump file to the folder containing the application and the corresponding PDB file, open the dump file in VS (or simply double-click the dump file), VS will create a solution itself, directly debug execution, and the code will stop to the line that caused the program to crash. Just like debugging code in vs. (VS2008)
When you open the dump file in VS2010, you will be presented with a minidump, file Summary, and be able to perform the actions in.
Let the program in crash when the decent exit dump file