程序入口点代码分析
- int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow) {
- #ifndef _DEBUG
- SingleInstance dcapp(_T("{STRONGDC-AEE8350A-B49A-4753-AB4B-E55479A48351}"));
- #else
- SingleInstance dcapp(_T("{STRONGDC-AEE8350A-B49A-4753-AB4B-E55479A48350}"));
- #endif
- //以上代码得到了单例的对象
- if(dcapp.IsAnotherInstanceRunning()) {
- // Allow for more than one instance...
- bool multiple = false;
- if(_tcslen(lpstrCmdLine) == 0) {
- if (::MessageBox(NULL, _T("There is already an instance of StrongDC++ running./nDo you want to launch another instance anyway?"),
- _T(APPNAME) _T(" ") _T(VERSIONSTRING), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2 | MB_TOPMOST) == IDYES) {
- //设置可以启动多个实例
- multiple = true;
- }
- }
- if(multiple == false) {
- HWND hOther = NULL;
- EnumWindows(searchOtherInstance, (LPARAM)&hOther);
- if( hOther != NULL ) {
- // pop up
- ::SetForegroundWindow(hOther);
- if( IsIconic(hOther)) {
- // restore
- ::ShowWindow(hOther, SW_RESTORE);
- }
- sendCmdLine(hOther, lpstrCmdLine);
- }
- return FALSE;
- }
- }
- // For SHBrowseForFolder, UPnP
- HRESULT hRes = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
- #ifdef _DEBUG
- EXTENDEDTRACEINITIALIZE( Util::getDataPath().c_str() );
- //File::deleteFile(Util::getDataPath() + "exceptioninfo.txt");
- #endif
- LPTOP_LEVEL_EXCEPTION_FILTER pOldSEHFilter = NULL;
- pOldSEHFilter = SetUnhandledExceptionFilter(&DCUnhandledExceptionFilter);
- AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES | ICC_LISTVIEW_CLASSES | ICC_TREEVIEW_CLASSES | ICC_PROGRESS_CLASS | ICC_STANDARD_CLASSES |
- ICC_TAB_CLASSES | ICC_UPDOWN_CLASS | ICC_USEREX_CLASSES); // add flags to support other controls
- hRes = _Module.Init(NULL, hInstance);
- ATLASSERT(SUCCEEDED(hRes));
- try {
- File f(WinUtil::getAppName(), File::READ, File::OPEN);
- TigerTree tth(TigerTree::calcBlockSize(f.getSize(), 1));
- size_t n = 0;
- size_t n2 = DEBUG_BUFSIZE;
- while( (n = f.read(buf, n2)) > 0) {
- tth.update(buf, n);
- n2 = DEBUG_BUFSIZE;
- }
- tth.finalize();
- strcpy(::tth, tth.getRoot().toBase32().c_str());
- WinUtil::tth = Text::toT(::tth);
- } catch(const FileException&) {
- dcdebug("Failed reading exe/n");
- }
- HINSTANCE hInstRich = ::LoadLibrary(_T("RICHED20.DLL"));
- int nRet = Run(lpstrCmdLine, nCmdShow);
- if ( hInstRich ) {
- ::FreeLibrary(hInstRich);
- }
- // Return back old VS SEH handler
- if (pOldSEHFilter != NULL)
- SetUnhandledExceptionFilter(pOldSEHFilter);
- _Module.Term();
- ::CoUninitialize();
- #ifdef _DEBUG
- EXTENDEDTRACEUNINITIALIZE();
- #endif
- return nRet;
- }