添加全局异常过滤生成转存储文件类

参考: http://tomlau-2000.blog.sohu.com/114084286.html

类:CrashDumper

CrashDumper.h

 

#ifndef _CRASH_DUMPER_H_
#define _CRASH_DUMPER_H_

#include <windows.h>

class CrashDumper
{
public:
    CrashDumper();
    ~CrashDumper();
    static bool _PlaceHolder();
private:
    LPTOP_LEVEL_EXCEPTION_FILTER m_OriginalFilter;
    static LONG WINAPI ExceptionFilter(struct _EXCEPTION_POINTERS* ExceptionInfo);
};

namespace
{
    const bool bPlaceHolder = CrashDumper::_PlaceHolder();
}

#endif
CrashDumper.cpp

#include <windows.h>
#include <tchar.h>
#include <dbghelp.h>
#include <string>

#include <QDebug>

#include "CrashDumper.h"

#ifdef UNICODE   

#     define tstring wstring    

#else      

#     define tstring string

#endif

#include <DbgHelp.h>
#pragma comment(lib, "dbghelp.lib")

CrashDumper dumper;
 
CrashDumper::CrashDumper()
{
    m_OriginalFilter = SetUnhandledExceptionFilter(ExceptionFilter);
}

CrashDumper::~CrashDumper()
{
    SetUnhandledExceptionFilter(m_OriginalFilter);
}

LONG WINAPI CrashDumper::ExceptionFilter(struct _EXCEPTION_POINTERS* ExceptionInfo)
{
    qDebug() << "exception filter";
    bool bDumpOK = false;

    TCHAR szPath[MAX_PATH];
    if(GetModuleFileName(NULL, szPath, sizeof(szPath)))
    {
        std::tstring strDumpFileName = szPath;
        strDumpFileName += TEXT(".dmp");
        HANDLE hFile = CreateFile(strDumpFileName.c_str(), FILE_ALL_ACCESS, 0, NULL, CREATE_ALWAYS, NULL, NULL);
        if (hFile != INVALID_HANDLE_VALUE)
        {
            MINIDUMP_EXCEPTION_INFORMATION exception_information;
            exception_information.ThreadId = GetCurrentThreadId();
            exception_information.ExceptionPointers = ExceptionInfo;
            exception_information.ClientPointers = TRUE;
            if (MiniDumpWriteDump(::GetCurrentProcess(),
                                  ::GetCurrentProcessId(),
                                  hFile, MiniDumpNormal, &exception_information, NULL, NULL))
            {
                bDumpOK = true;
            }

            CloseHandle(hFile);
        }
    }

    if (bDumpOK)
        qDebug() << "create dump file";

    qDebug() << "exception filter quit";
    return EXCEPTION_EXECUTE_HANDLER;
}

bool CrashDumper::_PlaceHolder() {return true;}

 

转载于:https://www.cnblogs.com/jianc/archive/2012/12/21/2827507.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值