WinInet异步InternetSetStatusCallback的回调函数有关问题
WinInet异步InternetSetStatusCallback的回调函数问题
微软的代码,回调函数里面访问的hConnect、hRequest等都是全局变量。
而我想实现多文件同时下载或上传,希望回调函数访问类的成员(也就是每一个对象的)。
可是类成员函数不能做回调函数。
加上static又不能访问非静态成员变量。
纠结了,求指导,可能设计有问题。
------解决方案--------------------
用全局变量数组
------解决方案--------------------
用 dwContext 传递指针
------解决方案--------------------
http://hi.baidu.com/ljp2010/blog/item/d28932012134110b7bec2c15.html
看下这个希望对你有帮助
微软的代码,回调函数里面访问的hConnect、hRequest等都是全局变量。
而我想实现多文件同时下载或上传,希望回调函数访问类的成员(也就是每一个对象的)。
可是类成员函数不能做回调函数。
加上static又不能访问非静态成员变量。
纠结了,求指导,可能设计有问题。
- C/C++ code
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;//hConnect全局 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;//hRequest全局 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(); } } } }
------解决方案--------------------
用全局变量数组
------解决方案--------------------
用 dwContext 传递指针
------解决方案--------------------
http://hi.baidu.com/ljp2010/blog/item/d28932012134110b7bec2c15.html
看下这个希望对你有帮助