#include <dbghelp.h>
#pragma comment(lib, "dbghelp.lib")
#include <eh.h>// 别忘记将编译器设定成 /EHa!
set_terminate(my_terminate_handler);
SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
LONG WINAPI MyUnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionPtrs)
{
// Do something, for example generate error report
//..
// Execute default exception handler next
ExceptionHandler2(pExceptionPtrs);
return EXCEPTION_EXECUTE_HANDLER;
}
void my_terminate_handler()
{
// Abnormal program termination (terminate() function was called)
// Do something here
// Finally, terminate program
std::cout << "terminate.\n";
exit(1);
}
unsigned long ExceptionHandler2(LPEXCEPTION_POINTERS pExceptionPointers)
{
char szFile[512];
SYSTEMTIME stLocalTime;
GetLocalTime( &stLocalTime );
sprintf_s(szFile, "dmp_%04d%02d%02d_%02d-%02d-%02d.dmp",stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond);
WCHAR wszClassName[256];
memset(wszClassName,0,sizeof(wszClassName));
MultiByteToWideChar(CP_ACP,0,szFile,strlen(szFile)+1,wszClassName,
sizeof(wszClassName)/sizeof(wszClassName[0]));
HANDLE hDmpFile = ::CreateFile(wszClassName, 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;
BOOL bOk = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDmpFile, MiniDumpNormal, &stInfo, NULL, NULL);
return bOk;
}