Use the HTTP Online Automatic Upgrade Program
Author: Yu Yanbin
Download source code
Development Background
In recent days, some programs have to be modified to avoid every notification to the program user, so they want to make an online automatic upgrade program. In vckbase, FTP is used. Many programs, such as KFW firewall, can monitor the data packets sent by the program. To prevent password leaks, you can use HTTP for update. My idea is to use the command line to pass the program name, version number, and URL of the update. ini configuration file. Command Line usage:
Update.exe program name VERSION File URL example: update.exe volleymail 3.0 http://www.extice.com/update/update.ini
The function prototype for parsing command line parameters is as follows:
CUpdateApp::GetCmdLinePara(CStringArray ¶Arr);
This function breaks down command line parameters and saves them to parw.r data. Then pass the command line information to the Main Dialog Box class. The Code is as follows:
dlg.m_strSoft = arr.GetAt(0); dlg.m_strVersion = arr.GetAt(1); AfxParseURL( arr.GetAt(2), dwType, dlg.m_strServer, dlg.m_strIniPath, dlg.m_dwPort);
This is the initialization of the dialog box. The software version number is displayed in the list box, 1:
m_cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5); m_pHttp=m_cis.GetHttpConnection( m_strServer,m_dwPort ); m_lbProduct.AddString(m_strSoft+" "+m_strVersion);
Figure 1
Then, find the available updates and update them through chttpfile. download the INI file to the temporary directory of the system, and then call getprivateprofilestring to read the latest version number and the file to be updated on the Internet to determine whether to update the file. Part of the Code:
CSF. open (m_strtempdir + "// update. ini ", cfile: modecreate | cfile: modewrite | cfile: typebinary); char Buf [2048]; int N; while (n = pfile-> Read (BUF, 2048)> 0) CSF. write (BUF, n); char Buf [128];: getprivateprofilestring (m_strsoft, "version", "1.0", Buf, sizeof (BUF ), m_strtempdir + "// update. ini "); m_strnewver = Buf; If (atof (m_strversion)> = atof (BUF) // the current version is later than {m_strstatus =" your current version is the latest! "; Updatedata (false); m_buok.enablewindow (false); return ;}
Update some code
Use cupdatedlg: findappprocessid () to check whether the program to be updated is running:
DWORD CUpdateDlg::FindAppProcessID() { HANDLE handle=::CreateToolhelp32Snapshot(TH32CS_SNAPALL,0); PROCESSENTRY32 Info; Info.dwSize = sizeof(PROCESSENTRY32); if(::Process32First(handle,&Info)) { do{ CString ss=Info.szExeFile; if(!ss.CompareNoCase(m_strSoft+".exe")) { ::CloseHandle(handle); return Info.th32ProcessID; } } while(::Process32Next(handle,&Info)); ::CloseHandle(handle); } return -1; }
This function returns the program process number. If the program to be updated is running, the system prompts you to manually exit or use terminateprocess to kill the process! The size of the downloaded file is as follows:
pFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH,str);
. To prevent network faults in half of downloads, first add the downloaded file with the extension name. upg. After the download is successful, replace the original program to complete the update.
The key code is as follows:
... Cstdiofile CSF; If (! CSF. open (STR + ". upg ", cfile: modecreate | cfile: modewrite | cfile: typebinary | cfile: sharedenywrite) {// first *. the upg file afxmessagebox ("Write File" + STR + "error! /N file in use. Close the program first! ", Mb_iconstop); pfile-> close (); Return false;} Char Buf [2048]; DWORD dwread = 0; while (n = pfile-> Read (BUF, sizeof (BUF)> 0) {dwread + = N; m_prog.setpos (100 * dwread/dwlen); MSG; For (INT I = 0; I <10; I ++) {If (peekmessage (& MSG, null, 0,0, pm_remove) {translatemessage (& MSG); dispatchmessage (& MSG) ;}} CSF. write (BUF, n);} pfile-> close ();... if (: deletefile (STR) {: Rename (STR + ". upg ", STR); m_strstatus = STRF Ile + "complete update! "; Updatedata (false );...
For other details, see source code.
Author Information
Email: yybhz@126.com
Author Home: http://yyb.yeah.net/