Qt程序增加Dump文件保存

qt程序出现程序闪退,对这些未能捕获的异常,存储未Dump文件方便我们定位哪块代码出的问题。利用Window API 的相关接口,具体如下

cpp 复制代码
#include <QCoreApplication>
#include <windows.h>
#include <DbgHelp.h>
#include <QDateTime>
#include <QDir>
#include <QString>


// 异常处理函数
LONG WINAPI UnhandledExceptionFilterEx(PEXCEPTION_POINTERS ExceptionInfo) {
    // 生成唯一的 dump 文件名
    QString dumpFileName = QDateTime::currentDateTime().toString("yyyyMMddhhmmss") + ".dmp";
    QString dumpFilePath = QDir::currentPath() + "/" + dumpFileName;

    // 使用 CreateFileW 创建文件用于保存 dump 文件
    HANDLE hFile = CreateFileW(dumpFilePath.toStdWString().c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile != INVALID_HANDLE_VALUE) {
        MINIDUMP_EXCEPTION_INFORMATION ExInfo;
        ExInfo.ThreadId = GetCurrentThreadId();
        ExInfo.ExceptionPointers = ExceptionInfo;
        ExInfo.ClientPointers = FALSE;

        // 写入 dump 文件
        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL);
        CloseHandle(hFile);
    }
    return EXCEPTION_EXECUTE_HANDLER;
}

/*******************************************************************
@fn    main
@biref    程序主函数入口
@detail   程序主函数入口
@param    argc
@param    argv
@return   int
@Create   
******************************************************************/
int main(int argc, char *argv[])
{
    // 设置异常处理函数
    SetUnhandledExceptionFilter(UnhandledExceptionFilterEx);

    QApplication a(argc, argv);

    return a.exec();
}

// 链接 DbgHelp.lib 库
#pragma comment(lib, "DbgHelp.lib")  

上边是按照最小dump生成的(MiniDumpNormal),如果需要其他可以调整出入参数类型

cpp 复制代码
typedef enum _MINIDUMP_TYPE {
    MiniDumpNormal                         = 0x00000000,
    MiniDumpWithDataSegs                   = 0x00000001,
    MiniDumpWithFullMemory                 = 0x00000002,
    MiniDumpWithHandleData                 = 0x00000004,
    MiniDumpFilterMemory                   = 0x00000008,
    MiniDumpScanMemory                     = 0x00000010,
    MiniDumpWithUnloadedModules            = 0x00000020,
    MiniDumpWithIndirectlyReferencedMemory = 0x00000040,
    MiniDumpFilterModulePaths              = 0x00000080,
    MiniDumpWithProcessThreadData          = 0x00000100,
    MiniDumpWithPrivateReadWriteMemory     = 0x00000200,
    MiniDumpWithoutOptionalData            = 0x00000400,
    MiniDumpWithFullMemoryInfo             = 0x00000800,
    MiniDumpWithThreadInfo                 = 0x00001000,
    MiniDumpWithCodeSegs                   = 0x00002000,
    MiniDumpWithoutAuxiliaryState          = 0x00004000,
    MiniDumpWithFullAuxiliaryState         = 0x00008000,
    MiniDumpWithPrivateWriteCopyMemory     = 0x00010000,
    MiniDumpIgnoreInaccessibleMemory       = 0x00020000,
    MiniDumpWithTokenInformation           = 0x00040000,
    MiniDumpWithModuleHeaders              = 0x00080000,
    MiniDumpFilterTriage                   = 0x00100000,
    MiniDumpWithAvxXStateContext           = 0x00200000,
    MiniDumpWithIptTrace                   = 0x00400000,
    MiniDumpScanInaccessiblePartialPages   = 0x00800000,
    MiniDumpFilterWriteCombinedMemory      = 0x01000000,
    MiniDumpValidTypeFlags                 = 0x01ffffff,
} MINIDUMP_TYPE;
相关推荐
IOT-Power2 小时前
QT 对话框(QDialog)中 accept、reject、exec、open的使用
开发语言·qt
_OP_CHEN2 小时前
【从零开始的Qt开发指南】(十九)Qt 文件操作:从 I/O 设备到文件信息,一站式掌握跨平台文件处理
开发语言·c++·qt·前端开发·文件操作·gui开发·qt文件
cn_mengbei3 小时前
鸿蒙PC开发指南:从零配置Qt环境到实战部署完整流程
qt·华为·harmonyos
GREGGXU3 小时前
Could not load the Qt platform plugin “xcb“ in ““ even though it was found.
linux·qt
Summer_Uncle4 小时前
【QT学习】qt项目使用MySQL数据库
数据库·qt·学习
Henry Zhu1234 小时前
Qt样式系统详解(上)
qt
深蓝海拓4 小时前
PySide6从0开始学习的笔记(二十三)使用QRunnable在线程池中执行临时任务
笔记·python·qt·学习·pyqt
嘿嘿潶黑黑5 小时前
关于QButtonGroup 在Qt5和Qt6之间的差异
开发语言·qt
hqwest5 小时前
码上通QT实战09--监控页面01-区域划分
开发语言·qt·layout·qss·qt 布局
mingren_13145 小时前
c++和qml交互
c++·qt·交互