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;
相关推荐
小道士写程序9 小时前
Qt 5.12 上读取 .xlsx 文件(Windows 平台)
开发语言·windows·qt
yxc_inspire14 小时前
基于Qt的app开发第十三天
c++·qt·app·tcp·面向对象
潇-xiao15 小时前
Qt 按钮类控件(Push Button 与 Radio Button)(1)
c++·qt
追风赶月、17 小时前
【QT】认识QT
开发语言·qt
溟洵20 小时前
【C++ Qt】窗口(Qt窗口框架、菜单栏QMenuBar)
c++·qt
Wyn_21 小时前
【QT】qtdesigner中将控件提升为自定义控件后,css设置样式不生效(已解决,图文详情)
开发语言·qt
道剑剑非道1 天前
QT开发技术【ffmpeg + QAudioOutput】音乐播放器
开发语言·qt·ffmpeg
@残梦1 天前
129、QT搭建FFmpeg环境
开发语言·qt·ffmpeg
范纹杉想快点毕业2 天前
C++抽象类与多态实战解析
java·c语言·开发语言·c++·python·qt