软件使用时会遇到崩溃问题,程序员可以通过查看堆栈信息定位问题,但是如果客户使用没有记录崩溃信息,这就导致无法查看,这时可以使用SetUnhandledExceptionFilter来捕获程序的崩溃,
具体使用方法如下:
#ifdef Q_OS_WIN #include <Windows.h> #include <aclapi.h> #include <dbghelp.h> #include <wbemidl.h> #include <comdef.h> #endif
#ifdef Q_OS_WIN LONG ApplicationCrashHandler(EXCEPTION_POINTERS *pException) { QString dumpFileName; dumpFileName = QDateTime::currentDateTime().toString("yyyy.MM.dd_HH.mm.ss_zzz")+".dmp"; dumpFileName.insert(dumpFileName.lastIndexOf("."), QString("_%1").arg("软件版本")); dumpFileName = "E/" + dumpFileName; HANDLE hDumpFile = CreateFile((LPCWSTR)(dumpFileName.utf16()), GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); if(hDumpFile!=INVALID_HANDLE_VALUE){ MINIDUMP_EXCEPTION_INFORMATION dumpInfo; dumpInfo.ExceptionPointers = pException; dumpInfo.ThreadId = GetCurrentThreadId(); dumpInfo.ClientPointers = TRUE;
bool dump = MiniDumpWriteDump(GetCurrentProcess(),GetCurrentProcessId(), hDumpFile,MiniDumpNormal,&dumpInfo,NULL,NULL); } return EXCEPTION_EXECUTE_HANDLER; } #endif