程序崩溃时生成DUMP文件

//首先,定义异常处理函数
LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo)
{
	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("DbgHelp.dll");
	if (hDbgHelp)
	{
		pfnMiniDumpWriteDump = (MiniDumpWriteDumpT)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
	}

	if (pfnMiniDumpWriteDump)
	{
		char szFileName[MAX_PATH] = { 0 };
		::GetModuleFileName(NULL, szFileName, _MAX_PATH);
		string str = szFileName;
		str = str.substr(0, str.length() - 4) + ".dmp";

		HANDLE hFile = ::CreateFile(str.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		if (hFile != INVALID_HANDLE_VALUE)
		{
			MINIDUMP_EXCEPTION_INFORMATION einfo;
			einfo.ThreadId = ::GetCurrentThreadId();
			einfo.ExceptionPointers = lpExceptionInfo;
			einfo.ClientPointers = FALSE;

			pfnMiniDumpWriteDump(::GetCurrentProcess(), ::GetCurrentProcessId(), hFile, MiniDumpNormal, &einfo, NULL, NULL);
			::CloseHandle(hFile);
		}

		return EXCEPTION_EXECUTE_HANDLER;
	}

	if (hDbgHelp != NULL)
	{
		FreeLibrary(hDbgHelp);
	}

	return EXCEPTION_EXECUTE_HANDLER;
}

//为程序设置异常处理函数
 
  
SetUnhandledExceptionFilter(ExceptionFilter);


 
  
 
 

你可能感兴趣的:(C++,Windows)