The Microsoft Component Object Model has been around for a long time, and has stood the test of time surprisingly well. Let's say you want to build an interesting C++ library and leverage it. What do you have on hand that won't require to start building things from scratch these days?
Considerations
These days, there are a few considerations when designing a system that you'll want to think up-front.
- Do you need proper COM support, or do you just want the design patterns? I consider 'proper COM' to include things like class registration and apartments/marshalling. Design patterns on the other hand are things like 'IUnknown everywhere', reference counting, dynamic interface discovery, and conventions on arguments and object lifetimes.
- If you want proper COM support, should you be looking at WinRT instead? Running on the application container environment (as opposed to desktop/Win32) gives you access to more devices and the Windows Store.
Libraries
- Microsoft Foundation Classes. This is the oldest Microsoft-supported library I'm aware of. It's been out of active development for many, many years. If you're not already using it for some other reason, very unlikely you should. Not supported for store applications either. For a short history, Wikipedia has you covered.
- Active Template Library (ATL). Never quite a header-only library, it achieved static library status in 2013, and it provided the classic patterns for other things to come like the base CComPtr, which has had very few revisions since. Like other successful libraries, it ended up growing a bunch and sprouted other bits of functionality that may not run properly everywhere.
- Windows Runtime C++ Template Library (WRL). The successor to ATL for more modern apps, supporting WinRT. The most interesting foundational capability added in my opinion is the support for weak references, even if you don't care about asynchronous calls and such.
- Windows Implementation Libraries (WIL). Multiple resource wrappers, including COM functionality, but more for consumption than authoring.
- C++/WinRT. The preferred modern way of interacting with WinRT from C++, intended to supercede C++/CX as well as WRL.
Depending on what you need, you might be able to get support from bits and pieces of larger projects that include COM support in some form. Here are a few interesting ones.
- dxc microcom. This library was created to be decoupled from other libraries (as we needed it to work in llvm/clang, where we didn't want to introduce more dependencies), and contributions over time have made it compile on non-Windows platforms as well.
- XPCOM. There is a nsCOMPtr explanation that will highlight the similarities for the base unknown case. I haven't looked at the implementation though. But hey, it's cross-platform!
- dotnet runtime. The .NET projects have a lot of support for many, many interesting things. The PAL includes various cross-platform definitions, and you'll find implementation bits as well.
Happy component-based programming!
Tags:
coding cpp
Home