码迷,mamicode.com
首页 > 编程语言 > 详细

封装了一个C++类,当程序意外崩溃的时候可以生成dump文件,以便确定错误原因。

时间:2020-07-09 19:40:17      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:void   info   inter   ret   for   attr   create   pat   cal   

#pragma once

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

#pragma comment(lib, "dbghelp.lib") 

typedef void(__stdcall *PF_CallBack)(const char* msg);


class MiniDump 
{
private:
    static      PF_CallBack     m_pFun;
    static      char            m_szProcessName[32];

public:
    MiniDump(){};
    ~MiniDump(){};

    static void TcharToChar(TCHAR * tchar, char * _char);
    static void CharToTchar(const char * _char, TCHAR * tchar);
    static const char * GenerateDumpFilePath();

    static void EnableAutoMinDump(const char* pProcessName, PF_CallBack pFunc, bool bEnable = true);
    static LONG ApplicationUnhandledExceptionFilter(EXCEPTION_POINTERS *pException);
    static void CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException);

};

PF_CallBack MiniDump::m_pFun = NULL;
char        MiniDump::m_szProcessName[32] = "test";

void MiniDump::EnableAutoMinDump(const char* pProcessName, PF_CallBack pFunc, bool bEnable)
{
    if (bEnable) {
        m_pFun = pFunc;
        memset(m_szProcessName, 0, sizeof(m_szProcessName));
        memcpy(m_szProcessName, pProcessName, strlen(pProcessName) + 1);
        SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)ApplicationUnhandledExceptionFilter);
    }
}

LONG MiniDump::ApplicationUnhandledExceptionFilter(EXCEPTION_POINTERS *pException)
{
    TCHAR szDumpFile[MAX_PATH] = { 0 };
    CharToTchar(GenerateDumpFilePath(), szDumpFile);
    CreateDumpFile(szDumpFile, pException);

    if (m_pFun != NULL) {
        char szMsg[2048] = { 0 };
        m_pFun(szMsg);
    }

    return EXCEPTION_EXECUTE_HANDLER;
}

void MiniDump::CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException)
{
    // 创建Dump文件;
    HANDLE hDumpFile = CreateFile(lpstrDumpFilePathName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    // Dump信息;
    if (hDumpFile != NULL && hDumpFile != INVALID_HANDLE_VALUE) {
        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
        dumpInfo.ExceptionPointers = pException;
        dumpInfo.ThreadId = GetCurrentThreadId();
        dumpInfo.ClientPointers = TRUE;

        // 写入Dump文件内容;
        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
        CloseHandle(hDumpFile);
    }
}

void MiniDump::TcharToChar(TCHAR * tchar, char * _char)
{
    int iLength = 0;
    iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL);
    WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL);
}

void MiniDump::CharToTchar(const char * _char, TCHAR * tchar)
{
    int iLength;
    iLength = MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, NULL, 0);
    MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, tchar, iLength);
}

const char * MiniDump::GenerateDumpFilePath()
{
    //路径
    TCHAR szDumpDirT[MAX_PATH] = { 0 };
    GetCurrentDirectory(MAX_PATH, szDumpDirT);
    char szDumpDir[MAX_PATH] = { 0 };
    TcharToChar(szDumpDirT, szDumpDir);

    //时间
    SYSTEMTIME stTime;
    GetLocalTime(&stTime);
    char szCurtTime[64] = { 0 };
    sprintf_s(szCurtTime, sizeof(szCurtTime) / sizeof(char), "%04d-%02d-%02d-%02d-%02d-%02d", stTime.wYear, stTime.wMonth, stTime.wDay, stTime.wHour, stTime.wMinute, stTime.wSecond);

    //dump文件路径
    char szDumpFile[MAX_PATH] = { 0 };
    sprintf_s(szDumpFile, sizeof(szDumpFile) / sizeof(char), "%s\\%s_%s.dmp", szDumpDir, m_szProcessName, szCurtTime);

    return szDumpFile;
}

  

