From 65ac403060f715a532e5a31927fecbfac208c905 Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期五, 10 一月 2025 10:36:16 +0800
Subject: [PATCH] 1.添加.dmp文件保存 2.添加删除最后一个PLC时销毁PLC View窗口

---
 SourceCode/Bond/BoounionPLC/BoounionPLC.cpp |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/SourceCode/Bond/BoounionPLC/BoounionPLC.cpp b/SourceCode/Bond/BoounionPLC/BoounionPLC.cpp
index b528c1f..3f49216 100644
--- a/SourceCode/Bond/BoounionPLC/BoounionPLC.cpp
+++ b/SourceCode/Bond/BoounionPLC/BoounionPLC.cpp
@@ -8,11 +8,60 @@
 #include "HorizontalLine.h"
 #include "VerticalLine.h"
 
+#include <DbgHelp.h>
+#pragma comment(lib, "DbgHelp.lib")
 
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #endif
 
+LONG WINAPI MyUnhandledExceptionFilter(EXCEPTION_POINTERS* pExceptionInfo) {
+	SYSTEMTIME st;
+	GetLocalTime(&st);
+
+	// 获取当前程序运行目录
+	char szAppPath[MAX_PATH];
+	GetModuleFileName(NULL, szAppPath, MAX_PATH);
+	std::string strCurrentDir = szAppPath;
+	size_t lastSlash = strCurrentDir.find_last_of("\\/");
+	strCurrentDir = strCurrentDir.substr(0, lastSlash);
+
+	// 检查Temp文件夹
+	struct stat buffer;
+	std::string strFullTempPath = strCurrentDir + "\\Temp";
+	if (stat(strFullTempPath.c_str(), &buffer) != 0) {
+		if (!CreateDirectory(strFullTempPath.c_str(), NULL)) {
+			return EXCEPTION_EXECUTE_HANDLER;
+		}
+	}
+
+	// 构建文件名
+	char szFileName[MAX_PATH];
+	snprintf(szFileName, MAX_PATH, "\\BoounionPLC_%04d%02d%02d_%02d%02d%02d.dmp",
+		st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
+
+	// 写入DMP文件
+	std::string strFullPath = strFullTempPath + szFileName;
+	HANDLE hFile = CreateFile(strFullPath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+	if (hFile != INVALID_HANDLE_VALUE) {
+		MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
+		dumpInfo.ExceptionPointers = pExceptionInfo;
+		dumpInfo.ThreadId = GetCurrentThreadId();
+		dumpInfo.ClientPointers = TRUE;
+
+		// 写入dump文件
+		BOOL success = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpWithFullMemory, &dumpInfo, NULL, NULL);
+		CloseHandle(hFile);
+
+		if (success) {
+			CString strMsg;
+			strMsg.Format(_T("Software crashes. Dump file: %s"), strFullPath.c_str());
+			AfxMessageBox(strMsg);
+		}
+	}
+
+	return EXCEPTION_EXECUTE_HANDLER;
+}
 
 // CBoounionPLCApp
 
@@ -30,6 +79,7 @@
 
 	// TODO: 在此处添加构造代码,
 	// 将所有重要的初始化放置在 InitInstance 中
+	SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
 }
 
 

--
Gitblit v1.9.3