Using C# types from C++/CX in Windows 8 Store Apps

September 12, 2012

imageOne of the best things happened to Windows 8 Store Apps (aka Metro apps) is the option to write apps and games with pure, native C++, with the help of CX extensions.

Aside from performance gained by using C++, being able to develop with C++ in Windows 8 Apps addresses one of the most requested scenarios: Porting and Code Sharing.

Lets say that you’re the creator of a famous Mobile Application, developed using C++ for Android and other platforms, and you may want to port this great app, or pieces of it to run under Windows 8.

Before running and picking C++ as the main project for your application, first thing to ask: Is it really necessary to develop the UI using C++?

Believe me, it’s not! I’ve been doing it for the past few months, and it’s absolutely unnecessary. If you’re not going to write a Game, the performance benefits using C++ for developing “modern” (Metro) apps is not worth the pain. And it hurts!

So, what I’m suggesting here is that you develop you Windows 8 Store app UI using C#, but keep using your portable C++ code, having a thin, lightweight WinRT layer between them.

The WinRT layer contains Windows Runtime Components. These components are special objects, can be shared across C#/C++/JS thanks to the common type system and the Winmd format, which is the same format used by the .NET framework.

If you ask me, I would prefer developing these components using C++/CX. But  you can also use C# with one disadvantage: Using a shared WinRT component written with C# from C++, causes the .NET runtime to be loaded into your application,  even if it’s a C++ only application.

Inspired by one of the questions asked in the Windows 8 Apps Israeli forum I’m coordinating, I’ve prepared a small application, demonstrates a .NET WinRT component, consumed by C++ using Visual Studio 2012.

First, I’ve created a C#, Windows Store, Windows Runtime Component library (DLL with a Winmd).

Then I’ve renamed the “Class1” sealed class to “InteropType” (pick your own name of course) and added a public method should be exposed to C++.

 

   1:  namespace SharedWinRTLib.Calculator
   2:  {
   3:      public sealed class InteropType
   4:      {
   5:          public double Add(double a, double b)
   6:          {
   7:              return a + b;
   8:          }
   9:      }
  10:  }

 

As you can see (line 5), I’ve added the Add method, which gets two double type parameters, and returns double type.

Important thing to consider when creating WinRT components, is that you must use only platform supported types, such as int, double, string, custom WinRT type, etc.

You can’t use simple .NET types which are not WinRT types, but you can also use interfaces which adhere to these rules.

After creating this WinRT library, I’ve created a simple Windows Store C++ application, and added reference to this Winmd assembly:

Right click on the C++ project > References…, then I’ve picked my Winmd lib, named SharedWinRTLib.

Adding this reference, now I can instantiate and use the InteropType component directly from C++.

   1:  void CppStoreApp::MainPage::Button_Click_1(
   2:      Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
   3:  {
   4:      auto calculator = ref new SharedWinRTLib::Calculator::InteropType();
   5:   
   6:      
   7:      double a = _wtof(textBoxA->Text->Data());
   8:      double b = _wtof(textBoxB->Text->Data());
   9:      double result = calculator->Add(a, b);
  10:      textBoxResult->Text = ref new Platform::String(to_wstring(result).c_str());
  11:  }

In the code snippet above, I’ve instantiated the InteropType component (line 4), which is located under the SharedWinRTLib::Calculator namespace.

Then I’ve converted the Text value of a text-box I’ve added to the UI (line 7, 8), to double, called the Add() method (line 9), and then converted the result to string again using the to_wstring C-Runtime function, and again to Platform::String, then placed the value in the result text-box (line 10).

Simple as that!

You can download the full code from here.

 

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*