global.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. #include "stdafx.h"
  2. #include "global.h"
  3. #include "AppXmlInfo.h"
  4. #include "InstallXmlInfo.h"
  5. #include "xunzip.h"
  6. #include "Wininet.h"
  7. #include <shlwapi.h>
  8. #include <stdio.h>
  9. #include <tchar.h>
  10. #include <afxinet.h>
  11. #include "atlbase.h"
  12. #include "shobjidl.h"
  13. #include <locale>
  14. #pragma comment(lib,"Wininet.lib")
  15. #pragma comment(lib, "shell32.lib")
  16. void DeleteDirectory(CString dir,bool delParenDir)
  17. {
  18. if(dir.IsEmpty())
  19. return ;
  20. CFileFind find;
  21. BOOL bFound = find.FindFile(dir + _T("\\*"),0);
  22. while(bFound)
  23. {
  24. bFound = find.FindNextFileW();
  25. if(find.IsDots())
  26. {
  27. continue;
  28. }
  29. SetFileAttributes(find.GetFilePath(),FILE_ATTRIBUTE_NORMAL);
  30. if(find.IsDirectory())
  31. {
  32. DeleteDirectory(find.GetFilePath(),true);
  33. }
  34. else
  35. {
  36. DeleteFile(find.GetFilePath());
  37. }
  38. }
  39. find.Close();
  40. if(delParenDir)
  41. RemoveDirectory(dir);
  42. return ;
  43. }
  44. void RemoveDirectoryRecursive(CString dir)
  45. {
  46. if(dir.IsEmpty())
  47. return ;
  48. CFileFind find;
  49. BOOL bFound = find.FindFile(dir + _T("\\*.*"),0);
  50. while(bFound)
  51. {
  52. bFound = find.FindNextFileW();
  53. if(find.IsDots())
  54. {
  55. continue;
  56. }
  57. SetFileAttributes(find.GetFilePath(),FILE_ATTRIBUTE_NORMAL);
  58. if(find.IsDirectory())
  59. {
  60. RemoveDirectoryRecursive(find.GetFilePath());
  61. }
  62. }
  63. find.Close();
  64. BOOL ret = RemoveDirectory(dir);
  65. return ;
  66. }
  67. CString GetSystemFolderByCSIDL(int csidl)
  68. {
  69. LPITEMIDLIST ppidl = NULL;
  70. TCHAR buffer[MAX_PATH];
  71. CString path;
  72. if (SHGetSpecialFolderLocation(NULL, csidl, &ppidl) == S_OK)
  73. {
  74. BOOL flag = SHGetPathFromIDList(ppidl, buffer);
  75. CoTaskMemFree(ppidl);
  76. path = buffer;
  77. }
  78. return path;
  79. }
  80. bool TestDownloadServer(CString url)
  81. {
  82. HINTERNET hInternet;
  83. hInternet=InternetOpen(_T("Testing"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
  84. if (hInternet==NULL)
  85. {
  86. return false;
  87. }
  88. HINTERNET hUrl;
  89. hUrl=InternetOpenUrl(hInternet,url,NULL,0,INTERNET_FLAG_RELOAD,0); //
  90. if (hUrl==NULL)
  91. {
  92. InternetCloseHandle(hInternet);
  93. return false;
  94. }
  95. InternetCloseHandle(hUrl);
  96. InternetCloseHandle(hInternet);
  97. return true;
  98. }
  99. bool ExecAppWaitForExit(CString appName ,CString workingDir )
  100. {
  101. BOOL bolSuccess ;
  102. STARTUPINFO StartupInfo;
  103. PROCESS_INFORMATION proinfo;
  104. memset(&StartupInfo,0,sizeof(STARTUPINFO));
  105. StartupInfo.cb=sizeof(STARTUPINFO);
  106. StartupInfo.lpReserved=NULL;
  107. StartupInfo.lpDesktop=NULL;
  108. StartupInfo.lpTitle=NULL;
  109. StartupInfo.dwFlags=STARTF_USESHOWWINDOW;
  110. StartupInfo.cbReserved2=0;
  111. StartupInfo.lpReserved2=NULL;
  112. StartupInfo.wShowWindow=SW_HIDE ;
  113. ZeroMemory(&proinfo, sizeof(proinfo));
  114. bolSuccess = CreateProcess(appName,NULL, NULL, NULL, TRUE,
  115. CREATE_UNICODE_ENVIRONMENT, NULL, workingDir, &StartupInfo, &proinfo) ;
  116. DWORD R = GetLastError();
  117. if ( bolSuccess !=0 ) //运行结束
  118. {
  119. CloseHandle( proinfo.hThread ) ;
  120. WaitForSingleObject( proinfo.hProcess ,INFINITE ) ;
  121. CloseHandle( proinfo.hProcess ) ;
  122. return true ;
  123. }
  124. else
  125. {
  126. return false ;
  127. }
  128. }
  129. bool MoveFileN(CString srcName,CString destName)
  130. {
  131. if(FileExist(destName))
  132. {
  133. DWORD dWold;
  134. dWold=::GetFileAttributes(destName);
  135. dWold&=~FILE_ATTRIBUTE_READONLY;
  136. ::SetFileAttributes(destName,dWold);
  137. ::DeleteFile(destName);
  138. }
  139. return ::MoveFile(srcName,destName)?true:false;
  140. }
  141. bool CreateSubDir(CString parentPath,CString childPath)
  142. {
  143. childPath.Replace(_T("/"),_T("\\"));
  144. CString strDir(childPath);//存放要创建的目录字符串
  145. //确保以'\'结尾以创建最后一个目录
  146. if (childPath.GetAt(childPath.GetLength()-1)!=_T('\\'))
  147. {
  148. childPath.AppendChar(_T('\\'));
  149. }
  150. //确保不以'\'开头
  151. if (childPath.GetAt(0)==_T('\\'))
  152. {
  153. childPath.Delete(0);
  154. }
  155. CStringArray vPath;//存放每一层目录字符串
  156. CString strTemp;//一个临时变量,存放目录字符串
  157. bool bSuccess = false;//成功标志
  158. //遍历要创建的字符串
  159. for (int i=0;i<childPath.GetLength();++i)
  160. {
  161. if (childPath.GetAt(i) != _T('\\'))
  162. {//如果当前字符不是'\\'
  163. strTemp.AppendChar(childPath.GetAt(i));
  164. }
  165. else
  166. {//如果当前字符是'\\'
  167. vPath.Add(strTemp);//将当前层的字符串添加到数组中
  168. strTemp.AppendChar(_T('\\'));
  169. }
  170. }
  171. //遍历存放目录的数组,创建每层目录
  172. for (int i=0;i<vPath.GetSize();i++)
  173. {
  174. //如果CreateDirectory执行成功,返回true,否则返回false
  175. bSuccess = CreateInstallPath(parentPath + _T("\\") + vPath.GetAt(i));
  176. if(!bSuccess)
  177. break;
  178. }
  179. if(bSuccess)
  180. {
  181. return bSuccess;
  182. }
  183. else
  184. {
  185. if(childPath.GetLength()==0)
  186. return true;
  187. else
  188. return false;
  189. }
  190. }
  191. bool FileExist(CString strFileName)
  192. {
  193. CFileFind fFind;
  194. return (fFind.FindFile(strFileName)!=0)?true:false;
  195. }
  196. bool ExtractFileFromZip(CString absolutPath,CString strZipFileName)
  197. {
  198. ZIPENTRYW ze;
  199. CString strZipFile = absolutPath + _T("\\") + strZipFileName;
  200. TCHAR * pszArchive = strZipFile.GetBuffer(strZipFile.GetLength());
  201. HZIP hz = OpenZip(pszArchive, 0, ZIP_FILENAME);
  202. bool succeed = false;
  203. if (hz)
  204. {
  205. ZRESULT zr = GetZipItem(hz, 0, &ze);
  206. if (zr == ZR_OK)
  207. {
  208. //ze.ctime.dwLowDateTime = ze.ctime.dwLowDateTime - 8*60*60;
  209. CString filename = ze.name;
  210. CString strItemFile = absolutPath + _T("\\")+filename ;
  211. TCHAR * targetFile = strItemFile.GetBuffer(strItemFile.GetLength());
  212. ZRESULT zr2 = UnzipItem(hz,0,targetFile,0,ZIP_FILENAME);
  213. if(zr2 == ZR_OK)
  214. {
  215. succeed = true;
  216. DWORD dWold;
  217. dWold=::GetFileAttributes(strItemFile);
  218. dWold&=~FILE_ATTRIBUTE_READONLY;
  219. ::SetFileAttributes(strItemFile,dWold);
  220. }
  221. }
  222. }
  223. CloseZip(hz);
  224. ::DeleteFile(strZipFile);
  225. return succeed;
  226. }
  227. CString GetSysPath(CString sEnvironmentName)
  228. {
  229. TCHAR buffer[1024];
  230. DWORD dwRet = GetEnvironmentVariable(sEnvironmentName, buffer, sizeof(buffer));
  231. CString value;
  232. if (dwRet > 0)
  233. {
  234. value = buffer;
  235. }
  236. return value;
  237. }
  238. bool FolderExist(CString strPath)
  239. {
  240. WIN32_FIND_DATA wfd;
  241. bool rValue = false;
  242. HANDLE hFind = FindFirstFile(strPath, &wfd);
  243. if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  244. {
  245. rValue = true;
  246. }
  247. FindClose(hFind);
  248. return rValue;
  249. }
  250. bool CreateInstallPath(CString dir)
  251. {
  252. if(!FolderExist(dir))
  253. {
  254. if(::CreateDirectory( dir , NULL)==0)
  255. return false;
  256. }
  257. return true;
  258. }
  259. bool DownloadUpdatePackage(CString serverAppXmlLocalPath,CString serverInstallXmlLocalPath,ThreadParamInfo *info)
  260. {
  261. CInstallXmlInfo installInfo(serverInstallXmlLocalPath);
  262. CAppXmlInfo appInfo(serverAppXmlLocalPath);
  263. for(int i=0;i<installInfo.installPaths.GetSize();i++)
  264. {
  265. if(!CreateInstallPath(installInfo.installPaths.GetAt(i)))
  266. {
  267. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_DIR_CREATE_ERROR,0);
  268. return false;
  269. }
  270. }
  271. if(info->stop) // 如果取消下载
  272. {
  273. AfxEndThread(0); // 结束下载线程
  274. }
  275. CString storePath = installInfo.tempPath + _T("\\");
  276. CString localAppXmlFile = installInfo.appPath + _T("\\") + serverAppXmlLocalPath.Mid(serverAppXmlLocalPath.ReverseFind('\\')+1);
  277. CString localInstallXmlFile = installInfo.appPath + _T("\\") + serverInstallXmlLocalPath.Mid(serverInstallXmlLocalPath.ReverseFind('\\')+1);
  278. //判断可用的下载服务器
  279. CString downloadserver;
  280. for(int i=0;i<appInfo.download_servers.GetSize();i++)
  281. {
  282. if(info->stop) // 如果取消下载
  283. {
  284. AfxEndThread(0); // 结束下载线程
  285. }
  286. if(TestDownloadServer(appInfo.download_servers.GetAt(i)))
  287. {
  288. downloadserver = appInfo.download_servers.GetAt(i);
  289. break;
  290. }
  291. }
  292. if(downloadserver.GetLength()==0)
  293. {
  294. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_DOWNLOAD_ERROR,0);
  295. return false;
  296. }
  297. if(info->stop) // 如果取消下载
  298. {
  299. AfxEndThread(0); // 结束下载线程
  300. }
  301. //对本地文件和服务端文件进行差异化比较,形成下载文件列表
  302. CStringArray differenceFiles;
  303. CStringArray zipFileNames;//只是用于记录zip文件名,方便解压缩
  304. if(FileExist(localAppXmlFile))
  305. {
  306. CAppXmlInfo appLocalInfo(localAppXmlFile);
  307. for(int i=0;i<appInfo.download_files.GetSize();i++)
  308. {
  309. bool match = false;
  310. for(int j=0;j<appLocalInfo.download_files.GetSize();j++)
  311. {
  312. if(info->stop) // 如果取消下载
  313. {
  314. AfxEndThread(0); // 结束下载线程
  315. }
  316. if(appInfo.download_files.GetAt(i).fileName.CompareNoCase(appLocalInfo.download_files.GetAt(j).fileName)==0)
  317. {
  318. match = true;
  319. CString local = appLocalInfo.download_files.GetAt(j).lastUpdateTime;
  320. CString server = appInfo.download_files.GetAt(i).lastUpdateTime;
  321. COleDateTime localTime,currentTime;
  322. if(localTime.ParseDateTime(local)&&currentTime.ParseDateTime(server))
  323. {
  324. if(localTime<currentTime)
  325. {
  326. match = false;
  327. break;
  328. }
  329. }
  330. break;
  331. }
  332. }
  333. if(!match){
  334. differenceFiles.Add(downloadserver + appInfo.download_files.GetAt(i).fileName);
  335. zipFileNames.Add(appInfo.download_files.GetAt(i).fileName);
  336. }
  337. }
  338. }
  339. else
  340. {
  341. for(int i=0;i<appInfo.download_files.GetSize();i++)
  342. {
  343. differenceFiles.Add(downloadserver + appInfo.download_files.GetAt(i).fileName);
  344. zipFileNames.Add(appInfo.download_files.GetAt(i).fileName);
  345. }
  346. }
  347. if(info->stop) // 如果取消下载
  348. {
  349. AfxEndThread(0); // 结束下载线程
  350. }
  351. //下载差异化文件,之前清理一下storePaht已经存在的文件
  352. if(storePath.Find(_T("Temp"))>0)
  353. DeleteDirectory(storePath,false);//危险,请确保传入的路径是你想要删除的路径
  354. for(int i=0;i<differenceFiles.GetSize();i++)
  355. {
  356. if(i==0)
  357. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_RANGE,differenceFiles.GetSize(),0);
  358. if(info->stop)
  359. {
  360. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_DOWNLOAD_ABORT,0);
  361. return false;
  362. }
  363. if(DownloadFile(differenceFiles.GetAt(i),storePath,info))
  364. {
  365. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_DOWNLOAD_OK,0);
  366. }
  367. else
  368. {
  369. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_DOWNLOAD_ERROR,0);
  370. return false;
  371. }
  372. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_PROGRESS,i+1,0);
  373. }
  374. //解压缩已经下载的zip文件
  375. for(int i=0;i<zipFileNames.GetSize();i++)
  376. {
  377. CString zipFileName = zipFileNames.GetAt(i);
  378. CString absolutePath = installInfo.tempPath;
  379. if(info->stop) // 如果取消下载
  380. {
  381. AfxEndThread(0); // 结束下载线程
  382. }
  383. if(!ExtractFileFromZip(absolutePath,zipFileName))
  384. {
  385. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_UNZIP_ERROR,0);
  386. return false;
  387. }
  388. }
  389. bool needReplaceSetup = false;
  390. CString tmpDir = GetSysPath(_T("temp"));
  391. CString batName = tmpDir + _T("\\sncupdate_replaceself.bat");
  392. CString appMainName = AfxGetApp()->m_pszAppName;
  393. if(differenceFiles.GetSize()>0)
  394. {
  395. //按照install.xml的配置,安装已经解压缩的文件
  396. for(int i=0;i<installInfo.locations.GetSize();i++)
  397. {
  398. if(info->stop) // 如果取消下载
  399. {
  400. AfxEndThread(0); // 结束下载线程
  401. }
  402. CString fileName = installInfo.locations.GetAt(i).fileName;
  403. CString fileType = installInfo.locations.GetAt(i).fileType;
  404. CString subDir = installInfo.locations.GetAt(i).subDir;
  405. CString app = _T("app");
  406. CString data = _T("data");
  407. if(FileExist(installInfo.tempPath + _T("\\") + fileName))
  408. {
  409. CString destDir;
  410. if(fileType.CompareNoCase(app)==0)
  411. {
  412. destDir = installInfo.appPath ;
  413. }
  414. else if(fileType.CompareNoCase(data)==0)
  415. {
  416. destDir = installInfo.dataPath ;
  417. }
  418. if(fileName.CompareNoCase(appMainName+_T(".exe"))==0)
  419. {
  420. //替换升级程序
  421. CStdioFile file;
  422. if(file.Open(batName, CFile::modeCreate | CFile::modeWrite))
  423. {
  424. char * old_locale = _strdup(setlocale(LC_CTYPE,NULL));
  425. setlocale(LC_CTYPE,"chs");
  426. CString script;
  427. script = _T(":repeat \r\n");
  428. file.WriteString(script);
  429. script = _T( "del \"")+ installInfo.appPath + _T("\\") + fileName + _T("\"\r\n ") ;
  430. file.WriteString(script);
  431. script = _T("if exist \"") +installInfo.appPath + _T("\\") + fileName + _T("\" goto Repeat\r\n");
  432. file.WriteString(script);
  433. script = _T("move \"") +installInfo.tempPath + _T("\\") + fileName + _T("\" \"") + installInfo.appPath + _T("\\") + fileName + _T("\"\r\n");
  434. file.WriteString(script);
  435. script = _T("del \"") + batName + _T("\"\r\n");
  436. file.WriteString(script);
  437. //script = _T("PAUSE\r\n");;
  438. //file.WriteString(script);
  439. setlocale(LC_CTYPE,old_locale);
  440. free(old_locale);
  441. file.Close();
  442. }
  443. needReplaceSetup = true;
  444. continue;
  445. }
  446. if(CreateSubDir(destDir,subDir))
  447. {
  448. destDir = destDir + subDir;
  449. }
  450. else
  451. {
  452. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_FILE_MOVE_ERROR,0);
  453. return false;
  454. }
  455. CString destName = destDir + _T("\\") + fileName;
  456. MoveFileN(installInfo.tempPath + _T("\\") + fileName,destName);
  457. }
  458. }
  459. }
  460. //移动配置文件到对应目录
  461. MoveFileN(serverAppXmlLocalPath,localAppXmlFile);
  462. MoveFileN(serverInstallXmlLocalPath,localInstallXmlFile);
  463. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_VERSION_UPDATED,0);
  464. //Sleep(1000);
  465. //完成安装,按照顺序启动程序进行设置
  466. CString on = _T("on");
  467. CString off = _T("off");
  468. for(int i=0;i<appInfo.run_apps.GetSize();i++)
  469. {
  470. CString appname = appInfo.run_apps.GetAt(i).appname;
  471. CString mode = appInfo.run_apps.GetAt(i).mode;
  472. CString runPath = installInfo.appPath + _T("\\") + appname;
  473. if(info->stop) // 如果取消下载
  474. {
  475. AfxEndThread(0); // 结束下载线程
  476. }
  477. if(mode.CompareNoCase(on)==0)
  478. {
  479. ExecAppWaitForExit(runPath,installInfo.appPath);
  480. }
  481. else if(mode.CompareNoCase(off)==0)
  482. {
  483. if(i!=appInfo.run_apps.GetSize()-1)
  484. {
  485. ExecAppWaitForExit(runPath,installInfo.appPath);
  486. }
  487. else
  488. {
  489. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_SETUP_FINISH,0);
  490. Sleep(1000);
  491. if(needReplaceSetup)
  492. ShellExecute(NULL,_T("open"),batName,NULL,tmpDir,SW_HIDE);
  493. ShellExecute(NULL,_T("open"),runPath,NULL,installInfo.appPath,SW_SHOW);
  494. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_SETUP_EXIT,0);
  495. }
  496. }
  497. }
  498. return true;
  499. }
  500. UINT ThreadFun(LPVOID lpParam)
  501. {
  502. ThreadParamInfo *info = (ThreadParamInfo*)lpParam;
  503. CString appXmlUrl,installXmlUrl;
  504. if(info->isproduct)
  505. {
  506. appXmlUrl= _T(STATIC_APP_CONFIG_URL);
  507. installXmlUrl = _T(STATIC_INSTALL_CONFIG_URL);
  508. }
  509. else
  510. {
  511. appXmlUrl= _T(STATIC_APP_CONFIG_URL_TEST);
  512. installXmlUrl = _T(STATIC_INSTALL_CONFIG_URL_TEST);
  513. }
  514. CString tmpPath = GetSysPath(_T("TEMP")) ;
  515. CString storePath = tmpPath + _T("\\SpeedNumCube");
  516. CString serverAppXmlLocalPath = storePath + _T("\\") + appXmlUrl.Mid(appXmlUrl.ReverseFind('/')+1);
  517. CString serverInstallXmlLocalPath = storePath + _T("\\") + installXmlUrl.Mid(installXmlUrl.ReverseFind('/')+1);
  518. if(!FolderExist(storePath))
  519. {
  520. //SECURITY_ATTRIBUTES attrib;
  521. if(::CreateDirectory( storePath,NULL)==0)
  522. {
  523. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_DIR_CREATE_ERROR,0);
  524. return 0;
  525. }
  526. }
  527. Sleep(1000);
  528. if(DownloadFile(appXmlUrl,storePath,info) && DownloadFile(installXmlUrl,storePath,info) )
  529. {
  530. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_NETWORK_AVAIABLE,0);
  531. if(!DownloadUpdatePackage(serverAppXmlLocalPath,serverInstallXmlLocalPath,info))
  532. {
  533. return 0;
  534. }
  535. }
  536. else
  537. {
  538. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_STATE,USER_PARAM_NETWORK_NOT_AVAIABLE,0);
  539. return 0;
  540. }
  541. return 1;
  542. }
  543. bool DownloadFile(CString szUrl,CString storePath,ThreadParamInfo *info)
  544. {
  545. char buffer[10240];
  546. CInternetSession session;
  547. CStdioFile *httpfile;
  548. try
  549. {
  550. httpfile = session.OpenURL(szUrl,1,INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);
  551. COleDateTime dlStart = COleDateTime::GetCurrentTime();
  552. ULONGLONG fileBytes = httpfile->SeekToEnd();
  553. httpfile->SeekToBegin();
  554. // 在当前目录创建新的目标文件
  555. CFile localfile;
  556. CFileException e;
  557. if(TRUE!=localfile.Open(storePath + _T("\\") + httpfile->GetFileName(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary,&e))
  558. {
  559. httpfile->Close();
  560. return false;
  561. }
  562. UINT bytesfetch; // 读取的的字节数
  563. ULONGLONG byteswrite = 0; // 已经写入的字节数
  564. double nperc,kbrecv; // 进度条的百分比,获取到的数据大小(Kbs为单位)
  565. double secs,kbsec; // 记录秒数, 速度(KB/秒)
  566. while (bytesfetch = httpfile->Read(buffer, sizeof(buffer))) // 读取文件
  567. {
  568. if(info->stop) // 如果取消下载
  569. {
  570. localfile.Close(); // 关闭我们的目标文件
  571. httpfile->Close(); // 关闭远程文件
  572. delete httpfile; // 删除CStdioFile对象,以防止泄漏
  573. AfxEndThread(0); // 结束下载线程
  574. }
  575. // 根据开始时间与当前时间比较,获取秒数
  576. COleDateTimeSpan dlElapsed = COleDateTime::GetCurrentTime() - dlStart;
  577. secs = dlElapsed.GetTotalSeconds();
  578. byteswrite = byteswrite + bytesfetch; // 设置新的进度条位置
  579. localfile.Write(buffer, bytesfetch); // 将实际数据写入文件
  580. nperc = (double)byteswrite * 100 / (double)fileBytes; // 进度百分比
  581. kbrecv = (double )byteswrite / 1024; // 获取收到的数据
  582. kbsec = kbrecv / secs; // 获取每秒下载多少(KB)
  583. WPARAM para = (WPARAM)nperc;
  584. if(byteswrite%102400==0 && byteswrite/102400>0)
  585. ::PostMessage(info->hwnd,WM_USERMSG_UPGRADE_CHILD_PROGRESS,para,0);
  586. }
  587. // 下载完成,关闭文件
  588. localfile.Close();
  589. }
  590. catch(CInternetException *IE)
  591. {
  592. CString strerror;
  593. TCHAR error[255];
  594. IE->GetErrorMessage(error,255); // 获取错误消息
  595. strerror = error;
  596. if(!httpfile)
  597. delete httpfile;
  598. IE->Delete(); // 删除异常对象,以防止泄漏
  599. return false;
  600. }
  601. return true;
  602. }