异步调用win32 internet functions

发表于:2007-07-01来源:作者:点击数: 标签:
前段时间在 开发 过程中需要访问网页,用CInternetSession很方便的实现同步访问,但用异步访问是总是弹出assert错误,极不爽快,而我的程序想实现异步访问,在网上搜寻了许久,但所述都不够详细,经鄙人一段时间的摸索,终于成功实现异步访问网页。。。特写

前段时间在开发过程中需要访问网页,用CInternetSession很方便的实现同步访问,但用异步访问是总是弹出assert错误,极不爽快,而我的程序想实现异步访问,在网上搜寻了许久,但所述都不够详细,经鄙人一段时间的摸索,终于成功实现异步访问网页。。。特写此文,以供有需要者共享,如果需要演示代码,可以发信至 ,本人无偿供应源代码。

下面简要叙述一下实现步骤:

1,用InternetOpen建立internet handle

HINTERNET hInternet=InternetOpen("MyProgram/1.0",PRE_CONFIG_INTERNET_ACCESS, 0,0,INTERNET_FLAG_ASYNC)

2, 调用InternetSetStatusCallback建立回调函数

   INTERNET_STATUS_CALLBACK pRet =
    InternetSetStatusCallback(m_hInternet, OnStatusCallback);

3,调用InternetOpenUrl函数打开一个URL

  InternetOpenUrl(hInternet,pstrURL,NULL,0,
   INTERNET_FLAG_TRANSFER_ASCII,
   (DWORD)hWnd);这里的hWnd句柄为接受回调函数发送的消息的窗口句柄

4,处理回调函数

void AFXAPI OnStatusCallback( HINTERNET hInternet,DWORD dwContext, DWORD dwInternetStatus,
         LPVOID lpvStatusInformation,
         DWORD dwStatusInformationLength )
{
// AFX_MANAGE_STATE( AfxGetAppModuleState( ) );
HWND hwnd=(HWND)dwContext
 switch(dwInternetStatus)
 {
 case INTERNET_STATUS_CONNECTION_CLOSED:
  // closed the connection to the server. The lpvStatusInformation parameter is NULL. 
  if(IsWindow(hwnd))
   PostMessage(m_hWndParent,WM_CONNECT_CLOSED,0,0);
  break;
 case INTERNET_STATUS_REQUEST_COMPLETE :
  {
  // by the Win32 API function InternetConnect to indicate that it has created the new handle. This lets the application call the Win32 function InternetCloseHandle from another thread if the connect is taking too long. See the ActiveX SDK for more information about these functions.
   INTERNET_ASYNC_RESULT* res = (INTERNET_ASYNC_RESULT*)lpvStatusInformation;
   HINTERNET hInet = (HINTERNET)(res->dwResult);
   if(IsWindow(hwnd))
    PostMessage(hwnd,WM_REQUEST_COMPLETE,(WPARAM)res->dwError ,(LPARAM)hInet);
  }
  break;
 default:
//  Message("Unknown status:%d",dwInternetStatus);
  break;
 }

}

5,在拥有hwnd句柄的窗口类里处理WindowProc函数,在头文件里定义类变量

HINTERNET m_hInternet

TCHAR m_buf[1024];

INTERNET_BUFFERS ip;

在CPP文件里处理WindowProc函数

LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
 // TODO: Add your specialized code here and/or call the base class
 
 switch(message)
 {
 case WM_CONNECT_CLOSED:
  {
   if(ip.dwBufferLength>0)
   {
    m_buf[ip.dwBufferLength]=0;
    ib.dwBufferTotal+=ip.dwBufferLength;
//    Message(m_buf);//here storage or show m_buf;
   }
   TRACE("--Bufferlen:%d,Total:%d----\n",ib.dwBufferLength,ib.dwBufferTotal);

//////////////here close two internet handle
  }
  break;
 case WM_REQUEST_COMPLETE:
  {
   if(wParam!=0)
   {//error
    char buf[255];
    sprintf(buf,"\r\n------Error:%d--------\r\n",wParam);
    Message(buf);
   }
   else
   {
    HINTERNET hinet=(HINTERNET)lParam;
    if(hinet>(HINTERNET)2)
     m_hInternet=hinet;
    else
    {//=1, meaning last pending complete
     hinet=m_hInternet;
     TRACE("-Buffer:%d--\n",ib.dwBufferLength);
     if(ib.dwBufferLength>0)
     {
      m_buf[ib.dwBufferLength]=0;
      TRACE("---Receive Length:%d\n",ib.dwBufferLength);
      ib.dwBufferTotal+=ib.dwBufferLength;
      // show or storage m_buf

}

// clear m_buf;
     m_buf[0]=0;
    }
    ib.dwBufferLength=1;
    BOOL bOk;

     
    while(ib.dwBufferLength!=0 )
    {
   
     memset(m_buf,0,sizeof(m_buf));
     ib.lpvBuffer = m_buf;
     ib.dwBufferLength = sizeof(m_buf)-1;
     bOk = InternetReadFileEx(hinet, &ib, IRF_ASYNC, (LPARAM)this->m_hWnd );
     if(!bOk && GetLastError()==ERROR_IO_PENDING)
     {
      TRACE("---------Pending----------");
      break;
     }
     if(bOk && ib.dwBufferLength!=0)
     {
      m_buf[ib.dwBufferLength]=0;
      TRACE("---Receive Length:%d\n",ib.dwBufferLength);
      ib.dwBufferTotal+=ib.dwBufferLength;
      Message(m_buf);
      m_buf[0]=0;
     }
    }
   }
  }
  break;
 }

 return CFrameWnd::WindowProc(message, wParam, lParam);
}

5, 如果对以上步骤有不理解者,或者最后不能实现异步访问者,请与我联系,欢迎大家。


 

 


原文转自:http://www.ltesting.net