1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <Windows.h> #include <DbgHelp.h> #include <QMessageBox>
LONG ApplicationCrashHandler(EXCEPTION_POINTERS *pException) { TCHAR szFileName[128] = { 0 }; SYSTEMTIME stLocalTime; GetLocalTime(&stLocalTime); swprintf_s(szFileName, L"%hs/dump/DumpFile-%04d%02d%02d-%02d%02d%02d.dmp", QCoreApplication::applicationDirPath().toStdString().c_str(), stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay, stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond); HANDLE hDumpFile = CreateFile(szFileName, GENERIC_WRITE, 0, 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; MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL); CloseHandle(hDumpFile); }
EXCEPTION_RECORD *record = pException->ExceptionRecord; QString errCode = QString::number(record->ExceptionCode, 16); QString errAdr = QString::number((uint)record->ExceptionAddress, 16); QString str = QString("errCode: 0x%1 errAddress: 0x%2").arg(errCode).arg(errAdr); QMessageBox::critical(NULL, "error", str, QMessageBox::Ok); return EXCEPTION_EXECUTE_HANDLER; }
int main(int argc, char *argv[]) { QApplication a(argc, argv); SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)ApplicationCrashHandler);
MainWindow w; w.show();
return a.exec(); }
|