Wednesday, January 28, 2015

WRL and the ComPtr

More recently I have been forced out of my comfort zone of C-style systems programming where you manage your own memory and concurrency and in to the world of writing code for WinRT ABI.

In case you don't know, WinRT (universal, modern, tailored, Windows Store?) apps can be written in C#, C++, or even *gasp* JavaScript.  It actually doesn't matter which laguage you use, because they all thunk down to the WinRT ABI.  The ABI is native (read: c++) code, so it is fast and efficient.  Under the covers, with WinRT API is just COM, or more accurately modern COM.  The MIDL syntax has been updated to make way modern runtime APIs.

How does a systems programmer write modern COM for ABI code?  In short, the Windows Runtime Library or WRL. Modern COM still has classic COM under the hood.  One thing they tried to do was to abstract away some of the error prone aspects of COM while making it more developer-friendly.  In some ways it is similar in purpose to ATL but without all of the ATL grossness.  It uses ComPtr instead of CComPtr for smart pointers, and I don't totally hate them like I did CComPtrs.  Modern COM uses a lot of templates types, and doesn't use exceptions.  Most Windows systems programmers avoid exceptions to make the code easier to debug with the KD.  Personally I also hate debugging templated code, but I guess it is needed for the generic programming concepts to work.


ComPtr is the Modern COM smart pointer.  Its behavior is similar to that of the ^ (hat) operator in WinRT CX code.  ComPtr is only for COM objects, and the lifetime automatically managed.  The ^ can be a smart pointer for non-COM things.