Windows下异常捕获,生成dump文件

//#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 系统中,当程序崩溃时,操作系统会自动生成一个 .dmp 文件,也就是所谓的 Dump 文件,其中包含了程序崩溃时的一些现场信息。当程序开发者拿到 Dump 文件后,就可以通过调试工具来分析崩溃原因,从而修复程序中的问题。 在 Qt 工程中,如果在 main 函数中发生了异常,一般可以通过以下代码来进行处理: ``` int main(int argc, char *argv[]) { qInstallMessageHandler(myMessageOutput); QApplication app(argc, argv); try { // ... } catch (std::exception &e) { qCritical() << "Exception caught: " << e.what(); return 1; } return app.exec(); } ``` 这样,在异常发生时,程序会输出异常信息并退出,同时会生成一个 Dump 文件。但是,如果异常发生在其他地方,例如某个函数中,那么就需要在该函数中进行相应的异常处理,并在处理完异常后手动触发生成 Dump 文件的功能。 一种常见的方法是使用 MiniDumpWriteDump 函数来生成 Dump 文件。例如: ``` #include <windows.h> #include <dbghelp.h> void generateDump(EXCEPTION_POINTERS* pException) { HANDLE hDumpFile = CreateFile("myapp.dmp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hDumpFile != INVALID_HANDLE_VALUE) { MINIDUMP_EXCEPTION_INFORMATION exceptionInfo; exceptionInfo.ThreadId = GetCurrentThreadId(); exceptionInfo.ExceptionPointers = pException; exceptionInfo.ClientPointers = FALSE; MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &exceptionInfo, NULL, NULL); CloseHandle(hDumpFile); } } void foo() { // ... __try { // ... } __except (generateDump(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) { // ... } } ``` 这段代码中,我们定义了一个 generateDump 函数来生成 Dump 文件,并在 foo 函数中使用 __try 和 __except 块来捕获异常并触发生成 Dump 文件的功能。注意,这里的 EXCEPTION_EXECUTE_HANDLER 参数表示异常处理程序应该被执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值