windows下使用dbghelp解决未知崩溃问题

文章提供了一种方法,通过Windows下的dbghelp库捕获和分析QT程序在不同环境下导致的未知崩溃问题。代码示例展示了如何生成dmp文件,并使用windbg进行调试,以定位和解决问题。代码已经处理了多字节字符集和Unicode字符集的兼容性,以及32和64位系统的适应性。

前言

编写QT程序后在自己电脑测试无问题,但是换一个电脑就会出现未知的崩溃问题,这种问题分析起来可不是紧靠加日志就可以解决的,所以需要借助windows下dbghelp来捕获崩溃时堆栈,生成dmp文件
在使用windbg进行分析

代码

以下代码已对多字节字符集和 Unicode 字符集进行了兼容,并且支持32和64位,可以直接将文件放入自己项目中,也可编译成动态库,但是需要注意动态库字符集及位数和工程使用的字符集和位数要一致。记得在链接器加入shlwapi.lib

// windump.h
#ifndef __WIN_DUMP_H__
#define __WIN_DUMP_H__

#include <string>
#include <atomic>

#if defined(_UNICODE) || defined(UNICODE)
using String = std::wstring;
#else
using String = std::string;
#endif //  defined(_UNICODE) || defined(UNICODE)

class WinDump
{
   
   
public:
	WinDump() {
   
   }
	~WinDump() {
   
   }

	static void CatchUnhandledException(const String &dmpFilePath);
};

#endif // __WIN_DUMP_H__

// windump.cpp
#pragma warning(disable: 4996)
#include "windump.h"
#include <mutex>
#include <time.h>
#include <Windows.h>
#include <DbgHelp.h>
#include <TlHelp32.h>
#include <Shlwapi.h>

//#if defined(_UNICODE) || defined(UNICODE)
//#error "not support UNICODE"
//#endif //  defined(_UNICODE) || defined(UNICODE)

#if defined(_UNICODE) || defined(UNICODE)
#define SPRINTF wsprintf
#define PRINTF  wprintf
#define _STR	"%ws"
#else</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值