贝贝源代码分析之一

来源:互联网 发布:python http下载文件 编辑:程序博客网 时间:2024/04/28 02:34
程序入口点代码分析
  1. int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/LPTSTR lpstrCmdLine, int nCmdShow) {
  2. #ifndef _DEBUG
  3.     SingleInstance dcapp(_T("{STRONGDC-AEE8350A-B49A-4753-AB4B-E55479A48351}"));
  4. #else
  5.     SingleInstance dcapp(_T("{STRONGDC-AEE8350A-B49A-4753-AB4B-E55479A48350}"));
  6. #endif
  7. //以上代码得到了单例的对象
  8.     if(dcapp.IsAnotherInstanceRunning()) {
  9.         // Allow for more than one instance...
  10.                 bool multiple = false;
  11.         if(_tcslen(lpstrCmdLine) == 0) {
  12.             if (::MessageBox(NULL, _T("There is already an instance of StrongDC++ running./nDo you want to launch another instance anyway?"), 
  13.                 _T(APPNAME) _T(" ") _T(VERSIONSTRING), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2 | MB_TOPMOST) == IDYES) {
  14.      //设置可以启动多个实例                                       
  15.                 multiple = true;
  16.             }
  17.         }
  18.         if(multiple == false) {
  19.             HWND hOther = NULL;
  20.             EnumWindows(searchOtherInstance, (LPARAM)&hOther);
  21.             if( hOther != NULL ) {
  22.                 // pop up
  23.                 ::SetForegroundWindow(hOther);
  24.                 if( IsIconic(hOther)) {
  25.                     // restore
  26.                     ::ShowWindow(hOther, SW_RESTORE);
  27.                 }
  28.                 sendCmdLine(hOther, lpstrCmdLine);
  29.             }
  30.             return FALSE;
  31.         }
  32.     }
  33.     
  34.     
  35.     // For SHBrowseForFolder, UPnP
  36.     HRESULT hRes = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); 
  37. #ifdef _DEBUG
  38.     EXTENDEDTRACEINITIALIZE( Util::getDataPath().c_str() );
  39.     //File::deleteFile(Util::getDataPath() + "exceptioninfo.txt");
  40. #endif
  41.     LPTOP_LEVEL_EXCEPTION_FILTER pOldSEHFilter = NULL;
  42.     pOldSEHFilter = SetUnhandledExceptionFilter(&DCUnhandledExceptionFilter);
  43.     
  44.     AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES | ICC_LISTVIEW_CLASSES | ICC_TREEVIEW_CLASSES | ICC_PROGRESS_CLASS | ICC_STANDARD_CLASSES |
  45.         ICC_TAB_CLASSES | ICC_UPDOWN_CLASS | ICC_USEREX_CLASSES);   // add flags to support other controls
  46.     
  47.     hRes = _Module.Init(NULL, hInstance);
  48.     ATLASSERT(SUCCEEDED(hRes));
  49.     
  50.     try {       
  51.         File f(WinUtil::getAppName(), File::READ, File::OPEN);
  52.         TigerTree tth(TigerTree::calcBlockSize(f.getSize(), 1));
  53.         size_t n = 0;
  54.         size_t n2 = DEBUG_BUFSIZE;
  55.         while( (n = f.read(buf, n2)) > 0) {
  56.             tth.update(buf, n);
  57.             n2 = DEBUG_BUFSIZE;
  58.         }
  59.         tth.finalize();
  60.         strcpy(::tth, tth.getRoot().toBase32().c_str());
  61.         WinUtil::tth = Text::toT(::tth);
  62.     } catch(const FileException&) {
  63.         dcdebug("Failed reading exe/n");
  64.     }   
  65.     HINSTANCE hInstRich = ::LoadLibrary(_T("RICHED20.DLL"));    
  66.     int nRet = Run(lpstrCmdLine, nCmdShow);
  67.  
  68.     if ( hInstRich ) {
  69.         ::FreeLibrary(hInstRich);
  70.     }
  71.     
  72.     // Return back old VS SEH handler
  73.     if (pOldSEHFilter != NULL)
  74.         SetUnhandledExceptionFilter(pOldSEHFilter);
  75.     _Module.Term();
  76.     ::CoUninitialize();
  77. #ifdef _DEBUG
  78.     EXTENDEDTRACEUNINITIALIZE();
  79. #endif
  80.     return nRet;
  81. }