Correct way to use InternetReadFile() asynchronously

wininet

Solution

After a more careful reading of the asynchronous example, I see now that it is reading repeatedly until a successful read of 0 bytes is encountered. So to answer my own question, you must call InternetReadFile() over and over again, and be prepared for either a synchronous or asynchronous response.

Problem

I've got code that's performing HTTP requests using WinInet API's asynchronously. In general, my code works, but I'm confused about the 'right' way to do things. In the documentation for InternetReadFile(), it states: To ensure all data is retrieved, an application must continue to call the InternetReadFile function until the function returns TRUE and the lpdwNumberOfBytesRead parameter equals zero. but in asynchronous mode, it may (or may not) return false, and an error of `ERROR_IO_PENDING`, indicating it'll do the work asynchronously, and call my callback when finished. If I read the documentation literally, it seems that the asynchronous calls could also just do a partial read of the requested buffer, and require the caller to keep calling InternetReadFile until a read of 0 bytes is encountered. A typical implementation using `InternetReadFile()` synchronously would look something like this: ``` while(InternetReadFile(Request, Buffer, BufferSize, &BytesRead) && BytesRead != 0) { // do something with Buffer } ``` but with the possibility that any one call to `InternetReadFile()` could signal that it's going to do the work asynchronously (and perhaps read part, but not all of your request), it becomes much more complicated. If I turn to MSDN sample code for guidance, the implementation is simple, simply calling `InternetReadFile()` once, and expecting a single return having read the entire requested buffer either instantly or asynchronously. Is this the correct way to use this function, or is MSDN Sample Code ignoring the possibility that `InternetReadFile()` will only read part of the requested buffer?

Original source