Get current time stamp int Getunixtime () { CTime time = Ctime::getcurrenttime (); return (int) time. GetTime (); } CString Formatunit (double nbytes) { char* Units[5] = {"B", "KB", "MB", "GB", "TB"}; int i; for (i = 0; ((int) nbytes) >= 1024 && i < 4; i++) Nbytes/= 1024; CString strresult; Strresult.format ("%.2f%s", Nbytes, Units[i]); return strresult; } DWORD winapi DownloadFile (lpvoid pparam) { cinstallonlinedlg* pwnd = (cinstallonlinedlg*) AfxGetApp ()- >m_pMainWnd; char FILEBUF[512]; CInternetSession netsession; CStdioFile *ftargfile = NULL; CString Szfile (Download_url), FileSize, Kbin, kbsec, NewName, Perc, Strleft, strright; int ndownloaded = 1; try { pwnd->setleftstring ("Verifying download Address ..."); ftargfile = Netsession.openurl (szfile, 1, internet_flag_transfer_binary | Internet_flag_reload); int filesize = Ftargfile->seektoend (); ftargfile->seektobegin (); //creates a new destination file cfile fdestfile in the current directory (Ftargfile->getfilename (), CFile:: Modecreate | Cfile::modewrite | Cfile::typebinary); int byteswrite; //bytes written to file int pos = 0; & nbsp; //Current progress bar position double kbsec; //record seconds, speed (kb/sec) int secs; pwnd->setfilename (Ftargfile->getfilename ()); pwnd->m_ Progress. SetRange32 (0, filesize); int nstarttime = Getunixtime (); int nlastoutputtime = 0; cstring strfilesize = Formatunit (filesize); while (byteswrite = Ftargfile->read (filebuf)) //Read file { //the number of seconds based on the start time compared to the current time int ncurrenttime = Getunixtime (); secs = Ncurrenttime-nstarttime; secs = secs = = 0? 1:secs; pos = pos + byteswrite; //Set new progress bar position fdestfile.write (Filebuf, byteswrite); //write the actual data to the file pwnd->m_progress. SetPos (POS); if ((ncurrenttime-nlastoutputtime) > 1) { kbsec = pos/secs; /Get how many downloads per second (KB) cstring strrecv = Formatunit (POS); cstring strspeed = Formatunit (kbsec); strleft.format ("Download Progress:%s/%s", Strrecv.getbuffer (), Strfilesize.getbuffer ()); pwnd->setleftstring (strleft); strright.format ("Download speed:%s/s", Strspeed.getbuffer ()); pwnd->setrightstring (strright); nlastoutputtime = ncurrenttime; } } //Download complete, close file fdestfile.close (); } catch (CInternetException *ie) { CString strerror; TCHAR error[255]; Ie->geterrormessage (Error, 255); Get error messages strerror = error; pwnd->setleftstring (strerror); pwnd->setrightstring (""); if (ftargfile) delete ftargfile; ie->delete (); // Remove the exception object to prevent leaks pwnd->setdownloadstatus (FALSE); pwnd->getdlgitem (Idc_button1)->enablewindow (TRUE); return 0; } if (ftargfile) delete ftargfile; if (ndownloaded = 1) { Pwnd->setleftstring ("Download complete, click" Install "!"); pwnd->m_btn. Loadbitmaps (Idb_btn_install_normal); pwnd->m_btn. SizeToContent (); pwnd->setdownloadstatus (TRUE); pwnd->getdlgitem (Idc_button1)->enablewindow (TRUE); } return 0; } |