MiniDumpWriteDump函数生成dmp文件

MiniDumpWriteDump函数生成dmp文件

  • 一:概述
  • 二: CreateDump.h
  • 三:CreateDump.cpp
  • 四:main测试
  • 五:winDbg分析

一:概述

v2008及以上版本都可以用。 包含CreateDump.h,CreateDump.cpp文件,main()测试例子。

二: CreateDump.h

#pragma once
#include 
#include 
using namespace std;
class CCreateDump
{
public:
    CCreateDump();
    ~CCreateDump(void);
    static CCreateDump* Instance();
    static long __stdcall UnhandleExceptionFilter(_EXCEPTION_POINTERS* ExceptionInfo);
    //声明Dump文件,异常时会自动生成。会自动加入.dmp文件名后缀
    void DeclarDumpFile();
private:
    static WCHAR    strDumpFile[MAX_PATH];
    static CCreateDump* __instance;
};

三:CreateDump.cpp

#include "stdafx.h"
#include "CreateDump.h"
#include 
#pragma comment(lib,  "dbghelp.lib")

CCreateDump* CCreateDump::__instance = NULL;
WCHAR  CCreateDump::strDumpFile[MAX_PATH] = { 0 };

CCreateDump::CCreateDump()
{
}

CCreateDump::~CCreateDump(void)
{

}

long  CCreateDump::UnhandleExceptionFilter(_EXCEPTION_POINTERS* ExceptionInfo)
{

    HANDLE hFile = CreateFile(strDumpFile, GENERIC_WRITE, FILE_SHARE_WRITE, 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;
        //   write   the   dump
        BOOL   bOK = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL);
        CloseHandle(hFile);
        if (!bOK)
        {
            DWORD dw = GetLastError();
            //写dump文件出错处理,异常交给windows处理
            return EXCEPTION_CONTINUE_SEARCH;
        }
        else
        {    //在异常处结束
            return EXCEPTION_EXECUTE_HANDLER;
        }
    }
    else
    {
        return EXCEPTION_CONTINUE_SEARCH;
    }
}

void CCreateDump::DeclarDumpFile()
{
    SYSTEMTIME syt;
    GetLocalTime(&syt);
    char c[MAX_PATH];
	::swprintf_s(strDumpFile, _countof(strDumpFile) - 1, L"%04d-%02d-%02d_%02d%02d%02d.dmp", syt.wYear, syt.wMonth, syt.wDay, syt.wHour, syt.wMinute, syt.wSecond);
    SetUnhandledExceptionFilter(UnhandleExceptionFilter);
}

CCreateDump* CCreateDump::Instance()
{
    if (__instance == NULL)
    {
        __instance = new CCreateDump;
    }
    return __instance;
}

四:main测试

int main()
{
	CCreateDump::Instance()->DeclarDumpFile();

	int ag=0;
	int bg=2;
	int cg =bg/0;
	std::cout<<cg<<std::endl;
    return 0;
}

五:winDbg分析

百度网盘下载地址
提取码:cc70
1) 设置.dmp、.pdb文件所在路径
在以下两个地方设置路径
file->symbol search path
file->source search path

2)分析.pdb
file->open crash
选择 .dmp文件
命令窗口输入: !analyze -v

你可能感兴趣的:(c++)