Inheritance in Windows Runtime Component (C++)

We have a geometry library which defines all geometry objects in our app. Use can either create geometry objects manually in the app, or import geometries from files, for example, CSV, KML, GPX and etc.

The geometry library was originally written in C++. As an example, the following is an example of the CPoint class in the geometry lib.

And then we start using this geometry lib in our Windows project by creating a Windows Runtime Component for it. Basically, the Windows Runtime Component is just a collections of wrapper classes to the original classes written in C++. Naturally, we would like to preserve the inheritance structures in C++ as much as we can.

However, Windows Runtime Component doesn’t have good supports for inheritance. The following is quoted from Ref classes and structs (C++/CX)

However, the Windows Runtime inheritane model not intended as a general inheritance model; in C++/CX this means that a user-defined public ref class cannot serve as a base class.

However, this is one exceptions, which is Windows::UI::Xaml:DependencyObject. We can defined an unsealed class for example MyBase that inherits from DependencyObject, then other ref classes may inherit from MyBase.

And that’s exactly what we did to have inheritance working for Windows Runtime Component. And so far, it seems that’s the only way to do it.

Finally, we start working on KML import. We want to let the important running in background, which make sense because this operation could be very slow depending on the size of the file. Everything works fine until I try to create geometry objects and save them to database. I notice the following exception:

The application called an interface that was marshalled for a different thread.

It is cased by the fact that all our geometry objects are derived from DependencyObject, which can only be used in UI thread. Orz