ETradeClient.cpp
10.4 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// ETradeClient.cpp : Defines the class behaviors for the application.
#include "stdafx.h"
#include "afxwinappex.h"
#include "afxdialogex.h"
#include "ETradeClient.h"
#include "MainFrm.h"
#include "LoginDialog.h"
#include <boost/filesystem.hpp>
#include "etradeclient/browser/browser_app.h"
#include "etradeclient/browser/embedded_browser.h"
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/string_converter.h"
#include "./ExLib/mfc/dbghelp.h"
#include "./ETradeClient/utility/win_msg_define.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#pragma comment(lib, "libcef.lib")
#pragma comment(lib, "libcef_dll_wrapper.lib")
#pragma comment(lib, "dbghelp.lib")
// Set to 0 to disable sandbox support.
#define CEF_ENABLE_SANDBOX 0
#if CEF_ENABLE_SANDBOX
// The cef_sandbox.lib static library is currently built with VS2010. It may not
// link successfully with other VS versions.
#pragma comment(lib, "cef_sandbox.lib")
#endif
//异常处理 add by liuye 2017.2.20
LONG WINAPI ExceptionHandler(PEXCEPTION_POINTERS ExceptionPointer)
{
AfxMessageBox(_T("程序发生异常,请联系工作人员!"));
// Open the file
CTime tmCurTime;
tmCurTime = CTime::GetCurrentTime();
CString sData = tmCurTime.Format(_T("%y%m%d"));
CString sDumpName = _T("Dump_") + sData + _T(".dmp");
HANDLE hFile = CreateFile(sDumpName, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
{
// Create the minidump
MINIDUMP_EXCEPTION_INFORMATION stuDump;
stuDump.ThreadId = GetCurrentThreadId();
stuDump.ExceptionPointers = ExceptionPointer;
stuDump.ClientPointers = FALSE;
MINIDUMP_TYPE emDumpType = MiniDumpNormal;
BOOL bCreateDump = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, emDumpType, (ExceptionPointer != 0) ? &stuDump : 0, 0, 0);
if (!bCreateDump)
_tprintf(_T("MiniDumpWriteDump failed. Error: %u \n"), GetLastError());
else
_tprintf(_T("Minidump created.\n"));
// Close the file
CloseHandle(hFile);
CWinApp* pApp = AfxGetApp();
SendMessage(pApp->m_pMainWnd->m_hWnd, WM_CRASH_CLOSE, NULL, NULL);
}
else
{
_tprintf(_T("CreateFile failed. Error: %u \n"), GetLastError());
}
return EXCEPTION_EXECUTE_HANDLER;
}
//
// Replace application ID string below with unique ID string; recommended
// format for string is CompanyName.ProductName.SubProduct.VersionInformation
static const wchar_t* APP_ID = L"DILIGROUP.CentralClearingSystem.V2.0";
// CETradeClientApp
BEGIN_MESSAGE_MAP(CETradeClientApp, CWinApp)
ON_COMMAND(ID_APP_ABOUT, &CETradeClientApp::OnAppAbout)
END_MESSAGE_MAP()
// CETradeClientApp construction
CETradeClientApp::CETradeClientApp() : m_browser_app(nullptr), m_cef_initialized(false)
{
SetAppID(APP_ID);
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CETradeClientApp object
CETradeClientApp theApp;
// CETradeClientApp initialization
BOOL CETradeClientApp::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
/*SUPER IMPORTANT!!!
You can init the logging module before "InitializeCef", however, you SHOULD NOT do any logging before
"InitializeCef". Because "InitializeCef" will spawn multiprocess, if any logging were made before "InitializeCef",
each process will have it's own log file, thus multiple log files were created.
Further more, only the main process's log file contains complete logging records, other processes spawned by
"InitializeCef" only partially contains the logging records made before "InitializeCef".
*/
SetUnhandledExceptionFilter(ExceptionHandler);
const uint32_t kMaxLogFileSizeKB = 1024 * 1204; // Each log file stores maximum 100 kb.
const std::string kLogFileNamePrefix("logfile");
Logging::Init(Logging::Level::kTrace, kLogFileNamePrefix, kMaxLogFileSizeKB);
try
{
// CEF initialization may crash when closing a browser due to some uncertain CEF bug.
// This doesn't happen commonly and is very hard to replicate.
// We are not sure whether it's due to CEF's bug or some incorrect usage of CEF library.
// So we can only log this fatal error info and analyze later.
m_cef_initialized = InitializeCef();
}
catch (...)
{
LOG_FATAL(L"CEF初始化失败!程序关闭。");
return FALSE;
}
// 注册程序实例并检查是否已经存在运行中的实例,如果有则注册失败并退出,
// 该机制用于防止多个程序实例同时运行。
// !!!一定要程序实例注册放在初始化CEF(“InitializeCef”)之后
// 因为CEF自身会创建多个同名进程,如果将此机制放在CEF初始化之前,将导致CEF创建渲染进程失败,从而无法显示网页内容。
if (!m_instance_mgr.Register(APP_ID))
{
LOG_FATAL(L"无法同时运行多个程序实例,程序实例注册失败,程序未启动。");
return false;
}
LOG_TRACE(L"程序启动。"); // Start logging after the "InitializeCef" was called.
if (!m_cef_initialized)
{
LOG_ERROR(L"CEF初始化失败!程序关闭。");
return FALSE;
}
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
LOG_ERROR(L"初始化OLE库失败!程序关闭。");
return FALSE;
}
AfxEnableControlContainer();
EnableTaskbarInteraction(FALSE);
// AfxInitRichEdit2() is required to use RichEdit control
// AfxInitRichEdit2();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(APP_ID);
// First, do login job.
if (!CLoginDialog(L"欢迎使用综合业务系统!").Launch())
{
LOG_TRACE(L"登录系统未完成,程序关闭。");
return FALSE;
}
// To create the main window, this code creates a new frame window
// object and then sets it as the application's main window object
CMainFrame* main_frm = new CMainFrame;
if (!main_frm)
{
LOG_ERROR(L"系统内存不足!创建CMainFrame对象失败,程序关闭。");
return FALSE;
}
m_pMainWnd = main_frm;
// create and load the frame with its resources
main_frm->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
// Then launch the main window.
if (!main_frm->Launch())
{
LOG_ERROR(L"主窗口启动失败,程序关闭。");
return FALSE;
}
// The one and only window has been initialized, so show and update it
main_frm->ShowWindow(SW_SHOWMAXIMIZED);
main_frm->UpdateWindow();
return TRUE;
}
int CETradeClientApp::ExitInstance()
{
if (m_cef_initialized)
{
// closing stop work loop
m_cef_initialized = false;
// release CEF app
m_browser_app = nullptr;
// shutdown CEF
CefShutdown();
}
AfxOleTerm(FALSE);
m_instance_mgr.Unregister();
return CWinApp::ExitInstance();
}
//BOOL CETradeClientApp::PumpMessage()
//{
// if(m_cef_initialized)
// CefDoMessageLoopWork(); // Integrate with an existing application message loop instead of running its own message loop
// return CWinApp::PumpMessage();
//}
bool CETradeClientApp::InitializeCef()
{
void* sandbox_info = NULL;
#if CEF_ENABLE_SANDBOX
// Manage the life span of the sandbox information object. This is necessary
// for sandbox support on Windows. See cef_sandbox_win.h for complete details.
CefScopedSandboxInfo scoped_sandbox;
sandbox_info = scoped_sandbox.sandbox_info();
#endif
m_browser_app = new BrowserApp();
CefMainArgs main_args(GetModuleHandle(NULL)); // get arguments
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, m_browser_app.get(), NULL);
if (exit_code >= 0)
return false; // Indicate the initialization is done and should not continue to next step.
namespace fs = boost::filesystem;
wchar_t exe_path[MAX_PATH];
HMODULE h_module = GetModuleHandle(NULL);
if (NULL != h_module)
GetModuleFileName(h_module, exe_path, sizeof(exe_path));
else
throw std::exception("Calling windows API to get exe path failed.");
std::string exe_path_ = wstr_2_str(fs::wpath(exe_path).parent_path().wstring());
// setup settings
CefSettings settings;
settings.command_line_args_disabled = true; // Won't accept command line argument.
settings.multi_threaded_message_loop = true; // Set to true to have the browser process message loop run in a separate thread.
CefString(&settings.log_file) = exe_path_ + "\\cef\\cef.log";
CefString(&settings.resources_dir_path) = exe_path_ + "\\cef\\Resources"; // WARNING: 'resources_dir_path' has to be absoulute path.
CefString(&settings.locales_dir_path) = exe_path_ + "\\cef\\Resources\\locales";
// 不使用缓存,避免在某些环境下,cef无法使用缓存路径下的数据,造成页面无法显示,注意: 如果缓存路径设置为空,将不支持HTML5 LocalStorage。
CefString(&settings.cache_path) = "";
return CefInitialize(main_args, settings, m_browser_app.get(), sandbox_info);
//@}
}
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// App command to run the dialog
void CETradeClientApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
// CETradeClientApp message handlers
// InstanceManager
CETradeClientApp::InstanceManager::InstanceManager() : m_mutex_handle(nullptr)
{}
CETradeClientApp::InstanceManager::~InstanceManager()
{
Unregister();
}
bool CETradeClientApp::InstanceManager::Register(const std::wstring& app_id)
{
HANDLE hHandle = CreateMutex(NULL, TRUE, app_id.c_str());
if (ERROR_ALREADY_EXISTS == GetLastError())
{
// Log
return false;
}
m_mutex_handle = hHandle;
return true;
}
void CETradeClientApp::InstanceManager::Unregister()
{
if (nullptr != m_mutex_handle)
{
ReleaseMutex(m_mutex_handle); // Explicitly release mutex
CloseHandle(m_mutex_handle); // close handle before terminating
m_mutex_handle = nullptr;
}
}