Windows with C++: Asynchronous WinHTTP

My latest Windows with C++ column in the August 2008 issue of MSDN Magazine is now online: Asynchronous WinHTTP.

This article had a bit of a rough time in the editing process and it’s not quite as polished as I would like. Nevertheless it’s still a good read and should give you a good starting point for building high performance and highly responsive HTTP client applications.

It was also heavily edited to meet the limited space requirements of print publication and some sections were dropped. One of them was a section on the positioning of WinHTTP compared to WinInet:

Veteran Windows developers may remember the Windows Internet (WinInet) API that has been around for years.  WinInet still provides a number of unique features such as support for FTP, credential caching, and user interface support. On the other hand it is not an ideal solution for service applications. It is also not suitable if your application needs to manage its own credentials, you don’t want to introduce user interface prompts, or don’t want to rely on Internet Explorer for proxy configuration.

The first paragraph in the Request Cancellation section was also edited to the point where it is incorrect and quite misleading. It should read:

WinHTTP provides a less error-prone model for asynchronous completion when compared to WinInet since your application is always notified of the completion of an operation through the callback function. On the hand, since worker threads are used to execute the callback function, cancelling a request does require some attention to detail.

Finally the section entitled Determining Proxy Settings was cut entirely from the print issue but is available as a sidebar in the online issue, although the associated screenshot was omitted.

Phew. Hopefully these minor issues will be straightened out eventually.

If you’re looking for one of my previous articles here is a complete list of them for you to browse through.

Produce the highest quality screenshots with the least amount of effort! Use Window Clippings.

Published Tuesday, July 29, 2008 6:04 AM by KennyKerr

Comments

# re: Windows with C++: Asynchronous WinHTTP

Tuesday, July 29, 2008 12:31 PM by ak

Good stuff, I'm glad some people are still doing plain c++ without any frameworks. Thank you Kenny!

# re: Windows with C++: Asynchronous WinHTTP

Saturday, August 09, 2008 12:51 PM by malachy

I sent you an email, concerning the failure of the article to to provide a clear statement that the callback function is called in the context of a different thread, and its failure to explain the implications of this (need for synchronization etc).

# re: Windows with C++: Asynchronous WinHTTP

Sunday, August 10, 2008 4:00 PM by KennyKerr

malachy: I didn't get your email (kennykerr@gmail.com). The article does in fact mention that the notifications are handled by worker threads (the thread pool).  I had very limited space to cover quite a large topic. As far as synchronization goes, it's very application specific thus hard to cover generally. Many applications treat WinHTTP with state transitions so no specific synchronization is needed. The only area where you absolutely need to handle it specifically is during cancellation but how that is handled is also very application specific. I did mention the need for this in the section on request cancellation in the article. For example a good approach is to post all notifications to your application's window message loop which can then provide implicit synchronization.

Hope that helps.

# re: Windows with C++: Asynchronous WinHTTP

Saturday, August 16, 2008 9:20 PM by malachy

I sent it again, but this time I sent it to kennykerr@gmail.com.  The first email was sent to the email address given in the printed/on-line article (i.e., it was sent to mmwincpp@microsoft.com)

I recognize that threads were mentioned in the section on cancellation, but they were mentioned only in passing, as a side-effect of the cancellation process.  There was no focus on threading in the more common activites of read completions and response completions.

As for the difficulties in covering the topic more generally, one of the stated goals of the article was to ignore synchronous WinHTTP and instead focus on the asynchronous model.  In fact, the fourth paragraph states that "too much emphasis in documentation and training in general is placed on single-threaded programming to the point where little room is left to discuss parallel programming."  The asynchronous model implicates threading as a core issue, and I feel that the topic should not have been avoided simply because it's a difficult one.

# re: Windows with C++: Asynchronous WinHTTP

Sunday, August 17, 2008 10:30 AM by KennyKerr

malachy: Thanks for the feedback!