INFO: Using WinInet APIs Asynchronously Within Visual Basic
This article was previously published under Q189850 SUMMARY
This article explains the necessary steps to implement asynchronous WinInet
APIs within Visual Basic. This article will also attempt to explain the
difficulties involved in using the WinInet API in an asynchronous manner.
It should be noted that Microsoft does not recommend using WinInet in Visual Basic in an asynchronous mode. It should also be noted that Microsoft does not support such use. This article is strictly for informational purposes only. MORE INFORMATION
WinInet provides a mechanism to make all I/O calls in an asynchronous
manner (that is, the call is guaranteed to not block, and when the I/O
request completes, a registered callback routine is fired with a specific
set of parameters indicating the completion of the call). To implement
this, a callback function must be defined and registered. To register a
callback routine use the following (from the Internet Client SDK aka
Internet Explorer Author's Toolkit documentation):
InternetSetStatusCallback() will take the handle returned from InternetOpen() or InternetConnect() and a function pointer to the callback routine. In Visual Basic, this function pointer can be obtained using the AddressOf statement: The signature of the callback routine is defined as: in the WININET.H header file (the header file is part of the Internet Client SDK aka Internet Explorer Author's Toolkit). Normally, this should suffice for setting up a callback routine in Visual Basic with WinInet APIs. However, there is a problem with how buffers are passed to calls like InternetReadFile(). In the Microsoft Knowledge Base article 185519 (http://support.microsoft.com/kb/185519/EN-US/) "Vbinet.exe WinInet API Declarations for Visual Basic," InternetReadFile() is declared as follows: While this declaration will work with synchronous WinInet calls within Visual Basic, there will be a problem with asynchronous operations. The problem stems from the fact that when the data arrives over the wire, the Visual Basic run-time will temporarily copy the data into an internal buffer and then format it to fit into the Visual Basic String data type. When the call is invoked in an asynchronous manner, the data is never copied from the temporary buffer. Consequently, the sBuffer variable in the above declaration will never be filled with the data that arrived. To work around this problem, you must manually allocate a buffer for the WinInet to copy the arriving data. For this, you must manage all memory allocation and deallocation using the Win32 memory management APIs (such as LocalAlloc()/LocalFree(), etc...). You must also manage the serialized access to these memory structures using various synchronization objects (mutexes, semaphores, events, etc...), again using the Win32 APIs (CreateMutex()/CreateSemaphore()/CreateEvent()). Once that is accomplished, the declaration of the InternetReadFile() (and all calls like it, such as taking a buffer that WinInet will fill in or read from) must use a declaration like below: The difference is that instead of passing a String data type, you are actually passing the memory address of the newly allocated buffer. The basic flow the application will be as follows:
REFERENCES
Internet Client SDK (Internet Explorer Author's Toolkit)
Microsoft Visual C++ On-Line Win32 API Documentation For additional information, please see the following article in the Microsoft Knowledge Base: 185519 (http://support.microsoft.com/kb/185519/EN-US/)
: Vbinet.exe WinInet API Declarations for Visual Basic
APPLIES TO
| Article Translations
|

Back to the top
