//ETradeClient.cpp:Definestheclassbehaviorsfortheapplication.#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#definenewDEBUG_NEW#endif#pragmacomment(lib,"libcef.lib")#pragmacomment(lib,"libcef_dll_wrapper.lib")#pragmacomment(lib,"dbghelp.lib")//Setto0todisablesandboxsupport.#defineCEF_ENABLE_SANDBOX0#ifCEF_ENABLE_SANDBOX//Thecef_sandbox.libstaticlibraryiscurrentlybuiltwithVS2010.Itmaynot//linksuccessfullywithotherVSversions.#pragmacomment(lib,"cef_sandbox.lib")#endif//异常处理addbyliuye2017.2.20LONGWINAPIExceptionHandler(PEXCEPTION_POINTERSExceptionPointer){AfxMessageBox(_T("程序发生异常,请联系工作人员!"));//OpenthefileCTimetmCurTime;tmCurTime=CTime::GetCurrentTime();CStringsData=tmCurTime.Format(_T("%y%m%d"));CStringsDumpName=_T("Dump_")+sData+_T(".dmp");HANDLEhFile=CreateFile(sDumpName,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);if((hFile!=NULL)&&(hFile!=INVALID_HANDLE_VALUE)){//CreatetheminidumpMINIDUMP_EXCEPTION_INFORMATIONstuDump;stuDump.ThreadId=GetCurrentThreadId();stuDump.ExceptionPointers=ExceptionPointer;stuDump.ClientPointers=FALSE;MINIDUMP_TYPEemDumpType=MiniDumpWithFullMemory;BOOLbCreateDump=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"));//ClosethefileCloseHandle(hFile);CWinApp*pApp=AfxGetApp();SendMessage(pApp->m_pMainWnd->m_hWnd,WM_CRASH_CLOSE,NULL,NULL);}else{_tprintf(_T("CreateFile failed. Error: %u \n"),GetLastError());}returnEXCEPTION_EXECUTE_HANDLER;}////ReplaceapplicationIDstringbelowwithuniqueIDstring;recommended//formatforstringisCompanyName.ProductName.SubProduct.VersionInformationstaticconstwchar_t*APP_ID=L"DILIGROUP.ETradeClient.V1.0";//CETradeClientAppBEGIN_MESSAGE_MAP(CETradeClientApp,CWinApp)ON_COMMAND(ID_APP_ABOUT,&CETradeClientApp::OnAppAbout)END_MESSAGE_MAP()//CETradeClientAppconstructionCETradeClientApp::CETradeClientApp():m_browser_app(nullptr),m_cef_initialized(false){SetAppID(APP_ID);//TODO:addconstructioncodehere,//PlaceallsignificantinitializationinInitInstance}//TheoneandonlyCETradeClientAppobjectCETradeClientApptheApp;//CETradeClientAppinitializationBOOLCETradeClientApp::InitInstance(){//InitCommonControlsEx()isrequiredonWindowsXPifanapplication//manifestspecifiesuseofComCtl32.dllversion6orlatertoenable//visualstyles.Otherwise,anywindowcreationwillfail.INITCOMMONCONTROLSEXInitCtrls;InitCtrls.dwSize=sizeof(InitCtrls);//Setthistoincludeallthecommoncontrolclassesyouwanttouse//inyourapplication.InitCtrls.dwICC=ICC_WIN95_CLASSES;InitCommonControlsEx(&InitCtrls);CWinApp::InitInstance();/*SUPERIMPORTANT!!!Youcaninittheloggingmodulebefore"InitializeCef",however,youSHOULDNOTdoanyloggingbefore"InitializeCef".Because"InitializeCef"willspawnmultiprocess,ifanyloggingweremadebefore"InitializeCef",eachprocesswillhaveit'sownlogfile,thusmultiplelogfileswerecreated.Furthermore,onlythemainprocess'slogfilecontainscompleteloggingrecords,otherprocessesspawnedby"InitializeCef"onlypartiallycontainstheloggingrecordsmadebefore"InitializeCef".*/SetUnhandledExceptionFilter(ExceptionHandler);constuint32_tkMaxLogFileSizeKB=1024*1204;//Eachlogfilestoresmaximum100kb.conststd::stringkLogFileNamePrefix("logfile");Logging::Init(Logging::Level::kTrace,kLogFileNamePrefix,kMaxLogFileSizeKB);try{//CEFinitializationmaycrashwhenclosingabrowserduetosomeuncertainCEFbug.//Thisdoesn'thappencommonlyandisveryhardtoreplicate.//Wearenotsurewhetherit'sduetoCEF'sbugorsomeincorrectusageofCEFlibrary.//Sowecanonlylogthisfatalerrorinfoandanalyzelater.m_cef_initialized=InitializeCef();}catch(...){LOG_FATAL(L"CEF初始化失败!程序关闭。");returnFALSE;}//注册程序实例并检查是否已经存在运行中的实例,如果有则注册失败并退出,//该机制用于防止多个程序实例同时运行。//!!!一定要程序实例注册放在初始化CEF(“InitializeCef”)之后//因为CEF自身会创建多个同名进程,如果将此机制放在CEF初始化之前,将导致CEF创建渲染进程失败,从而无法显示网页内容。if(!m_instance_mgr.Register(APP_ID)){LOG_FATAL(L"无法同时运行多个程序实例,程序实例注册失败,程序未启动。");returnfalse;}LOG_TRACE(L"程序启动。");//Startloggingafterthe"InitializeCef"wascalled.if(!m_cef_initialized){LOG_ERROR(L"CEF初始化失败!程序关闭。");returnFALSE;}//InitializeOLElibrariesif(!AfxOleInit()){AfxMessageBox(IDP_OLE_INIT_FAILED);LOG_ERROR(L"初始化OLE库失败!程序关闭。");returnFALSE;}AfxEnableControlContainer();EnableTaskbarInteraction(FALSE);//AfxInitRichEdit2()isrequiredtouseRichEditcontrol//AfxInitRichEdit2();//Standardinitialization//Ifyouarenotusingthesefeaturesandwishtoreducethesize//ofyourfinalexecutable,youshouldremovefromthefollowing//thespecificinitializationroutinesyoudonotneed//Changetheregistrykeyunderwhichoursettingsarestored//TODO:Youshouldmodifythisstringtobesomethingappropriate//suchasthenameofyourcompanyororganizationSetRegistryKey(APP_ID);//First,dologinjob.if(!CLoginDialog(L"欢迎使用地利电子交易结算柜员系统!").Launch()){LOG_TRACE(L"登录系统未完成,程序关闭。");returnFALSE;}//Tocreatethemainwindow,thiscodecreatesanewframewindow//objectandthensetsitastheapplication'smainwindowobjectCMainFrame*main_frm=newCMainFrame;if(!main_frm){LOG_ERROR(L"系统内存不足!创建CMainFrame对象失败,程序关闭。");returnFALSE;}m_pMainWnd=main_frm;//createandloadtheframewithitsresourcesmain_frm->LoadFrame(IDR_MAINFRAME,WS_OVERLAPPEDWINDOW|FWS_ADDTOTITLE,NULL,NULL);//Thenlaunchthemainwindow.if(!main_frm->Launch()){LOG_ERROR(L"主窗口启动失败,程序关闭。");returnFALSE;}//Theoneandonlywindowhasbeeninitialized,soshowandupdateitmain_frm->ShowWindow(SW_SHOWMAXIMIZED);main_frm->UpdateWindow();returnTRUE;}intCETradeClientApp::ExitInstance(){if(m_cef_initialized){//closingstopworkloopm_cef_initialized=false;//releaseCEFappm_browser_app=nullptr;//shutdownCEFCefShutdown();}AfxOleTerm(FALSE);m_instance_mgr.Unregister();returnCWinApp::ExitInstance();}//BOOLCETradeClientApp::PumpMessage()//{//if(m_cef_initialized)//CefDoMessageLoopWork();//Integratewithanexistingapplicationmessageloopinsteadofrunningitsownmessageloop//returnCWinApp::PumpMessage();//}boolCETradeClientApp::InitializeCef(){void*sandbox_info=NULL;#ifCEF_ENABLE_SANDBOX//Managethelifespanofthesandboxinformationobject.Thisisnecessary//forsandboxsupportonWindows.Seecef_sandbox_win.hforcompletedetails.CefScopedSandboxInfoscoped_sandbox;sandbox_info=scoped_sandbox.sandbox_info();#endifm_browser_app=newBrowserApp();CefMainArgsmain_args(GetModuleHandle(NULL));//getarguments//Executethesecondaryprocess,ifany.intexit_code=CefExecuteProcess(main_args,m_browser_app.get(),NULL);if(exit_code>=0)returnfalse;//Indicatetheinitializationisdoneandshouldnotcontinuetonextstep.namespacefs=boost::filesystem;wchar_texe_path[MAX_PATH];HMODULEh_module=GetModuleHandle(NULL);if(NULL!=h_module)GetModuleFileName(h_module,exe_path,sizeof(exe_path));elsethrowstd::exception("Calling windows API to get exe path failed.");std::stringexe_path_=wstr_2_str(fs::wpath(exe_path).parent_path().wstring());//setupsettingsCefSettingssettings;settings.command_line_args_disabled=true;//Won'tacceptcommandlineargument.settings.multi_threaded_message_loop=true;//Settotruetohavethebrowserprocessmessageloopruninaseparatethread.CefString(&settings.log_file)=exe_path_+"\\cef\\cef.log";CefString(&settings.resources_dir_path)=exe_path_+"\\cef\\Resources";//WARNING:'resources_dir_path'hastobeabsoulutepath.CefString(&settings.locales_dir_path)=exe_path_+"\\cef\\Resources\\locales";//不使用缓存,避免在某些环境下,cef无法使用缓存路径下的数据,造成页面无法显示,注意:如果缓存路径设置为空,将不支持HTML5LocalStorage。CefString(&settings.cache_path)="";returnCefInitialize(main_args,settings,m_browser_app.get(),sandbox_info);//@}}//CAboutDlgdialogusedforAppAboutclassCAboutDlg:publicCDialogEx{public:CAboutDlg();//DialogDataenum{IDD=IDD_ABOUTBOX};protected:virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport//Implementationprotected:DECLARE_MESSAGE_MAP()};CAboutDlg::CAboutDlg():CDialogEx(CAboutDlg::IDD){}voidCAboutDlg::DoDataExchange(CDataExchange*pDX){CDialogEx::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg,CDialogEx)END_MESSAGE_MAP()//AppcommandtorunthedialogvoidCETradeClientApp::OnAppAbout(){CAboutDlgaboutDlg;aboutDlg.DoModal();}//CETradeClientAppmessagehandlers//InstanceManagerCETradeClientApp::InstanceManager::InstanceManager():m_mutex_handle(nullptr){}CETradeClientApp::InstanceManager::~InstanceManager(){Unregister();}boolCETradeClientApp::InstanceManager::Register(conststd::wstring&app_id){HANDLEhHandle=CreateMutex(NULL,TRUE,app_id.c_str());if(ERROR_ALREADY_EXISTS==GetLastError()){//Logreturnfalse;}m_mutex_handle=hHandle;returntrue;}voidCETradeClientApp::InstanceManager::Unregister(){if(nullptr!=m_mutex_handle){ReleaseMutex(m_mutex_handle);//ExplicitlyreleasemutexCloseHandle(m_mutex_handle);//closehandlebeforeterminatingm_mutex_handle=nullptr;}}