//#include "ExceptionDumpGenerate.h"
#include <windows.h>
#include <QString>
#include <QDateTime>
#include <QCoreApplication>
#include <QMessageBox>
#include <QDir>
#include <dbghelp.h>
#pragma comment(lib, "dbghelp.lib")
QString _dumpName = "yxExe";
LONG applicationCrashHandler(EXCEPTION_POINTERS* pException)
{
QString dumpDirPath = QCoreApplication::applicationDirPath() + "/Dump";
QDir dir(dumpDirPath);
if (!dir.exists())
dir.mkpath(dumpDirPath);
QString dumpFilePath = QString("%1/%2-%3.dmp")
.arg(dumpDirPath)
.arg(_dumpName)
.arg(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"));
HANDLE hDumpFile = CreateFile((LPCWSTR)dumpFilePath.utf16(), 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;
//将dump信息写入dmp文件
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
CloseHandle(hDumpFile);
}
//创建消息提示
EXCEPTION_RECORD* record = pException->ExceptionRecord;
QString errCode(QString::number(record->ExceptionCode, 16));
QString errAddr(QString::number((uint)record->ExceptionAddress, 16));
QString errFlag(QString::number(record->ExceptionFlags, 16));
QString errPara(QString::number(record->NumberParameters, 16));
QString strTip = QString("<FONT size=4> \
<div><b>运行异常,程序崩溃!</b><br/></div> \
<div>ErrorCode:%1</div> \
<div>ErrorAddr:%2</div> \
<div>ErrorFlag:%3</div> \
<div>ErrorPara:%4</div> \
</FONT>").arg(errCode).arg(errAddr).arg(errFlag).arg(errPara);
QMessageBox::critical(NULL, "程序崩溃", strTip, QMessageBox::Ok);
return EXCEPTION_EXECUTE_HANDLER;
}
void initExceptionDumpGenerate()
{
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)applicationCrashHandler);
//主动抛出异常
//RaiseException(EXCEPTION_FLT_DIVIDE_BY_ZERO, 0, 0, NULL);
}
Windows下异常捕获,生成dump文件
于 2023-08-03 16:14:44 首次发布