Добрый день всем.
Если знатоки по функция wininet?
Я не могу скачать файл асинхронно.
bool CFileLoader::LoadFile(LPCTSTR szUrl, LPCTSTR szFilePath, LPCSTR azProxy)
{
ATLTRACE(ldr, DIAG, _T(__FUNCTION__) _T(" %s, %s, %hs\n"), szUrl, szFilePath, azProxy);
if(szUrl == m_strUrl)
return false;
LPSTR szValue = (LPSTR)strCopy.c_str();
if (m_hInternet)
{
m_hInternet.Close();
}
DWORD dwAccessType = INTERNET_OPEN_TYPE_PRECONFIG;
hInt = InternetOpenA(VERSION_PRODUCTNAME, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
InternetSetStatusCallback(hInt, (INTERNET_STATUS_CALLBACK)InternetCallbackProc);
m_strUrl = szUrl;
InternetOpenUrl(hInt, m_strUrl, NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_NO_CACHE_WRITE, (LPARAM)this );
BOOL bOk;
return true;
}
int CFileLoader::InternetCallbackProc(HINTERNET hInternet,
DWORD_PTR dwContext,
DWORD dwInternetStatus,
LPVOID statusInfo,
DWORD statusLen)
{
CFileLoader* pThis = (CFileLoader*)dwContext;
return pThis->InternetHandler(hInternet, dwInternetStatus, statusInfo, statusLen);
}
int CFileLoader::InternetHandler(HINTERNET hInternet,
DWORD dwInternetStatus,
LPVOID statusInfo,
DWORD statusLen)
{
DWORD dwBytesRead = 0;
DWORD dwBytesWritten = 0;
INTERNET_ASYNC_RESULT* res;
res = (INTERNET_ASYNC_RESULT*)statusInfo;
unsigned long _timeout_millis = 1000 * 60;
switch (dwInternetStatus)
{
case INTERNET_STATUS_HANDLE_CREATED:
urlHandle = (HINTERNET)(res->dwResult);
SetEvent(handle_create);
break;
case INTERNET_STATUS_REQUEST_COMPLETE:
{
// Check for errors.
if (LPINTERNET_ASYNC_RESULT(statusInfo)->dwError != 0)
{
//_snprintf(buf, 256, "REQUEST_COMPLETE (%d) Error (%d) encountered", statusLen, GetLastError());
break;
}
// Set the resource handle to the HINTERNET handle returned in the callback.
HINTERNET hInt = HINTERNET(LPINTERNET_ASYNC_RESULT(statusInfo)->dwResult);
char buf[8129];
INTERNET_BUFFERSA ib;
::ZeroMemory(&ib, sizeof(ib));
ib.dwStructSize = sizeof(ib);
// This is not exactly async, but we're assuming it'll complete quickly
// because the update file is small and we now that connection is working
// since we already got headers back
UINT size=0;
BOOL ok;
while (TRUE) {
ib.lpvBuffer = buf;
ib.dwBufferLength = sizeof(buf);
ok = InternetReadFileExA(urlHandle, &ib, IRF_ASYNC, (LPARAM)this);
if (!ok && GetLastError()==ERROR_IO_PENDING) {
DWORD readSize = ib.dwBufferLength;
if (readSize > 0) {
//Должен записывать в файл данные ib.lpvBuffer
}
}
if (!ib.dwBufferLength)
{
ATLTRACE(_T("Error: %i\n"), GetLastError());
break; // read the whole file or error
}
}
free(ib.lpvBuffer);
InternetCloseHandle(urlHandle);
urlHandle = 0;
if (ok) {
// read the whole file
ATLTRACE(_T("The end \n"));
}
}
break;
case INTERNET_STATUS_HANDLE_CLOSING:
InternetCloseHandle(urlHandle);
break;
case INTERNET_STATUS_RESPONSE_RECEIVED:
//SetEvent(request_complete);
break;
}
return false;
}