LAPTOP-SNT8I5JK\Boounion
2025-05-23 7c9d38f5bb9c1d9daa8ef7117779be7e1a4d883b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
 
// BoounionPLC.cpp : ¶¨ÒåÓ¦ÓóÌÐòµÄÀàÐÐΪ¡£
//
 
#include "stdafx.h"
#include "BoounionPLC.h"
#include "BoounionPLCDlg.h"
#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
 
BEGIN_MESSAGE_MAP(CBoounionPLCApp, CWinApp)
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
 
 
// CBoounionPLCApp ¹¹Ôì
 
CBoounionPLCApp::CBoounionPLCApp()
{
    // Ö§³ÖÖØÐÂÆô¶¯¹ÜÀíÆ÷
    m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
 
    // TODO: ÔÚ´Ë´¦Ìí¼Ó¹¹Ôì´úÂ룬
    // ½«ËùÓÐÖØÒªµÄ³õʼ»¯·ÅÖÃÔÚ InitInstance ÖÐ
    SetUnhandledExceptionFilter(MyUnhandledExceptionFilter);
}
 
 
// Î¨Ò»µÄÒ»¸ö CBoounionPLCApp ¶ÔÏó
 
CBoounionPLCApp theApp;
 
 
// CBoounionPLCApp ³õʼ»¯
 
