WEB开发网
开发学院软件开发VC WinInet开发中的同步和异步区别 阅读

WinInet开发中的同步和异步区别

 2008-09-05 19:25:32 来源:WEB开发网   
核心提示: 回调函数怎么写啊?看看微软提供的一个例子吧,这个例子是用POST的方式上传数据,WinInet开发中的同步和异步区别(2),比上述下载数据步骤更为麻烦,把InternetOpenUrl这个函数分成更多的函数来处理,有时间再挖挖这个例子,微软这个例子是在一个sendreqexasync.cp

回调函数怎么写啊?看看微软提供的一个例子吧。这个例子是用POST的方式上传数据,比上述下载数据步骤更为麻烦,把InternetOpenUrl这个函数分成更多的函数来处理。有时间再挖挖这个例子。微软这个例子是在一个sendreqexasync.cpp的文件中,在微软的网站上应该可以得到。

省去其他,我们单单看看回调的写法:

......
void __stdcall Callback(HINTERNET hInternet,
       DWORD dwContext,
       DWORD dwInternetStatus,
       LPVOID lpStatusInfo,
       DWORD dwStatusInfoLen)
{
  cout << "Callback dwInternetStatus: " << dwInternetStatus << " Context: " << dwContext << endl;
  cout.flush();
  
  switch(dwContext)
  {
  case 1: // Connection handle
    if (dwInternetStatus == INTERNET_STATUS_HANDLE_CREATED)
    {
      INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
      hConnect = (HINTERNET)pRes->dwResult;
      cout << "Connect handle created" << endl;
      cout.flush();
      SetEvent(hConnectedEvent);
    }
    break;
  
  case 2: // Request handle
    switch(dwInternetStatus)
    {
    case INTERNET_STATUS_HANDLE_CREATED:
      {
        INTERNET_ASYNC_RESULT *pRes = (INTERNET_ASYNC_RESULT *)lpStatusInfo;
        hRequest = (HINTERNET)pRes->dwResult;
        cout << "Request handle created" << endl;
        cout.flush();
      }
      break;
  
    case INTERNET_STATUS_REQUEST_SENT:
      {
        DWORD *lpBytesSent = (DWORD*)lpStatusInfo;
        cout << "Bytes Sent: " << *lpBytesSent << endl;
        dwNumBytesComplete += *lpBytesSent;
      }
      break;
  
    case 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);
      }
      break;
  
    case INTERNET_STATUS_RECEIVING_RESPONSE:
      cout << "Receiving Response" << endl;
      cout.flush();
      break;
  
    case INTERNET_STATUS_RESPONSE_RECEIVED:
      {
        DWORD *dwBytesReceived = (DWORD*)lpStatusInfo;
        cout << "Received " << *dwBytesReceived << endl;
        cout.flush();
      }
    }
  }
}

上一页  1 2 

Tags:WinInet 开发 同步

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接