zoukankan      html  css  js  c++  java
  • C++ 程序崩溃时生成Dump文件

    #include <DbgHelp.h>
    
    //生产DUMP文件
    int GenerateMiniDump(HANDLE hFile, PEXCEPTION_POINTERS pExceptionPointers, PWCHAR pwAppName)
    {
    	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;
    			TCHAR* szVersion = L"v1.0";
    			TCHAR dwBufferSize = MAX_PATH;
    			SYSTEMTIME stLocalTime;
    
    			GetLocalTime(&stLocalTime);
    			//GetTempPath(dwBufferSize, szPath);
    
    			//wsprintf(szFileName, L"%s%s", szPath, szAppName);
    			CreateDirectory(szFileName, NULL);
    
    			wsprintf(szFileName, L"%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp",
    				//szPath, szAppName, szVersion,
    				szVersion,
    				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);
    
    	return EXCEPTION_EXECUTE_HANDLER;
    }
    
    
    LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo)
    {
    	if (IsDebuggerPresent())
    	{
    		return EXCEPTION_CONTINUE_SEARCH;
    	}
    
    	return GenerateMiniDump(NULL, lpExceptionInfo, L"test");
    }
    
    
    int main()
    {
            //加入崩溃dump文件功能 
    	SetUnhandledExceptionFilter(ExceptionFilter);
    }
    
  • 相关阅读:
    如何在DOS中枚举PCI设备
    [Color]深入学习YCbCr色彩模型
    [Imm]Imm API学习笔记——输入法属性
    VBE_INFO(获取VBE信息)
    用VB写高效的图像处理程序 V2.0(2006524)
    ANSI环境下支持多语言输入的单行文本编辑器 V0.01
    分析外星人计算Pi的程序
    位运算模块mBit.bas
    [FileFormat]用VB写的高速GIF、JPEG 编码/解码 程序
    ANTLR笔记3 ANTLRWorks
  • 原文地址:https://www.cnblogs.com/jkcx/p/6396270.html
Copyright © 2011-2022 走看看