please help:about the asynchronous operator
I have pay lots time on the asynchonous operator.
the code is as below,
HANDLE hConnectedEvent, hRequestCompleteEvent;
BOOL bAllDone = FALSE;
HINTERNET hsession, hinternet;
void __stdcall Callback(HINTERNET hInternet,
DWORD dwContext,
DWORD dwInternetStatus,
LPVOID lpStatusInfo,
DWORD dwStatusInfoLen);
int main(int argc, char* argv[])
{
hConnectedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hRequestCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hsession = InternetOpen(_T("ASYNCNET.exe"),
INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
if (hsession == NULL )
{
cout<<"error occur ! the InternetOpen function\n";
return 1;
}
if ( InternetSetStatusCallback(hsession,
(INTERNET_STATUS_CALLBACK)Callback ) ==
INTERNET_INVALID_STATUS_CALLBACK)
{
InternetCloseHandle(hsession);
cout<<"error happen at the internetsetstatuscallback fuction n";
cout<<"the error is "<< GetLastError()<<endl;
return 1;
}
hinternet = InternetOpenUrl(hsession, _T("http://www.google.com"),
NULL, 0, INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE |
INTERNET_FLAG_NO_CACHE_WRITE |INTERNET_FLAG_KEEP_CONNECTION, 1);
if ( hinternet == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
InternetCloseHandle(hsession);
cout<<"internetOpenUrl fails,the error code is
"<<GetLastError()<<endl;
return 1;
}
WaitForSingleObject(hConnectedEvent, INFINITE);
}
BYTE lpReadBuff[256];
BOOL bOk;
do
{
INTERNET_BUFFERS InetBuff;
ZeroMemory(&InetBuff, sizeof(InetBuff));
InetBuff.dwStructSize = sizeof(InetBuff);
InetBuff.lpvBuffer = lpReadBuff;
InetBuff.dwBufferLength = sizeof(lpReadBuff) - 1;
bOk = InternetReadFileEx(hinternet, &InetBuff, 0, 2);
if ( bOk == FALSE)
{
if (GetLastError() == ERROR_IO_PENDING)
{
WaitForSingleObject(hRequestCompleteEvent, INFINITE);
}
else
{
cout << "InternetReadFileEx failed, error " <<
GetLastError()<<endl<<endl<<endl;
cout.flush();
return 1;
}
}
lpReadBuff[InetBuff.dwBufferLength] = 0;
cout << lpReadBuff<<endl;
cout.flush();
if (InetBuff.dwBufferLength == 0)
bAllDone = TRUE;
} while (bAllDone == FALSE);
CloseHandle(hConnectedEvent);
CloseHandle(hRequestCompleteEvent);
InternetCloseHandle(hinternet);
InternetCloseHandle(hsession);
return 0;
}
void __stdcall Callback(HINTERNET hInternet,
DWORD dwContext,
DWORD dwInternetStatus,
LPVOID lpStatusInfo,
DWORD dwStatusInfoLen)
{
switch(dwContext)
{
case 1: // Connection handle
if (dwInternetStatus == INTERNET_STATUS_HANDLE_CREATED)
{
INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT
*)lpStatusInfo;
hinternet = (HINTERNET)pRes->dwResult;
cout << "connect success!!!!" << endl;
cout.flush();
SetEvent(hConnectedEvent);
}
break;
case 2: // Request handle
if (dwInternetStatus ==INTERNET_STATUS_REQUEST_COMPLETE)
{
INTERNET_ASYNC_RESULT *pAsyncRes = (INTERNET_ASYNC_RESULT
*)lpStatusInfo;
cout << "Function call finished" << endl;
cout << "dwResult: " << pAsyncRes->dwResult << endl;
cout << "dwError: " << pAsyncRes->dwError << endl;
cout.flush();
SetEvent(hRequestCompleteEvent);
}
}
}
=======================================================================
I found the bOk = InternetReadFileEx(hinternet, &InetBuff, 0, 2);
then bOk == FALSE ,and the lasterror is
12019(ERROR_INTERNET_INCORRECT_HANDLE_STATE )
and I change some flag(using function InternetOpenUrl) ,the callback
function can't arrive the switch 's case 2!
how this happen?
please help me
thank you!
date: Mon, 18 Aug 2008 03:02:01 -0700 (PDT)
author: dabang