C++:程序运行崩溃后生成dump文件
2021/9/10 12:04:51
本文主要是介绍C++:程序运行崩溃后生成dump文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
使用方法基本上是一个固定的模板,在main函数中调用一下就可以
主函数
#include<iostream> #include"dump_lzb.h" int main() { //dump文件功能 SetUnhandledExceptionFilter(ExceptionFilter); std::cout << "dump功能测试" << std::endl; // 使程序崩溃产生 Dump 文件 int* p = NULL; *p = 1; }
运行之后会在当前路径下生成一个文件夹
dump_lzb.h头文件
#pragma once #include <windows.h> #include <DbgHelp.h> LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo);
dump_lzb.cpp源文件
#include "dump_lzb.h" #include<iostream> int GenerateMiniDump(HANDLE hFile, PEXCEPTION_POINTERS pExceptionPointers, PWCHAR pwAppName) { std::cout << "进入到GenerateMiniDump函数体内" << std::endl; 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; SYSTEMTIME stLocalTime; GetLocalTime(&stLocalTime); wsprintf(szPath, L"%s_DumpDir", szAppName); CreateDirectory(szPath, NULL); wsprintf(szFileName, L"%s//%04d%02d%02d-%02d%02d%02d-%ld-%ld.dmp", szPath, 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); std::cout << EXCEPTION_EXECUTE_HANDLER << std::endl; return EXCEPTION_EXECUTE_HANDLER; } LONG WINAPI ExceptionFilter(LPEXCEPTION_POINTERS lpExceptionInfo) { if (IsDebuggerPresent()) { std::cout << "跟踪位置" << std::endl; return EXCEPTION_CONTINUE_SEARCH; } return GenerateMiniDump(NULL, lpExceptionInfo, (PWCHAR)(L"BlendSim")); }
这篇关于C++:程序运行崩溃后生成dump文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-10Rakuten 乐天积分系统从 Cassandra 到 TiDB 的选型与实战
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用