dump代码提取

直接上代码

运行环境为Vs2003及vs2013

BugDump.h

/********************************************************************
	描述:	bug处理模块
*********************************************************************/
#pragma once



class CBugDump
{
	//	NO_COPY_CLASS(CBugDump);

public:

	//启用或者停用bug处理功能
	static BOOL Enable(BOOL bEnable);

private:
	static LONG WINAPI OnUnhandledExceptionFilter(struct _EXCEPTION_POINTERS* pExceptionInfo);

};
bugdump.cpp

#include "stdafx.h"
#include "BugDump.h"
#include 
#include 
#include 
#include 
#include 


/*
BOOL WINAPI MiniDumpWriteDump(  
__in  HANDLE hProcess,  
__in  DWORD ProcessId,  
__in  HANDLE hFile,  
__in  MINIDUMP_TYPE DumpType,  
__in  PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,  
__in  PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,  
__in  PMINIDUMP_CALLBACK_INFORMATION CallbackParam  
);  
*/
typedef BOOL (WINAPI *fnMiniDumpWriteDump)(HANDLE ,DWORD ,HANDLE ,MINIDUMP_TYPE ,
										   PMINIDUMP_EXCEPTION_INFORMATION ,PMINIDUMP_USER_STREAM_INFORMATION ,
										   PMINIDUMP_CALLBACK_INFORMATION );


BOOL CBugDump::Enable(BOOL bEnable)
{
	if(bEnable)
	{
		HMODULE hDll = LoadLibrary(_T("dbghelp.dll"));
		if(hDll == NULL)
			return FALSE;

		fnMiniDumpWriteDump fn = reinterpret_cast(GetProcAddress(hDll,"MiniDumpWriteDump"));
		::FreeLibrary(hDll);

		if(fn == NULL)
			return FALSE;

		LPTOP_LEVEL_EXCEPTION_FILTER lpOld = SetUnhandledExceptionFilter(&CBugDump::OnUnhandledExceptionFilter);
		lpOld = SetUnhandledExceptionFilter(&CBugDump::OnUnhandledExceptionFilter);
		lpOld = NULL;
	}
	else
	{
		SetUnhandledExceptionFilter(NULL);
	}

	return TRUE;
}

LONG WINAPI CBugDump::OnUnhandledExceptionFilter(struct _EXCEPTION_POINTERS* pExceptionInfo)
{
	LONG lRetValue = EXCEPTION_CONTINUE_SEARCH;

	HMODULE hDll = LoadLibrary(_T("dbghelp.dll"));
	if(hDll)
	{   
		fnMiniDumpWriteDump fn = reinterpret_cast(GetProcAddress(hDll,"MiniDumpWriteDump"));
		if(fn)
		{
			TCHAR szFullPath[MAX_PATH];
			GetModuleFileName(NULL,szFullPath,MAX_PATH);
			TCHAR drive[_MAX_DRIVE]={};//c:
			TCHAR dir[_MAX_DIR]={};// \sample\ctrl
			TCHAR fname[_MAX_FNAME]={};// gameserver
			TCHAR ext[_MAX_EXT]={};	// exe
 			_tsplitpath(szFullPath,drive,dir, fname, ext);
 			
			//当前时间
			SYSTEMTIME sysTime;
			GetLocalTime(&sysTime);
			CString szFileName;
			szFileName.Format(TEXT("%s:/%s/%s_%d%d%d%d%d%d.dmp"),drive,dir,fname,sysTime.wYear,sysTime.wMonth,sysTime.wDay,
				sysTime.wHour,sysTime.wMinute,sysTime.wSecond);

			TCHAR* pszFileName = new TCHAR[MAX_PATH];
			pszFileName = szFileName.GetBuffer(szFileName.GetLength());
			szFileName.ReleaseBuffer();

			HANDLE hFile = CreateFile(pszFileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

			delete[] szFileName;
			pszFileName = NULL;

			if(INVALID_HANDLE_VALUE!=hFile)
			{
				_MINIDUMP_EXCEPTION_INFORMATION ExInfo = {0};
				ExInfo.ThreadId = GetCurrentThreadId();
				ExInfo.ExceptionPointers = pExceptionInfo;
				ExInfo.ClientPointers = NULL;

				BOOL bRet = fn(GetCurrentProcess(),GetCurrentProcessId(),hFile,MiniDumpWithFullMemory,&ExInfo,NULL,NULL);

				CloseHandle(hFile);

				if(bRet)
				{
					// 						CString strCmd;
					// 						strCmd.Format(_T("-filename=%s -exename=博乐游戏 -shellname=minboohall.exe"),szFileName);
					// 						
					// 						CString strExt = szExt;
					// 						strExt.MakeLower();
					// 						if(strExt == _T(".exe"))
					// 							strCmd+=_T(" -restart=true");
					// 						else
					// 							strCmd+=_T(" -restart=disable");
					// 
					// 						//执行文件
					// 						HANDLE hReport = ShellExecute(NULL,_T("Open"),_T("BugReporter.exe"),strCmd,NULL,SW_SHOW);
					// 						if(HandleToLong(hReport)>32)
					// 						{
					// 							lRetValue = EXCEPTION_EXECUTE_HANDLER;
					// 						}
				//	std::wstring info = std::wstring(L"提交错误日志文件") + std::wstring( szFileName );
				//	MessageBox( NULL, info.c_str(), L"debug", 0 );
				}
				else
				{
					DeleteFile(szFileName);
				}

			}

		}
		::FreeLibrary(hDll);
		hDll = NULL;
	}

#ifndef _DEBUG
	if (EXCEPTION_EXECUTE_HANDLER == lRetValue)
		ExitProcess(0);
	else
		return lRetValue;


#endif
	return lRetValue;
}

看看得懂的人看哈

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