1. 程式人生 > >c++ 寫dump檔案

c++ 寫dump檔案

//必不可少的標頭檔案和lib庫檔案
#include "dbghelp.h"
#pragma comment(lib,"dbghelp.lib")


//異常捕獲函式
LONG WINAPI UnhandledExceptionFunction(struct _EXCEPTION_POINTERS* pExceptionInfo)
{
	CTime timeCur = CTime::GetCurrentTime();

        //dump檔案路徑
	CString strCrashFilesPath = _T();
	
	HANDLE hDumpFile = CreateFile(strCrashFilesPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
	dumpInfo.ExceptionPointers = pExceptionInfo;
	dumpInfo.ThreadId = GetCurrentThreadId();
	dumpInfo.ClientPointers = TRUE;

	MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
	CloseHandle(hDumpFile);

	ExitProcess(0);
	return 1;
}


在程式的入口處添加註冊函式,比如mfc對話方塊的APP::InitInstance()中添加註冊(如果是純c++程式碼,可以在main()中添加註冊):
SetUnhandledExceptionFilter(UnhandledExceptionFunction);