WinInet provides interface ftpgetfile implementation to download files from FTP server, and provides interface FtpPutFile implementation to upload files to an FTP server. Through these two interfaces, we are able to complete the basic FTP file transfer client function. But at the same time of transmission, we want to know how much of the current transmission, the transmission of the entire file about how long it will take, the current progress is how much time, probably how long it will be transferred to finish. We cannot implement this function simply by relying on these two interfaces. Looking through MSDN carefully, you find that WinInet also provides such an interface: Internetsetstatuscallback. This function is the callback function that sets the file transfer, and its prototype is as follows:
This interface sets a state callback function for an Internet session: Lpfninternetcallback, the prototype declaration for this callback function is as follows:
With this callback function, we can control the current transmission progress: we can count how many bytes have been transferred and calculate the transmission rate, so we can tell how much time is left. The following class is the package that implements the FTP file transfer client: ////////////////////////////////////////////////////////////////////////// Module Name: File transfer State control Module function: the implementation of the interface from time to time report the current file transfer status, and implementation upload and download The client function of the file. File name: ftpclientctrl.h //________________________________________________________________________ #ifndef _wininet_ #include <wininet.h> #endif Class Cftpclientctrl { Public Cftpclientctrl (); Virtual ~cftpclientctrl (); Enum { Ftp_begin,//For this event, wparam = Ftp_begin,lparam represents the file size to transfer Ftp_transfer,//For this event, wparam = Ftp_transfer,lparam Indicates the size that has been transmitted Ftp_end//For this event, wparam = Ftp_end,lparam represents the result, 0--fails, 1--succeeds }; Set the connection parameters for the FTP server BOOL Setserverparam (LPCTSTR lpszipaddr,//IP address LPCTSTR Lpszuser,//Login name LPCTSTR lpszpwd,//Login password WORD Wport = internet_default_ftp_port,//FTP server port LPCTSTR lpszproxyname = NULL,//proxy name LPCTSTR lpszproxypwd = NULL); Proxy password Start downloading files BOOL Begindownload (HWND hwnd,//Receive message window LPCTSTR lpszremote,//remote file name LPCTSTR lpszlocal,//save local files DWORD dwfiletype = Ftp_transfer_type_unknown); File transfer BOOL begindownload (DWORD dwthread,//message-receiving thread LPCTSTR lpszremote,//remote file name LPCTSTR lpszlocal,//save local files DWORD dwfiletype = Ftp_transfer_type_unknown); File transfer Start uploading files BOOL Beginupload (HWND hwnd,//Receive message window LPCTSTR lpszremote,//remote file name LPCTSTR lpszlocal,//local file name DWORD dwfiletype = Ftp_transfer_type_unknown); File transfer BOOL beginupload (DWORD dwthread,//message-receiving thread LPCTSTR lpszremote,//remote file name LPCTSTR lpszlocal,//local file name DWORD dwfiletype = Ftp_transfer_type_unknown); File transfer End Transfer File BOOL Endftpfile (); Gets the current FTP session handle Hinternet getcurrentsession () Const{return m_hsession;} Get the current connection handle Hinternet getcurrentconnect () Const{return m_hconnect;} Get remote file name LPCTSTR getcurrentremotefile () Const{return m_szremote;} Get local file name LPCTSTR getcurrentlocalfile () Const{return m_szlocal;} Gets the file type of the current transfer DWORD getcurrentfiletype () Const{return M_dwfiletype;} Get the current transport Receive message window HWND Getcurrentftpwnd () Const{return m_hwnd;} Get the current transport receive message thread DWORD getcurrentftpthread () Const{return M_dwthread;} Gets the size that is currently transferred DWORD getcurrentftpsize () Const{return m_dwcurrentftpsize;} Increments the size that has been transferred DWORD Increaseftpsize (DWORD dwstep); Sign up for a message void Registermsghandle (UINT umsghandle) {m_umsghandle = Umsghandle;} Get a message that has been registered UINT getregistermsghandle () Const{return M_umsghandle;} Get total file length DWORD gettotalfilesize () Const{return m_dwtotalfilesize;} Clear FTP void Clearftpsession (); Set FTP transfer direction void Setftpdirect (BOOL bdownload = TRUE) {m_bftpdirect = bdownload;} Get FTP Transfer Direction BOOL getftpdirect () Const{return m_bftpdirect; Determine if the file type is ASCII Static BOOL Isfileascii (LPCTSTR lpszfile); Protected To get the size of a remote file DWORD getremotefilesize (); To get the size of a local file DWORD getlocalfilesize (); Download files BOOL DownloadFile (LPCTSTR lpszremote,//remote file name LPCTSTR lpszlocal,//save local files DWORD dwfiletype = Ftp_transfer_type_unknown); File transfer Uploading files BOOL UploadFile (LPCTSTR lpszremote,//remote file name LPCTSTR lpszlocal,//save local files DWORD dwfiletype = Ftp_transfer_type_unknown); File transfer Protected Hinternet m_hsession; Applied to InternetOpen Hinternet M_hconnect; Applied to InternetConnect TCHAR m_szftpsrvip[24]; IP Address of FTP server TCHAR m_szftpsrvuser[32]; Login user for FTP server TCHAR m_szftpsrvpwd[32]; Login Password for FTP server TCHAR m_szproxyname[64]; The name of the agent TCHAR m_szproxypwd[32]; // |