#include <DbgHelp.h>
#pragma comment(lib, "Dbghelp.lib")
LONG WINAPI ExceptionHandler(LPEXCEPTION_POINTERS pExceptionPointers)
{
char szFile[MAX_PATH] = {0};
SYSTEMTIME st = {0};
GetLocalTime(&st);
sprintf_s(szFile, "xxx_%d%02d%02d_%02d%02d%02d.dmp",
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond);
HANDLE hDmpFile = ::CreateFileA(szFile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);
MINIDUMP_EXCEPTION_INFORMATION stInfo = {0};
stInfo.ThreadId = GetCurrentThreadId();
stInfo.ExceptionPointers = pExceptionPointers;
stInfo.ClientPointers = TRUE;
return MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDmpFile, MiniDumpNormal, &stInfo, NULL, NULL);
}
然后在程序初始化的位置
::SetUnhandledExceptionFilter(ExceptionHandler);
这样,只要程序发生崩溃,便会记录一个minidump文件。dmp调试方法:
双击dmp文件,默认以vs打开:
进行调试,如下:
这样便可还原崩溃现场,进行跟踪调试快速找出bug。