利用Windows的Wininet库,欲实现带速度和进度显示的FTP文件下载。目前可以成功下载文件,但用于实现速度和进度下载状态的回调函数却不能成功调用,一下是核心部分的可编译代码。 
#include <windows.h> 
#include <Wininet.h> 
#include <string> // 回调函数定义,它就是不能被调用,所以在此略去具体实现 
void CALLBACK CallMaster( 
    HINTERNET hInternet, 
    DWORD_PTR dwContext, 
    DWORD dwInternetStatus, 
    LPVOID lpvStatusInformation, 
    DWORD dwStatusInformationLength 


switch (dwInternetStatus) 

case INTERNET_STATUS_REQUEST_COMPLETE: 
// Some code. 
break; 
default: 
// Some code. 
break; 

} int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                    PSTR szCmdLine, int iCmdShow) 

// IP地址,用户名,密码,远程文件路径,下载到本地文件路径在测试时自行设定 
std::basic_string <TCHAR> strIP, strUserName, strPassword, 
strRemoteFilePath, strLocalFilePath; // 例行的初始化工作 
HINTERNET hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC); HINTERNET hSessiont = InternetConnect(hInternet, strIP.c_str(), 
INTERNET_INVALID_PORT_NUMBER, 
strUserName.c_str(), 
strPassword.c_str(), 
INTERNET_SERVICE_FTP, 
INTERNET_FLAG_PASSIVE, 
0); // 设定回调函数,应该也是成功了 
InternetSetStatusCallback(hSessiont, CallMaster); // 开始下载文件:虽然文件可以下载,但绑定的CallMaster函数怎么也不被调用 
FtpGetFile(hSessiont, strRemoteFilePath.c_str(), strLocalFilePath.c_str(), FALSE, FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0); InternetCloseHandle(hSessiont); 
InternetCloseHandle(hInternet);     return 0; 
}

解决方案 »

  1.   

    没用过
    但是看过一点这块的东西
    是不是回调函数应该复写void CALLBACK InternetStatusCallback(
      HINTERNET hInternet,
      DWORD_PTR dwContext,
      DWORD dwInternetStatus,
      LPVOID lpvStatusInformation,
      DWORD dwStatusInformationLength
    );
      

  2.   

    在CallMaster里面一进来就加上log信息等,看是否被回调...
      

  3.   

    InternetSetStatusCallback返回确定正常的?
      

  4.   

    The callback function specified in the lpfnInternetCallback parameter will not be called on asynchronous operations for the request handle when the dwContext parameter of HttpOpenRequest is set to zero (INTERNET_NO_CALLBACK), or the connection handle when the dwContext handle of InternetConnect is set to zero (INTERNET_NO_CALLBACK).