Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News C++ Component Extensions: The New Face of COM

C++ Component Extensions: The New Face of COM

This item in japanese

COM Programming is alive and well on the Windows platform and a new variant of C++ makes it much more approachable. Known as C++ Component Extensions, this new language was used to create the new Windows runtime, WinRT.

Though based on COM, C++ Component Extensions looks more like .NET programming than anything else. To being with, you will work directly with classes and objects rather than through the COM interfaces.

C++ components will start with a public class declared with the signature “public ref class XXX sealed” where XXX is the class name. A class so decorated is known as an “activatable class” and can be consumed by .NET and JavaScript-based applications. The “ref” keyword indicated the class is a “Windows Runtime compatible type”. The “sealed” keyword prevents inheritance, a requirement if the class is to be consumed by JavaScript. Though the documentation is fuzzy on this point, it appears that classes not used from JavaScript do not need to be sealed. For example, the Button class inherits from ButtonBase. In addition to classes, C++ Component Extensions supports structs. However a Windows Runtime struct is limited to just naked data members.

Classes may contain constructors, methods, properties, and events. Outside of the class itself you may find delegates signatures for use by the events. While this isn’t C++/CLI the syntax is remarkably similar complete with the use of the ^ symbol for handles.

Memory in C++ Component Extensions is handled differently depending on the semantics of the language that consumes it. If a C++ application uses the library then objects will be reference counted. .NET consumers will of course use mark-and-sweep garbage collection.

Public methods that are exposed across the ABI or Abstract Binary Interface. Such methods must use Windows Runtime types for parameters. C++ built in types such as int and double will be automatically converted; other types must be declared explicitly. For public methods Platform::String is used, internally Microsoft recommends using the standard C++ string type.

Custom exception types are not supported across the ABI. Developers must throw one of the built-in exception types. If additional information is needed one can throw the generic COMException which takes a HRESULT as a parameter.

While JavaScript supporting classes are sealed, they can at least implement interfaces.

Rate this Article