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;
相关推荐
范特西.i4 天前
QT聊天项目(8)
开发语言·qt
枫叶丹44 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
十五年专注C++开发4 天前
Qt deleteLater作用及源码分析
开发语言·c++·qt·qobject
kangzerun5 天前
SQLiteManager:一个优雅的Qt SQLite数据库操作类
数据库·qt·sqlite
金刚狼885 天前
qt和qt creator的下载安装
开发语言·qt
追烽少年x5 天前
Qt中使用Zint库显示二维码
qt
谁刺我心5 天前
qt源码、qt在线安装器镜像下载
开发语言·qt
金刚狼885 天前
在qt creator中创建helloworld程序并构建
开发语言·qt
扶尔魔ocy5 天前
【转载】QT使用linuxdeployqt打包
开发语言·qt
YxVoyager5 天前
在VS2017中使用Qt的foreach宏,IntelliSense无法正确识别函数定义
c++·qt