码迷,mamicode.com
首页 > 编程语言 > 详细

C++软件添加dump调试打印日志

时间:2016-06-28 07:04:59      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

#include <DbgHelp.h>

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

 

LONG WINAPI TopLevelExceptionFilter(struct _EXCEPTION_POINTERS *pExceptionInfo) 
{
       //cout << "Enter TopLevelExceptionFilter Function" << endl; 
   TCHAR szPath[MAX_PATH] = { 0 };
   TCHAR szFileName[MAX_PATH] = { 0 };
   TCHAR* szVersion = "v1.0";
   TCHAR dwBufferSize = MAX_PATH;
   SYSTEMTIME stLocalTime;
   GetLocalTime(&stLocalTime);
   CreateDirectory(szFileName, NULL);
   StringCchPrintf(szFileName, MAX_PATH, "%s-%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp",
   szVersion, stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,
   stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond,
   GetCurrentProcessId(), GetCurrentThreadId());

     HANDLE hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
     MINIDUMP_EXCEPTION_INFORMATION stExceptionParam; 
       stExceptionParam.ThreadId    = GetCurrentThreadId(); 
       stExceptionParam.ExceptionPointers = pExceptionInfo; 
       stExceptionParam.ClientPointers    = FALSE; 
       MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpWithFullMemory, &stExceptionParam, NULL, NULL); 
       CloseHandle(hFile); 
       getchar();

       return EXCEPTION_EXECUTE_HANDLER;
}

//-----------------------------------------------------------------------------------------

在程序最开始的地方调用这个函数

SetUnhandledExceptionFilter(TopLevelExceptionFilter);

C++软件添加dump调试打印日志

标签:

原文地址:http://www.cnblogs.com/makyiox/p/5622064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!