To really start digging into the Metro style apps in C++, I wanted to make a simple component. Currently, there are two ways of making generic HTTP calls from C++ in a Metro application. One is IXMLHTTPRequest2 and the other is Casablanca. IXMLHTTPRequest2 is not really that great of an API. Casablanca is really going to be awesome and suggest folks try it out. When it’s released I’m positive it’ll be the preferred way of doing HTTP in C++. It is pre-beta and the API is going to change in coming releases.
So why did I make this? As a learning exercise. There’s a lot of concepts I wanted to learn and figured a WinRT wrapper around IXMLHTTPRequest2 would be full of surprises.
In the code I have a few things of interest:
- A custom IBuffer implementation made with WRL (Microsoft::WRL::RuntimeClass). WRL will let us mix old style COM interfaces with a runtime object. It’s very well made and way easier than ATL.
- CoCreateInstanceFromApp usage. This is the Metro version of CoCreateInstance and will let you use your own registration free COM objects and Metro approved COM classes.
- Lots of WRL ComPtr. Never use raw COM interface pointers unless you know what you are doing! This will ensure you never miss an AddRef/Release
- WeakReferences. A Circular reference is the Achilles heal for reference counted objects! Learn when to use WeakReferences
I implemented the standard /CX events for callbacks. The new async model (the impl is still async) would have been better, so one could use the continuations or async/await, but I’ll tackle that in another project.
One note, in the sample application I seemed to have odd perf problems with long strings in a XAML text box, so you will see I hard-coded the string to be no more than 200 chars.
As always, this is a prototype for learning and fun. Since I’m still learning, there could be mistakes, bugs and what not
Here’s the code!