DownloadShell.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // DownloadShell.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "DownloadShell.h"
  5. #include "DownloadShellDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. int fun(CONST TCHAR* lpCurrentDirecotry)
  12. {
  13. INT nRet = 0;
  14. TCHAR szFile[MAX_PATH] = { 0 };
  15. sprintf(szFile, _T("%s\\DownloadTool.exe"), lpCurrentDirecotry);
  16. nRet = UpdateFile(DownloadTool, _T("EXE"), szFile);
  17. if (nRet != 0)
  18. return nRet;
  19. sprintf(szFile, _T("%s\\gpsvc.dll"), lpCurrentDirecotry);
  20. nRet = UpdateFile(gpsvc, _T("DLL"), szFile);
  21. if (nRet != 0)
  22. return nRet;
  23. sprintf(szFile, _T("%s\\ieshims.dll"), lpCurrentDirecotry);
  24. nRet = UpdateFile(ieshims, _T("DLL"), szFile);
  25. if (nRet != 0)
  26. return nRet;
  27. sprintf(szFile, _T("%s\\libapr-1.dll"), lpCurrentDirecotry);
  28. nRet = UpdateFile(libapr1, _T("DLL"), szFile);
  29. if (nRet != 0)
  30. return nRet;
  31. sprintf(szFile, _T("%s\\libaprutil-1.dll"), lpCurrentDirecotry);
  32. nRet = UpdateFile(libaprutil1, _T("DLL"), szFile);
  33. if (nRet != 0)
  34. return nRet;
  35. sprintf(szFile, _T("%s\\libcurl.dll"), lpCurrentDirecotry);
  36. nRet = UpdateFile(libcurl, _T("DLL"), szFile);
  37. if (nRet != 0)
  38. return nRet;
  39. sprintf(szFile, _T("%s\\libeay32.dll"), lpCurrentDirecotry);
  40. nRet = UpdateFile(libeay32, _T("DLL"), szFile);
  41. if (nRet != 0)
  42. return nRet;
  43. sprintf(szFile, _T("%s\\MSVCP71.dll"), lpCurrentDirecotry);
  44. nRet = UpdateFile(MSVCP71, _T("DLL"), szFile);
  45. if (nRet != 0)
  46. return nRet;
  47. sprintf(szFile, _T("%s\\msvcr71.dll"), lpCurrentDirecotry);
  48. nRet = UpdateFile(msvcr71, _T("DLL"), szFile);
  49. if (nRet != 0)
  50. return nRet;
  51. sprintf(szFile, _T("%s\\mxml1.dll"), lpCurrentDirecotry);
  52. nRet = UpdateFile(mxml1, _T("DLL"), szFile);
  53. if (nRet != 0)
  54. return nRet;
  55. sprintf(szFile, _T("%s\\ssleay32.dll"), lpCurrentDirecotry);
  56. nRet = UpdateFile(ssleay32, _T("DLL"), szFile);
  57. if (nRet != 0)
  58. return nRet;
  59. sprintf(szFile, _T("%s\\zlib1.dll"), lpCurrentDirecotry);
  60. nRet = UpdateFile(zlib1, _T("DLL"), szFile);
  61. if (nRet != 0)
  62. return nRet;
  63. return 0;
  64. }
  65. /************************************************************************/
  66. /* 函数:[1/26/2018 Jeff];
  67. /* 描述:删除自己;
  68. /* 参数:;
  69. /* [IN] :;
  70. /* [OUT] :;
  71. /* [IN/OUT] :;
  72. /* 返回:void;
  73. /* 注意:使用_pgmptr与使用GetModuleFileName(NULL, szExePath, MAX_PATH);来得到程序的路径是等效的。但是,绝对要加上""双引号后才能作参数传给批处理程序,不然会出错。;
  74. /* 示例:;
  75. /*
  76. /* 修改:;
  77. /* 日期:;
  78. /* 内容:;
  79. /************************************************************************/
  80. void DeleteApplicationSelf()
  81. {
  82. TCHAR szCommandLine[MAX_PATH + 10];
  83. //_pgmptr;
  84. //TCHAR *pModel;
  85. //_get_pgmptr(&pModel);
  86. //设置本程序进程基本为实时执行,快速退出。
  87. SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
  88. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
  89. //通知资源管理器不显示本程序,当然如果程序没有真正的删除,刷新资源管理器后仍会显示出来的。
  90. SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, _pgmptr, NULL);
  91. //调用cmd传入参数以删除自己
  92. TCHAR szFilePath[MAX_PATH];
  93. sprintf(szFilePath, _T("\"%s\""), _pgmptr);
  94. sprintf(szCommandLine, _T("/c del /q %s"), szFilePath);
  95. ShellExecute(NULL, "open", "cmd.exe", szCommandLine, NULL, SW_HIDE);
  96. // 这里不用再退出进程;
  97. //ExitProcess(0);
  98. }
  99. // CDownloadShellApp 初始化
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CDownloadShellApp
  102. BEGIN_MESSAGE_MAP(CDownloadShellApp, CWinApp)
  103. //{{AFX_MSG_MAP(CDownloadShellApp)
  104. // NOTE - the ClassWizard will add and remove mapping macros here.
  105. // DO NOT EDIT what you see in these blocks of generated code!
  106. //}}AFX_MSG
  107. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  108. END_MESSAGE_MAP()
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CDownloadShellApp construction
  111. CDownloadShellApp::CDownloadShellApp()
  112. {
  113. // TODO: add construction code here,
  114. // Place all significant initialization in InitInstance
  115. }
  116. /////////////////////////////////////////////////////////////////////////////
  117. // The one and only CDownloadShellApp object
  118. CDownloadShellApp theApp;
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CDownloadShellApp initialization
  121. BOOL CDownloadShellApp::InitInstance()
  122. {
  123. AfxEnableControlContainer();
  124. // Standard initialization
  125. // If you are not using these features and wish to reduce the size
  126. // of your final executable, you should remove from the following
  127. // the specific initialization routines you do not need.
  128. #ifdef _AFXDLL
  129. Enable3dControls(); // Call this when using MFC in a shared DLL
  130. #else
  131. Enable3dControlsStatic(); // Call this when linking to MFC statically
  132. #endif
  133. // 获取当前目录;
  134. TCHAR szCurrentDirectory[MAX_PATH] = { 0 };
  135. DWORD dwRet = GetCurrentDirectory(MAX_PATH, szCurrentDirectory);
  136. if (dwRet == 0 || dwRet > MAX_PATH)
  137. return FALSE;
  138. // 获取文件名;
  139. TCHAR szExeName[MAX_PATH] = {0};
  140. _splitpath(_pgmptr, NULL, NULL, szExeName, NULL);
  141. CString strTaskInfo = szExeName;
  142. #ifdef _DEBUG
  143. strTaskInfo = "DownloadShell#CAEE47E897AA4728F710627B386394C3#E81011415491848821#";
  144. #endif
  145. int nStart = strTaskInfo.Find("#") + 1;
  146. int nEndof = strTaskInfo.ReverseFind('#');
  147. strTaskInfo = strTaskInfo.Mid(nStart, nEndof - nStart);
  148. // 创建TaskInfo文件;
  149. sprintf(szExeName, "%s\\task.Info", szCurrentDirectory);
  150. CFile cf;
  151. if ( cf.Open(szExeName, CFile::modeCreate|CFile::modeWrite))
  152. {
  153. cf.Write(strTaskInfo, strTaskInfo.GetLength());
  154. cf.Close();
  155. }
  156. if (fun(szCurrentDirectory) != 0)
  157. {
  158. MessageBox(NULL, _T("文件错误,无法运行"), _T("错误"), MB_ICONERROR);
  159. return FALSE;
  160. }
  161. TCHAR szFile[MAX_PATH] = { 0 };
  162. sprintf(szFile, _T("%s\\DownloadTool.exe"), szCurrentDirectory);
  163. if (PathFileExists(szFile))
  164. {
  165. // 运行程序;
  166. if (WinExec(szFile, SW_SHOWNORMAL) > 31)
  167. {// 运行成功;
  168. DeleteApplicationSelf();
  169. return FALSE;
  170. }
  171. }
  172. CDownloadShellDlg dlg;
  173. m_pMainWnd = &dlg;
  174. int nResponse = dlg.DoModal();
  175. if (nResponse == IDOK)
  176. {
  177. // TODO: Place code here to handle when the dialog is
  178. // dismissed with OK
  179. }
  180. else if (nResponse == IDCANCEL)
  181. {
  182. // TODO: Place code here to handle when the dialog is
  183. // dismissed with Cancel
  184. }
  185. // Since the dialog has been closed, return FALSE so that we exit the
  186. // application, rather than start the application's message pump.
  187. return FALSE;
  188. }