BOOL CBoounionPLCApp::InitInstance()
{
    // Èç¹ûÒ»¸öÔËÐÐÔÚ Windows XP ÉϵÄÓ¦ÓóÌÐòÇåµ¥Ö¸¶¨Òª
    // Ê¹ÓàComCtl32.dll °æ±¾ 6 »ò¸ü¸ß°æ±¾À´ÆôÓÿÉÊÓ»¯·½Ê½£¬
    //ÔòÐèÒª InitCommonControlsEx()¡£  ·ñÔò£¬½«ÎÞ·¨´´½¨´°¿Ú¡£
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    // ½«ËüÉèÖÃΪ°üÀ¨ËùÓÐÒªÔÚÓ¦ÓóÌÐòÖÐʹÓõÄ
    // ¹«¹²¿Ø¼þÀà¡£
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);
 
    CWinApp::InitInstance();
 
 
    AfxEnableControlContainer();
 
    // ´´½¨ shell ¹ÜÀíÆ÷£¬ÒÔ·À¶Ô»°¿ò°üº¬
    // ÈκΠshell Ê÷ÊÓͼ¿Ø¼þ»ò shell ÁбíÊÓͼ¿Ø¼þ¡£
    CShellManager *pShellManager = new CShellManager;
 
    // ¼¤»î¡°Windows Native¡±ÊÓ¾õ¹ÜÀíÆ÷£¬ÒÔ±ãÔÚ MFC ¿Ø¼þÖÐÆôÓÃÖ÷Ìâ
    CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
 
    // ±ê×¼³õʼ»¯
    // Èç¹ûδʹÓÃÕâЩ¹¦Äܲ¢Ï£Íû¼õС
    // ×îÖÕ¿ÉÖ´ÐÐÎļþµÄ´óС£¬ÔòÓ¦ÒÆ³ýÏÂÁÐ
    // ²»ÐèÒªµÄÌØ¶¨³õʼ»¯Àý³Ì
    // ¸ü¸ÄÓÃÓÚ´æ´¢ÉèÖõÄ×¢²á±íÏî
    // TODO: Ó¦Êʵ±Ð޸ĸÃ×Ö·û´®£¬
    // ÀýÈçÐÞ¸ÄΪ¹«Ë¾»ò×éÖ¯Ãû
    SetRegistryKey(_T("Ó¦ÓóÌÐòÏòµ¼Éú³ÉµÄ±¾µØÓ¦ÓóÌÐò"));
 
 
    // ±¾³ÌÐòÎļþĿ¼
    TCHAR sDrive[_MAX_DRIVE];
    TCHAR sDir[_MAX_DIR];
    TCHAR sFilename[_MAX_FNAME], sAppFilename[_MAX_FNAME];
    TCHAR sExt[_MAX_EXT];
    GetModuleFileName(AfxGetInstanceHandle(), sAppFilename, _MAX_FNAME);
    _tsplitpath_s(sAppFilename, sDrive, sDir, sFilename, sExt);
    m_strAppDir = CString(sDrive) + CString(sDir);
    m_strAppFile = CString(sFilename);
    m_model.setWorkDir((LPTSTR)(LPCTSTR)m_strAppDir);
 
 
    // gdi+
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
 
 
    CString strIniFile;
    strIniFile.Format(_T("%s\\BoounionPLCconfiguration.ini"), (LPTSTR)(LPCTSTR)m_strAppDir);
    int plcTimeout = GetPrivateProfileInt("PLC", "Timeout", 700, strIniFile);
    int plcBufSize = GetPrivateProfileInt("PLC", "BufSize", 20, strIniFile);
    int plcPacketSize = GetPrivateProfileInt("PLC", "PacketSize", 80, strIniFile);
    MCL_Initialize();
    MCL_SetTimeout(plcTimeout);
    MCL_SetMaxActionSize(plcBufSize);
    MCL_SetPacketSize(plcPacketSize);
    RX_Init();
 
 
    m_hAppMutex = CreateMutexA(NULL, FALSE, _T("BoounionPLC"));
    if (GetLastError() == ERROR_ALREADY_EXISTS) {
        AfxMessageBox(_T("ÒѾ­ÔËÐÐÁËÒ»¸öʵÀý£¬Æô¶¯Ê§°Ü!"));
        return FALSE;
    }
 
 
    // ¿Ø¼þ×¢²á
    CHorizontalLine::RegisterWndClass();
    CVerticalLine::RegisterWndClass();
 
    
    CBoounionPLCDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
        // TODO: ÔÚ´Ë·ÅÖô¦ÀíºÎʱÓÃ
        //  ¡°È·¶¨¡±À´¹Ø±Õ¶Ô»°¿òµÄ´úÂë
    }
    else if (nResponse == IDCANCEL)
    {
        // TODO: ÔÚ´Ë·ÅÖô¦ÀíºÎʱÓÃ
        //  ¡°È¡Ïû¡±À´¹Ø±Õ¶Ô»°¿òµÄ´úÂë
    }
    else if (nResponse == -1)
    {
        TRACE(traceAppMsg, 0, "¾¯¸æ: ¶Ô»°¿ò´´½¨Ê§°Ü£¬Ó¦ÓóÌÐò½«ÒâÍâÖÕÖ¹¡£\n");
        TRACE(traceAppMsg, 0, "¾¯¸æ: Èç¹ûÄúÔÚ¶Ô»°¿òÉÏʹÓàMFC ¿Ø¼þ£¬ÔòÎÞ·¨ #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS¡£\n");
    }
 
    // É¾³ýÉÏÃæ´´½¨µÄ shell ¹ÜÀíÆ÷¡£
    if (pShellManager != NULL)
    {
        delete pShellManager;
    }
 
    // ÓÉÓÚ¶Ô»°¿òÒѹرգ¬ËùÒÔ½«·µ»Ø FALSE ÒÔ±ãÍ˳öÓ¦ÓóÌÐò£¬
    //  ¶ø²»ÊÇÆô¶¯Ó¦ÓóÌÐòµÄÏûÏ¢±Ã¡£
    return FALSE;
}
 
int CBoounionPLCApp::ExitInstance()
{
    // gdi+
    Gdiplus::GdiplusShutdown(m_gdiplusToken);
 
    m_model.term();
    RX_Term();
    MCL_Term();
    CloseHandle(m_hAppMutex);
 
    return CWinApp::ExitInstance();
}