#pragma once
#include <windows.h>#include <dbghelp.h>
#pragma comment(lib, "dbghelp.lib") 
typedef void(__stdcall *PF_CallBack)(const char* msg);

class MiniDump {private:    static      PF_CallBack     m_pFun;    static      char            m_szProcessName[32];
public:    MiniDump(){};    ~MiniDump(){};
    static void TcharToChar(TCHAR * tchar, char * _char);    static void CharToTchar(const char * _char, TCHAR * tchar);    static const char * GenerateDumpFilePath();
    static void EnableAutoMinDump(const char* pProcessName, PF_CallBack pFunc, bool bEnable = true);    static LONG ApplicationUnhandledExceptionFilter(EXCEPTION_POINTERS *pException);    static void CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException);
};
PF_CallBack MiniDump::m_pFun = NULL;char        MiniDump::m_szProcessName[32] = "test";
void MiniDump::EnableAutoMinDump(const char* pProcessName, PF_CallBack pFunc, bool bEnable){    if (bEnable) {        m_pFun = pFunc;        memset(m_szProcessName, 0, sizeof(m_szProcessName));        memcpy(m_szProcessName, pProcessName, strlen(pProcessName) + 1);        SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)ApplicationUnhandledExceptionFilter);    }}
LONG MiniDump::ApplicationUnhandledExceptionFilter(EXCEPTION_POINTERS *pException){    TCHAR szDumpFile[MAX_PATH] = { 0 };    CharToTchar(GenerateDumpFilePath(), szDumpFile);    CreateDumpFile(szDumpFile, pException);
    if (m_pFun != NULL) {        char szMsg[2048] = { 0 };        m_pFun(szMsg);    }
    return EXCEPTION_EXECUTE_HANDLER;}
void MiniDump::CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException){    // 创建Dump文件;    HANDLE hDumpFile = CreateFile(lpstrDumpFilePathName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);    // Dump信息;    if (hDumpFile != NULL && hDumpFile != INVALID_HANDLE_VALUE) {        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;        dumpInfo.ExceptionPointers = pException;        dumpInfo.ThreadId = GetCurrentThreadId();        dumpInfo.ClientPointers = TRUE;
        // 写入Dump文件内容;        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);        CloseHandle(hDumpFile);    }}
void MiniDump::TcharToChar(TCHAR * tchar, char * _char){    int iLength = 0;    iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL);    WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL);}
void MiniDump::CharToTchar(const char * _char, TCHAR * tchar){    int iLength;    iLength = MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, NULL, 0);    MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, tchar, iLength);}
const char * MiniDump::GenerateDumpFilePath(){    //路径    TCHAR szDumpDirT[MAX_PATH] = { 0 };    GetCurrentDirectory(MAX_PATH, szDumpDirT);    char szDumpDir[MAX_PATH] = { 0 };    TcharToChar(szDumpDirT, szDumpDir);
    //时间    SYSTEMTIME stTime;    GetLocalTime(&stTime);    char szCurtTime[64] = { 0 };    sprintf_s(szCurtTime, sizeof(szCurtTime) / sizeof(char), "%04d-%02d-%02d-%02d-%02d-%02d", stTime.wYear, stTime.wMonth, stTime.wDay, stTime.wHour, stTime.wMinute, stTime.wSecond);
    //dump文件路径    char szDumpFile[MAX_PATH] = { 0 };    sprintf_s(szDumpFile, sizeof(szDumpFile) / sizeof(char), "%s\\%s_%s.dmp", szDumpDir, m_szProcessName, szCurtTime);
    return szDumpFile;}

封装了一个C++类,当程序意外崩溃的时候可以生成dump文件,以便确定错误原因。

标签:void   info   inter   ret   for   attr   create   pat   cal   

原文地址:https://www.cnblogs.com/hailong88/p/13275373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!