Many programs can Update versions over the Internet. Windows Update is a typical example. To implement this feature, you must first check the application version. How can I check the version of my program on the Internet?
However, if you only want to check the version of the program, you can save the new version information in text form on the Web site. You need to obtain the file information through FTP. File downloading can be implemented through the ready-made Windows Internet API, that is, WinInet, which everyone is familiar with. If you haven't used it, it doesn't matter, this article describes in detail how to use it to write FTP programs. WinInet is easy to use. It has a fixed routine: Step 1 creates a connection, step 2 creates an FTP session, step 3 opens the file, and step 4 reads the file data. The code is like the following:
HINTERNET h = InternetOpen (...);
HINTERNET hftp = InternetConnect (..., INTERNET_SERVICE_FTP ,..);
HINTERNET hftpfile = FtpOpenFile (...);
InternetReadFile (...);
Let's dive into the details and enjoy the highlights. To facilitate code reuse, I wrote the class CWebVersion, which encapsulates all the details and implements a simple function: obtaining the program version information through the Web to check the version. This class is also very convenient to use:
If (CWebVersion: Online ()){
CWebVersion ver ("pub.chinafsdu.net ");
If (ver. ReadVersion ("version.txt"), "pub", "pub "){
DWORD maj = ver. dwVersionMS;
DWORD min = ver. dwVersionLS;
}
}
Below isCWebVersion declaration:
//////////////////////////////////////// ////////////////////////
// WebVersion. h
//
# Pragma once
Class CWebVersion {
Protected:
Enum {BUFSIZE = 64 };
LPCTSTR m_lpServer;// Server name
DWORDM_dwError;// Most recent error code
TCHARM_errInfo [2, 256];// Extended error info
TCHARM_version [BUFSIZ];// Version number as text
VoidSaveErrorInfo ();// Helper to save error info
Public:
DWORD dwVersionMS;// Version number: most-sig 32 bits
DWORD dwVersionLS;// Version number: least-sig 32 bits
CWebVersion (LPCTSTR server): m_lpServer (server ){}
~ CWebVersion (){}
StaticBOOL Online ();
BOOLReadVersion (maid, maid