1. 程式人生 > >程式崩潰時生成Dump檔案

程式崩潰時生成Dump檔案

Dump檔案是程序的記憶體映象,可以把程式執行時的狀態完整的儲存下來,之後通過除錯工具可查出崩潰大致原因。

  • SetUnhandledExceptionFilter()設定一個在程式崩潰時被呼叫的回撥函式。
  • MiniDumpWriteDump()建立Dump檔案。
我寫了一個CDumpFile類,程式崩潰時會生成“檔名_版本號 日期_時間.dmp”檔案,在App裡建立並呼叫它的Initialize()函式即可。

【DumpFile.h】

#pragma once
#include <Dbghelp.h>  

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

class CDumpFile
{
public:
	CDumpFile();
	~CDumpFile();

private:
	static LONG WINAPI UnhandledExceptionFilterEx(EXCEPTION_POINTERS* pException);
	static void CreateDumpFile(LPCTSTR lpstrDumpFilePathName, EXCEPTION_POINTERS* pException);
public:
	void Initialize(CString strFileDir = L""); // strFileDir為崩潰檔案儲存目錄,預設為模組所在目錄
};

【DumpFile.cpp】
#include "stdafx.h"
#include "DumpFile.h"

CDumpFile::CDumpFile()
{
}

CDumpFile::~CDumpFile()
{	
}

static WCHAR g_szDumpFileDir[MAX_PATH] = { 0 };

CString GetFileVersion(const CString& strFilePath)
{
	CString strAppVersion;
	DWORD u32RessourceVersionInfoSize;
	DWORD u32JustAJunkVariabel;
	char* ps8VersionInfoPtr;
	struct LANGANDCODEPAGE {
		WORD wLanguage;
		WORD wCodePage;
	} *pstTranslationPtr(nullptr);
	wchar_t* ps16InformationPtr;
	UINT  u32VersionInfoSize;
	wchar_t  as16VersionValue[255];

	u32RessourceVersionInfoSize = GetFileVersionInfoSize(strFilePath, &u32JustAJunkVariabel);
	if (0 != u32RessourceVersionInfoSize)
	{
		ps8VersionInfoPtr = new char[u32RessourceVersionInfoSize];
		if (GetFileVersionInfo(strFilePath, 0, u32RessourceVersionInfoSize, ps8VersionInfoPtr))
		{
			if (!VerQueryValue(
				ps8VersionInfoPtr,
				TEXT("VarFileInfo\\Translation"),
				(LPVOID*)&pstTranslationPtr,
				&u32VersionInfoSize))
			{
				delete[] ps8VersionInfoPtr;
				return strAppVersion;
			}
		}

		wsprintf(as16VersionValue,
			L"\\StringFileInfo\\%04x%04x\\FileVersion",
			pstTranslationPtr[0].wLanguage,
			pstTranslationPtr[0].wCodePage);

		if (!VerQueryValue(
			ps8VersionInfoPtr,
			as16VersionValue,
			(LPVOID*)&ps16InformationPtr,
			&u32VersionInfoSize))
		{
			delete[] ps8VersionInfoPtr;
			return strAppVersion;
		}

		if (wcslen(ps16InformationPtr) > 0)
		{
			strAppVersion = CString(ps16InformationPtr);
		}
		delete[] ps8VersionInfoPtr;
	}
	return strAppVersion;
}

void CDumpFile::Initialize(CString strFileDir)
{
	wcsncpy_s(g_szDumpFileDir, strFileDir, strFileDir.GetLength());

	SetUnhandledExceptionFilter(UnhandledExceptionFilterEx);
}

LONG WINAPI CDumpFile::UnhandledExceptionFilterEx(EXCEPTION_POINTERS* pException)
{
	WCHAR szModuleFilePath[MAX_PATH] = { 0 };
	GetModuleFileName(NULL, szModuleFilePath, MAX_PATH);

	CString strFileVersion = GetFileVersion(szModuleFilePath);

	WCHAR* pszModualFilename = PathFindFileName(szModuleFilePath);
	CString strFileName = CString(pszModualFilename) + L"_" + strFileVersion + CTime::GetCurrentTime().Format(" %Y-%m-%d %H_%M_%S") + L".dmp";

	if (wcslen(g_szDumpFileDir) == 0)
	{
		/* 儲存在模組所在目錄 */
		GetModuleFileName(NULL, g_szDumpFileDir, MAX_PATH);
		PathRemoveFileSpec(g_szDumpFileDir);
	}

	WCHAR szDumpFilePath[MAX_PATH] = { 0 };
	PathAppend(szDumpFilePath, g_szDumpFileDir);
	if (!PathIsDirectory(szDumpFilePath))
	{
		SHCreateDirectoryEx(NULL, szDumpFilePath, nullptr);
	}

	PathAppend(szDumpFilePath, strFileName);
	CreateDumpFile(szDumpFilePath, pException);

	// 崩潰時彈框  
	FatalAppExit(-1, _T("提示資訊:軟體崩潰,請重啟。"));
	return EXCEPTION_CONTINUE_SEARCH;
}

void CDumpFile::CreateDumpFile(LPCTSTR lpstrDumpFilePathName, EXCEPTION_POINTERS* pException)
{
	// 建立Dump檔案  
	HANDLE hDumpFile = CreateFile(lpstrDumpFilePathName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
	dumpInfo.ExceptionPointers = pException;
	dumpInfo.ThreadId = GetCurrentThreadId();
	dumpInfo.ClientPointers = TRUE;

	// 寫入Dump檔案內容  
	MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);

	CloseHandle(hDumpFile);
}
參考文章: