WinRT for Desktop Apps

August in France means only one thing: vacation time. Now, Christophe Nasarre, Microsoft Premier Field Engineer from Microsoft France, is back from his personal vacation with another video on WinRT development. It is time to go back to the fundamentals with this video post where you'll learn how to use WinRT APIs in your Desktop applications.


During our //Build episode, I quickly presented my WinMDiff tool that displays the differences between two versions of WinRT. The implementation is using the unmanaged reflection API of .NET to load .winmd files containing the WinRT metadata.

This is possible because WinRT is using the same metadata file format as .NET has used for years. So shouldn’t this make easier for a .NET Desktop application to use a WinRT type? It definitively helps! Like a Windows Store App, a Desktop App sees WinRT types as .NET Types and the projection layer does the rest.

Tweaking your Visual Studio project

First, the project must be manually changed in its XML format by adding the following PropertyGroup element:

  <PropertyGroup>

    <TargetPlatformVersion>8.0</TargetPlatformVersion>

  </PropertyGroup>

Once this is done, reload the project and add a reference to the Windows | Core | Windows 255.255.255.255 metadata. After these changes, you can write .NET code that is using WinRT types as if they were other .NET types: IntelliSense and auto-completion will work as expected. Unfortunately, you still need to add a few references to make it compile:

  • System.Runtime.dll: for simple types extension methods
  • System.Runtime.InteropServices.WindowsRuntime.dll: for IBuffer, IInput/OutputStream and IStorageFile/Folder extension methods
  • System.Runtime.WindowsRuntime: for IAsyncOperation extension methods and have await pattern work with WinRT types

Beware the runtime

Even though Visual Studio lets you write and compile code that is using WinRT types, you might end up with exceptions are runtime. The reason is simple: not all WinRT types are supposed to be callable from a Desktop application due to security and sandboxing.

For each WinRT type, the last section of the MSDN documentation tells you if it is usable from a Windows Store App and from a Desktop application. In addition, I’ve provided the DualApi Finder tool that lists the WinRT types callable from a Desktop application. It detects the types that are decorated with the DualApiPartition attribute.

Watch the video for the code sample about how to use WinRT networking and file access APIs in a Desktop application. Last but not least, don’t miss the final part that describes the pitfalls you might face.

For your convenience, here’s the video timeline (note that it will launch a new browser window):

  • [1:00] find out where the WinRT metadata are stored at compile time and at runtime
  • [1:58] how to tweak a Desktop project in Visual Studio to use WinRT APIs
  • [4:28] using the WinRT network profiles in a console application
  • [10:29] look for the missing references to let projection works at compile time
  • [14:58] how to figure out which WinRT type can be used in a Desktop application
  • [19:25] three examples of issues you might face at runtime

Resources

 

Thank you for watching!


Original content from Christophe Nasarre; posted by MSPFE Editor Arvind Shyamsundar