关闭

做过http异步下载文件的请帮忙看一下

71人阅读 评论(0) 收藏 举报
我用http异步 下载文件时,在下载函数里循环 接收文件数据时没有问题,
do   
{   
INTERNET_BUFFERS InetBuff;   
FillMemory(&InetBuff, sizeof(InetBuff), 0);   
InetBuff.dwStructSize = sizeof(InetBuff);   
InetBuff.lpvBuffer = lpReadBuff;   
InetBuff.dwBufferLength = sizeof(lpReadBuff) - 1;   

if (!InternetReadFileEx(hRequest,   
&InetBuff,   
0, 2))   
{   
if (GetLastError() == ERROR_IO_PENDING)   
{   
 
WaitForSingleObject(hRequestCompleteEvent, INFINITE);   
}   
else   
{   
 
return;   
}   
}   
fwrite(lpReadBuff, sizeof(char),InetBuff.dwBufferLength, pFile);
memset(lpReadBuff, 0, sizeof(lpReadBuff));

if (InetBuff.dwBufferLength == 0)    
bAllDone = TRUE;   

} while (bAllDone == FALSE); 

但是我想在回调函数里处理接收数据,毕竟是异步嘛,数据处理在回调函数里接收比较好
        
        case INTERNET_STATUS_RESPONSE_RECEIVED:   
        {   
                DWORD *dwBytesReceived = (DWORD*)lpStatusInfo;   
                if (*dwBytesReceived == 0)   
                    bAllDone = TRUE;   

                if (pFile)   
                {   
                        if (bAllDone)   
                        {
                                InternetCloseHandle(hRequest);
                                InternetSetStatusCallback( hInstance, NULL);
                                InternetCloseHandle(hInstance);
                                fclose(pFile);
                                return;
                        }
                                fwrite(lpReadBuff, sizeof(char),*dwBytesReceived, pFile);
                        memset(lpReadBuff, 0, sizeof(lpReadBuff));

                        INTERNET_BUFFERS InetBuff;   
                        FillMemory(&InetBuff, sizeof(InetBuff), 0);   
                        InetBuff.dwStructSize = sizeof(InetBuff);   
                        InetBuff.lpvBuffer = lpReadBuff;   
                        InetBuff.dwBufferLength = sizeof(lpReadBuff) - 1;   

                        if (!InternetReadFileEx(hRequest,   
                                &InetBuff,   
                                0, 2))   
                        {   
                                if (GetLastError() == ERROR_IO_PENDING)   
                                {   
                                        WaitForSingleObject(hRequestCompleteEvent, INFINITE);   
                                }   
                                else   
                                {   
                                        return;   
                                }   
                        }   
                } 

但是我在回调函数里接收数据,WaitForSingleObject(hRequestCompleteEvent, INFINITE);   时进入不了INTERNET_STATUS_REQUEST_COMPLETE状态,
但是在主函数里循环 接收就可以进入那个状态,
我的方法有什么问题?求大家解惑!!
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:888次
    • 积分:144
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档