Chinaunix首页 | 论坛 | 博客
  • 博客访问: 431647
  • 博文数量: 86
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 822
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-25 10:36
文章分类

全部博文(86)

文章存档

2022年(1)

2021年(3)

2020年(1)

2019年(9)

2018年(24)

2017年(20)

2016年(20)

2015年(8)

我的朋友

分类: C/C++

2016-08-10 14:43:56

#include
#include < Dbghelp.h>
#include   
#include
#include
using namespace std;
 
 
#pragma comment(lib, "Dbghelp.lib")
 
 
namespace NSDumpFile
{
    void CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException)  
    {  
        // 创建Dump文件  
        //  
        HANDLE hDumpFile = CreateFile(lpstrDumpFilePathName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);  
 
 
        // Dump信息  
        //  
        MINIDUMP_EXCEPTION_INFORMATION dumpInfo;  
        dumpInfo.ExceptionPointers = pException;  
        dumpInfo.ThreadId = GetCurrentThreadId();  
        dumpInfo.ClientPointers = TRUE;  
 
 
        // 写入Dump文件内容  
        //  
        MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);  
 
 
        CloseHandle(hDumpFile);  
    }  
 
 
    LPTOP_LEVEL_EXCEPTION_FILTER WINAPI MyDummySetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER lpTopLevelExceptionFilter)
    {
        return NULL;
    }
 
 
    BOOL PreventSetUnhandledExceptionFilter()
    {
        HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
        if (hKernel32 ==   NULL)
            return FALSE;
 
 
        void *pOrgEntry = GetProcAddress(hKernel32, "SetUnhandledExceptionFilter");
        if(pOrgEntry == NULL)
            return FALSE;
 
 
        unsigned char newJump[ 100 ];
        DWORD dwOrgEntryAddr = (DWORD) pOrgEntry;
        dwOrgEntryAddr += 5; // add 5 for 5 op-codes for jmp far
 
 
        void *pNewFunc = &MyDummySetUnhandledExceptionFilter;
        DWORD dwNewEntryAddr = (DWORD) pNewFunc;
        DWORD dwRelativeAddr = dwNewEntryAddr -  dwOrgEntryAddr;
 
 
        newJump[ 0 ] = 0xE9;  // JMP absolute
        memcpy(&newJump[ 1 ], &dwRelativeAddr, sizeof(pNewFunc));
        SIZE_T bytesWritten;
        BOOL bRet = WriteProcessMemory(GetCurrentProcess(),    pOrgEntry, newJump, sizeof(pNewFunc) + 1, &bytesWritten);
        return bRet;
    }
 
 
    LONG WINAPI UnhandledExceptionFilterEx(struct _EXCEPTION_POINTERS *pException)
    {
        TCHAR szMbsFile[MAX_PATH] = { 0 };
        ::GetModuleFileName(NULL, szMbsFile, MAX_PATH);
        TCHAR* pFind = _tcsrchr(szMbsFile, '\\');
        if(pFind)
        {
            *(pFind+1) = 0;
            _tcscat(szMbsFile, _T("CrashDumpFile.dmp"));
            CreateDumpFile(szMbsFile, pException);
        }
 
 
        // TODO: MiniDumpWriteDump
        FatalAppExit(-1,  _T("Fatal Error"));
        return EXCEPTION_CONTINUE_SEARCH;
    }
 
 
    void RunCrashHandler()
    {
        SetUnhandledExceptionFilter(UnhandledExceptionFilterEx);
        PreventSetUnhandledExceptionFilter();
    }
};
 
 
#define DeclareDumpFile() NSDumpFile::RunCrashHandler();
阅读(1251) | 评论(0) | 转发(0) |
0

上一篇:auto_increment_increment

下一篇:c++ 各种干货库

给主人留下些什么吧!~~