From 829fe6c6bc33d53fda9c31fd45a37e1df87befff Mon Sep 17 00:00:00 2001
From: mrDarker <mr.darker@163.com>
Date: 星期五, 30 一月 2026 11:16:24 +0800
Subject: [PATCH] Merge branch 'clh' into liuyang

---
 SourceCode/Bond/SGMeasurement/SGMeasurement.cpp |   84 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 76 insertions(+), 8 deletions(-)

diff --git a/SourceCode/Bond/SGMeasurement/SGMeasurement.cpp b/SourceCode/Bond/SGMeasurement/SGMeasurement.cpp
index 2c3c2e6..fe11f5b 100644
--- a/SourceCode/Bond/SGMeasurement/SGMeasurement.cpp
+++ b/SourceCode/Bond/SGMeasurement/SGMeasurement.cpp
@@ -7,10 +7,61 @@
 #include "SGMeasurement.h"
 #include "SGMeasurementDlg.h"
 
+#include <DbgHelp.h>
+#pragma comment(lib, "DbgHelp.lib")
+
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #endif
 
+LONG WINAPI MyUnhandledExceptionFilter(EXCEPTION_POINTERS* pExceptionInfo)
+{
+	// 鑾峰彇褰撳墠绋嬪簭鐩綍
+	TCHAR szAppPath[MAX_PATH] = { 0 };
+	::GetModuleFileName(NULL, szAppPath, MAX_PATH);
+	PathRemoveFileSpec(szAppPath);
+
+	// 鏋勯�� Temp 鏂囦欢澶硅矾寰�
+	CString strTempPath;
+	strTempPath.Format(_T("%s\\Temp"), szAppPath);
+	CreateDirectory(strTempPath, NULL);
+
+	// 鐢熸垚甯︽椂闂存埑鐨� Dump 鏂囦欢鍚�
+	SYSTEMTIME st;
+	GetLocalTime(&st);
+
+	CString strDumpFile;
+	strDumpFile.Format(
+		_T("%s\\SG_%04d%02d%02d_%02d%02d%02d.dmp"),
+		strTempPath,
+		st.wYear, st.wMonth, st.wDay,
+		st.wHour, st.wMinute, st.wSecond
+	);
+
+	// 鎵撳紑 dump 鏂囦欢
+	HANDLE hFile = CreateFile(strDumpFile, 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);
+	}
+
+	return EXCEPTION_EXECUTE_HANDLER;
+}
 
 // CSGMeasurementApp
 
@@ -51,6 +102,18 @@
 
 	CWinApp::InitInstance();
 
+	// 鍞竴瀹炰緥杩愯妫�娴�
+	m_hMutex = ::CreateMutex(NULL, FALSE, _T("MutexSGMeasurementApp"));
+	if (m_hMutex != NULL) {
+		if (::GetLastError() == ERROR_ALREADY_EXISTS) {
+			AfxMessageBox(_T("The Program is already running. Exit this Program."), MB_OK | MB_ICONERROR);
+			return FALSE;
+		}
+	}
+
+	// 璁剧疆鏈鐞嗗紓甯歌繃婊ゅ櫒锛屾崟鑾峰穿婧冨苟鐢熸垚 Dump 鏂囦欢
+	SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
+
 	// 鍒濆鍖� MFC RichEdit 鎺т欢
 	AfxInitRichEdit2();
 
@@ -75,23 +138,19 @@
 	CSGMeasurementDlg dlg;
 	m_pMainWnd = &dlg;
 	INT_PTR nResponse = dlg.DoModal();
-	if (nResponse == IDOK)
-	{
+	if (nResponse == IDOK) {
 		// TODO:鈥滅‘瀹氣�濇潵鍏抽棴瀵硅瘽妗嗙殑浠g爜
 	}
-	else if (nResponse == IDCANCEL)
-	{
+	else if (nResponse == IDCANCEL) {
 		// TODO:鈥滃彇娑堚�濇潵鍏抽棴瀵硅瘽妗嗙殑浠g爜 
 	}
-	else if (nResponse == -1)
-	{
+	else if (nResponse == -1) {
 		TRACE(traceAppMsg, 0, "璀﹀憡: 瀵硅瘽妗嗗垱寤哄け璐ワ紝搴旂敤绋嬪簭灏嗘剰澶栫粓姝€�俓n");
 		TRACE(traceAppMsg, 0, "璀﹀憡: 濡傛灉鎮ㄥ湪瀵硅瘽妗嗕笂浣跨敤 MFC 鎺т欢锛屽垯鏃犳硶 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS銆俓n");
 	}
 
 	// 鍒犻櫎涓婇潰鍒涘缓鐨� shell 绠$悊鍣ㄣ��
-	if (pShellManager != nullptr)
-	{
+	if (pShellManager != nullptr) {
 		delete pShellManager;
 	}
 
@@ -101,4 +160,13 @@
 
 	// 鐢变簬瀵硅瘽妗嗗凡鍏抽棴锛屾墍浠ュ皢杩斿洖 FALSE 浠ヤ究閫�鍑哄簲鐢ㄧ▼搴忥紝鑰屼笉鏄惎鍔ㄥ簲鐢ㄧ▼搴忕殑娑堟伅娉点��
 	return FALSE;
+}
+
+int CSGMeasurementApp::ExitInstance()
+{
+	if (m_hMutex) {
+		CloseHandle(m_hMutex);
+		m_hMutex = nullptr;
+	}
+	return CWinApp::ExitInstance();
 }
\ No newline at end of file

--
Gitblit v1.9.3