编程语言
首页 > 编程语言> > C++:程序运行崩溃后生成dump文件

C++:程序运行崩溃后生成dump文件

作者:互联网

使用方法基本上是一个固定的模板,在main函数中调用一下就可以

主函数

#include<iostream>
#include"dump_lzb.h"

int main()
{
    //dump文件功能 
    SetUnhandledExceptionFilter(ExceptionFilter);
    std::cout << "dump功能测试" << std::endl;
    // 使程序崩溃产生 Dump 文件
    int* p = NULL;
    *p = 1;
}

 运行之后会在当前路径下生成一个文件夹

 dump_lzb.h头文件

#pragma once
#include <windows.h>
#include <DbgHelp.h>

LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo);

dump_lzb.cpp源文件

#include "dump_lzb.h"
#include<iostream>

int GenerateMiniDump(HANDLE hFile, PEXCEPTION_POINTERS pExceptionPointers, PWCHAR pwAppName)
{
	std::cout << "进入到GenerateMiniDump函数体内" << std::endl;
	BOOL bOwnDumpFile = FALSE;
	HANDLE hDumpFile = hFile;
	MINIDUMP_EXCEPTION_INFORMATION ExpParam;

	typedef BOOL(WINAPI* MiniDumpWriteDumpT)(
		HANDLE,
		DWORD,
		HANDLE,
		MINIDUMP_TYPE,
		PMINIDUMP_EXCEPTION_INFORMATION,
		PMINIDUMP_USER_STREAM_INFORMATION,
		PMINIDUMP_CALLBACK_INFORMATION
		);

	MiniDumpWriteDumpT pfnMiniDumpWriteDump = NULL;
	HMODULE hDbgHelp = LoadLibrary(L"DbgHelp.dll");
	if (hDbgHelp)
		pfnMiniDumpWriteDump = (MiniDumpWriteDumpT)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");

	if (pfnMiniDumpWriteDump)
	{
		if (hDumpFile == NULL || hDumpFile == INVALID_HANDLE_VALUE)
		{
			TCHAR szPath[MAX_PATH] = { 0 };
			TCHAR szFileName[MAX_PATH] = { 0 };
			TCHAR* szAppName = pwAppName;
			SYSTEMTIME stLocalTime;

			GetLocalTime(&stLocalTime);

			wsprintf(szPath, L"%s_DumpDir", szAppName);
			CreateDirectory(szPath, NULL);

			wsprintf(szFileName, L"%s//%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp",
				szPath,
				stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
				stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
				GetCurrentProcessId(), GetCurrentThreadId());
			hDumpFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE,
				FILE_SHARE_WRITE | FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);

			bOwnDumpFile = TRUE;
			OutputDebugString(szFileName);
		}

		if (hDumpFile != INVALID_HANDLE_VALUE)
		{
			ExpParam.ThreadId = GetCurrentThreadId();
			ExpParam.ExceptionPointers = pExceptionPointers;
			ExpParam.ClientPointers = FALSE;

			pfnMiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
				hDumpFile, MiniDumpWithDataSegs, (pExceptionPointers ? &ExpParam : NULL), NULL, NULL);

			if (bOwnDumpFile)
				CloseHandle(hDumpFile);
		}
	}

	if (hDbgHelp != NULL)
		FreeLibrary(hDbgHelp);
	std::cout << EXCEPTION_EXECUTE_HANDLER << std::endl;
	return EXCEPTION_EXECUTE_HANDLER;
}

LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo)
{
	if (IsDebuggerPresent())
	{
		std::cout << "跟踪位置" << std::endl;
		return EXCEPTION_CONTINUE_SEARCH;
	}
	return GenerateMiniDump(NULL, lpExceptionInfo, (PWCHAR)(L"BlendSim"));
}

标签:include,程序运行,dump,int,POINTERS,C++,lzb,cout
来源: https://blog.csdn.net/cd_yourheart/article/details/120219073