我们经常会遇到崩溃,此时我们需要定位问题再什么地方,这时候我们就会需要用到dump文件
方式一:
#include "Windows.h"
#include "DbgHelp.h"
#pragma comment(lib, "dbghelp.lib")int GenerateMiniDump(PEXCEPTION_POINTERS pExceptionPointers)
{ QString dmpFileName = QString("%1.dmp").arg(QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss"));QString dmpFile = QString("%1/%2").arg(QApplication::applicationDirPath()).arg(dmpFileName);//将QString 转为 const char* HANDLE hDumpFile = CreateFileW(dmpFile .toStdWString().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);if (hDumpFile != INVALID_HANDLE_VALUE){MINIDUMP_EXCEPTION_INFORMATION dumpInfo;dumpInfo.ThreadId = GetCurrentThreadId();dumpInfo.ExceptionPointers = pExceptionPointers;dumpInfo.ClientPointers = TRUE;// 创建Dump文件MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpWithDataSegs, pExceptionPointers ? &dumpInfo : nullptr, nullptr, nullptr);CloseHandle(hDumpFile);}return EXCEPTION_EXECUTE_HANDLER;
}LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo)
{// 这里做一些异常的过滤或提示if (IsDebuggerPresent()){return EXCEPTION_CONTINUE_SEARCH;}return GenerateMiniDump(lpExceptionInfo);
}int main()
{SetUnhandledExceptionFilter(ExceptionFilter);return 0;
}
方式二:
#include "Windows.h"
#include "DbgHelp.h"int GenerateMiniDump(PEXCEPTION_POINTERS pExceptionPointers)
{
// 定义函数指针typedef BOOL(WINAPI* MiniDumpWriteDumpT)(HANDLE,DWORD,HANDLE,MINIDUMP_TYPE,PMINIDUMP_EXCEPTION_INFORMATION,PMINIDUMP_USER_STREAM_INFORMATION,PMINIDUMP_CALLBACK_INFORMATION);// 从 "DbgHelp.dll" 库中获取 "MiniDumpWriteDump" 函数MiniDumpWriteDumpT pfnMiniDumpWriteDump = NULL;HMODULE hDbgHelp = LoadLibrary(_T("DbgHelp.dll"));if (NULL == hDbgHelp){return EXCEPTION_CONTINUE_EXECUTION;}pfnMiniDumpWriteDump = (MiniDumpWriteDumpT)GetProcAddress(hDbgHelp, "MiniDumpWriteDump");if (NULL == pfnMiniDumpWriteDump){FreeLibrary(hDbgHelp);return EXCEPTION_CONTINUE_EXECUTION;}// 创建 dmp 文件件TCHAR szFileName[MAX_PATH] = { 0 };TCHAR* szVersion = _T("DumpFile");SYSTEMTIME stLocalTime;GetLocalTime(&stLocalTime);wsprintf(szFileName, _T("%s-%04d%02d%02d-%02d%02d%02d.dmp"),szVersion, stLocalTime.wYear, stLocalTime.wMonth, stLocalTime.wDay,stLocalTime.wHour, stLocalTime.wMinute, stLocalTime.wSecond);HANDLE hDumpFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE,FILE_SHARE_WRITE | FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0);if (INVALID_HANDLE_VALUE == hDumpFile){FreeLibrary(hDbgHelp);return EXCEPTION_CONTINUE_EXECUTION;}// 写入 dmp 文件MINIDUMP_EXCEPTION_INFORMATION expParam;expParam.ThreadId = GetCurrentThreadId();expParam.ExceptionPointers = pExceptionPointers;expParam.ClientPointers = FALSE;pfnMiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),hDumpFile, MiniDumpWithDataSegs, (pExceptionPointers ? &expParam : NULL), NULL, NULL);// 释放文件CloseHandle(hDumpFile);FreeLibrary(hDbgHelp);return EXCEPTION_EXECUTE_HANDLER;
}LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo)
{// 这里做一些异常的过滤或提示if (IsDebuggerPresent()){return EXCEPTION_CONTINUE_SEARCH;}return GenerateMiniDump(lpExceptionInfo);
}int main()
{SetUnhandledExceptionFilter(ExceptionFilter);return 0;
}
这两种都是差不生成 dump文件 区别在于
第一个是直接调用 dbghelp.lib,然后使用c++实行文件名的形式
第二个是 动态调用 dbghelp.lib,使用QT 实现 文件名的形式
参考博客:
dump文件生成二
dump文件生成一