长风破浪会有时,直挂云帆济沧海

Dream Word

博客园 首页 新随笔 联系 订阅 管理

1:分析程序异常等等信息,在入口处初始化即可

//生成Dump文件信息 OS:Windows
#pragma once

#include <windows.h>
#include <imagehlp.h>
#if (_MSC_VER < 1700) // vs2010 and before version
#include <stdlib.h>
#else
#include <tchar.h>
#endif
#include <ctime>
#include <cstdio>

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


class MiniDump
{
public:
	MiniDump() = delete;
	~MiniDump() = delete;

	static LONG WINAPI RunUnhandledFilter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
	{
		LONG ret = EXCEPTION_EXECUTE_HANDLER;
		TCHAR szFileName[64];
		SYSTEMTIME st;
		::GetLocalTime(&st);
		std::srand(static_cast<unsigned int>(std::time(0)));
#if defined(UNICODE)
		swprintf_s(szFileName, L"%d-%d-%d-%d-%d-%d-%d-%d.dmp", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, std::rand());
#else
		sprintf_s(szFileName, "%d-%d-%d-%d-%d-%d-%d-%d.dmp", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, std::rand());
#endif

		HANDLE hFile = ::CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

		if (hFile != INVALID_HANDLE_VALUE)
		{
			MINIDUMP_EXCEPTION_INFORMATION ExInfo;
			ExInfo.ThreadId = ::GetCurrentThreadId();
			ExInfo.ExceptionPointers = lpExceptionInfo;
			ExInfo.ClientPointers = false;

			// write the dump
#if !defined(FINALIDEALSEE)
			BOOL bOK = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpWithFullMemory, &ExInfo, NULL, NULL);
#else
			BOOL bOK = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpNormal, &ExInfo, NULL, NULL);
#endif
			if (bOK)
			{
				printf("Create Dump File Success!\n");
			}
			else
			{
				printf("MiniDumpWriteDump Failed: %d\n", GetLastError());
			}

			::CloseHandle(hFile);
		}
		else
		{
#if defined(UNICODE)
			wprintf(L"Create File %ls Failed %d\n", szFileName, GetLastError());
#else
			printf("Create File %s Failed %d\n", szFileName, GetLastError());
#endif
		}
		//禁用对话框提示信息//
		//FatalAppExit(-1, _T("Fatal Error, Check Dump File"));

		return ret;
	}

	static void InitMinDump()
	{
		SetUnhandledExceptionFilter(RunUnhandledFilter);
	}
};

 

posted on 2017-10-12 21:37  长风II  阅读(351)  评论(0编辑  收藏  举报