一个产生dump文件类

#include <DbgHelp.h>
#pragma comment(lib, "dbghelp.lib")
class CExceptionHandler
{
public:
	static CString GetDumpFilename()
	{
		CString filename;
		TCHAR buffer[MAX_PATH];
		GetModuleFileName(NULL,buffer,MAX_PATH);
		filename = buffer;
		int index = filename.ReverseFind(_T('.'));
		filename=filename.Left(index);
		filename+=_T(".dmp");

		return filename;
	}

	static LONG WINAPI SelfUnhandledExceptionFilter(__in struct _EXCEPTION_POINTERS *ExceptionInfo)
	{
		USES_CONVERSION;

		MINIDUMP_EXCEPTION_INFORMATION ExceptionParam;
		ExceptionParam.ThreadId=GetCurrentThreadId();
		ExceptionParam.ExceptionPointers=ExceptionInfo;
		ExceptionParam.ClientPointers=TRUE;

		MINIDUMP_USER_STREAM UserStreams[2];
		
		WCHAR strBulid[MAX_PATH];
		StringCchPrintf(strBulid, MAX_PATH, L"Build: %s %s", A2W(__DATE__), A2W(__TIME__));
		UserStreams[0].Type=CommentStreamW;
		UserStreams[0].Buffer=strBulid;
		UserStreams[0].BufferSize=MAX_PATH;


		HMODULE hModule;
		GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
			(LPCTSTR)ExceptionInfo->ExceptionRecord->ExceptionAddress,&hModule);
		WCHAR strModuleName[MAX_PATH];
		GetModuleFileNameW(hModule,strModuleName,MAX_PATH);

		WCHAR szError[MAX_PATH*2];
		StringCchPrintf(szError,MAX_PATH*2,
			L"Module=%s ,Code=%x ,Flag=%x ,Address=%p",
			strModuleName,ExceptionInfo->ExceptionRecord->ExceptionCode,
			ExceptionInfo->ExceptionRecord->ExceptionFlags,
			ExceptionInfo->ExceptionRecord->ExceptionAddress);
		UserStreams[1].Type=CommentStreamW;
		UserStreams[1].Buffer=szError;
		UserStreams[1].BufferSize=MAX_PATH*2;


		MINIDUMP_USER_STREAM_INFORMATION UserStreamInfo;
		UserStreamInfo.UserStreamArray=UserStreams;
		UserStreamInfo.UserStreamCount=2;

		CString filename = GetDumpFilename();
		HANDLE hFile = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
		
		::MiniDumpWriteDump(GetCurrentProcess(), 
			GetCurrentProcessId(), 
			hFile, 
			MiniDumpWithFullMemory,
			&ExceptionParam,
			&UserStreamInfo,
			NULL
			);

		return EXCEPTION_EXECUTE_HANDLER;
	}

	static void Setup()
	{
		::SetUnhandledExceptionFilter(SelfUnhandledExceptionFilter);

	}
};


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值