Search code examples
c#.netwindows-8windows-runtime

About .net and winRT


I'm used to code in .net but I'm kinda new to winRT and have a couple of questions:

  1. When I create a Metro windows store app in c# using winRT and the subset of .net for windows store apps, is this a managed application? (hosted by the clr?) or is it unmanaged and the .net part is simply mapped to winRT.
  2. What happens when there is a reference to a windowsRT component written in c# from an unmanaged language like c++? It makes use of the CLR or is it 100% native?

Solution

  • is this a managed application?

    A WinRT app runs as an out-of-process COM server. Large parts of the code you use is unmanaged, anything you use in your XAML for example was written in C++. But that is an implementation detail that is pretty hard to see, it has all been wrapped carefully by the language projection built into the CLR. Which makes all the WinRT interop look like managed classes. Not unlike the way you can add a reference to a COM component and it will look like managed code as well.

    For all practical purposes you can call your app a managed app and you'll use the managed debugger to fix bugs.

    It makes use of the CLR or is it 100% native?

    Native C++ is 100% native. Same scenario in reverse if you write a WinRT component in C# and it is used in a C++ or Javascript app, those apps have no idea that you wrote your code in C#. It is COM that's underneath it, the universal glue that Microsoft uses to allow different languages to talk to each other. The CLR takes care of the interop, just like it does if you write an add-in for an Office app or use Microsoft.Office.Interop.Excel to automate Excel.

    Note how a .winmd file is pivotal to bridge the gap, the exact equivalent of a COM type library or .NET metadata that tells a compiler what the interfaces look like in a language-independent way. Also visible in some of the restrictions when you write your own WinRT component, your class needs to be sealed since COM does not support implementation inheritance. And cannot be generic. And you can expose a DateTimeOffset but not a DateTime, etcetera.