系列文章目录
文章目录
- 系列文章目录
- 前言
- 一、dmp文件生成
- 二、使用步骤
- 1.代码案例
- 2.运行截图
 
前言
qt编译的可执行程序在windows下崩溃可生成dmp文件,用于调试定位崩溃原因。
一、dmp文件生成
略
二、使用步骤
1.代码案例
代码如下(示例):
untitled.pro(pro工程配置文件),添加以下代码
LIBS += -lDbgHelp
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <winsock2.h>
#include <dbghelp.h>
#include <windows.h>
#include <winnt.h>
#include <QMessageBox>
#include <QString>
#include <QTime>
LONG ExceptionCapture(EXCEPTION_POINTERS *pException)
{
    //当前时间串
    const int TIMESTRLEN = 32;
    WCHAR timeStr[TIMESTRLEN];
    SYSTEMTIME time;
    GetLocalTime(&time);
    swprintf_s(timeStr, TIMESTRLEN, L"%4d%02d%02d%02d%02d%02d", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond);
    WCHAR strname[MAX_PATH];
    swprintf_s(strname, MAX_PATH, L"application_%s.dmp", timeStr);
    //创建 Dump 文件
    HANDLE hDumpFile = CreateFile(strname, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
    if( hDumpFile != INVALID_HANDLE_VALUE)
    {
        //Dump信息
        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
        dumpInfo.ExceptionPointers = pException;
        dumpInfo.ThreadId = GetCurrentThreadId();
        dumpInfo.ClientPointers = TRUE;
        //写入Dump文件内容
        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, nullptr, nullptr);
    }
    //完成一些数据保存工作
    //.....
    //弹出错误对话框并退出程序
    QMessageBox::critical(nullptr,"错误提示",QString("当前程序遇到异常.\n  异常文件:%1").arg(QString::fromWCharArray(strname)),QMessageBox::Ok,QMessageBox::Ok);
    return EXCEPTION_EXECUTE_HANDLER;
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    //注冊异常捕获函数
    SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)ExceptionCapture);
    //测试用
    int *p=nullptr;
    *p=100;
    return a.exec();
}
2.运行截图

 

该处使用的url网络请求的数据。





![[StartingPoint][Tier2]Vaccine](https://img-blog.csdnimg.cn/img_convert/b8d655cc6871374fa21f68a810b121b7.jpeg)













