mrDarker
2025-09-12 e1dade1e252ffe20ba74a091ca9e6b78dae737bb
1. 添加程序唯一运行凭证
2. 添加程序闪退时的DUMP文件
已修改2个文件
89 ■■■■ 文件已修改
SourceCode/Bond/SGMeasurement/SGMeasurement.cpp 84 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
SourceCode/Bond/SGMeasurement/SGMeasurement.h 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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("MutexEdgeInspector_App"));
    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:“确定”来关闭对话框的代码
    }
    else if (nResponse == IDCANCEL)
    {
    else if (nResponse == IDCANCEL) {
        // TODO:“取消”来关闭对话框的代码 
    }
    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();
}
SourceCode/Bond/SGMeasurement/SGMeasurement.h
@@ -23,10 +23,13 @@
// 重写
public:
    virtual BOOL InitInstance();
    virtual int ExitInstance();
// 实现
    DECLARE_MESSAGE_MAP()
private:
    HANDLE m_hMutex;
};
extern CSGMeasurementApp theApp;