Skip to content

Using Windows Runtime in a .NET desktop application

jbe2277 edited this page Oct 18, 2019 · 8 revisions

Note: The open-source application Waf Music Manager shows the techniques described in this article.

.NET desktop applications can leverage of the new Windows Runtime (WinRT) API when they run on Windows 8 or newer. This new API comes with an object-oriented design. Internally, it uses COM so that various languages can use the API including native, managed and scripting languages.

Windows Runtime was created for Windows Store Apps. Since Windows 10 these application types are called UWP Apps (Universal Windows Platform). The API reference lists the API set for Windows 10. The complete API set can only be used by UWP apps. Desktop applications can use a reduced set.

Which API sets cannot be used by desktop applications?

  • XAML UI APIs do not work. The UWP XAML framework is a complete new UI technology stack and cannot be mixed with older UI technologies out of the box. Microsoft announced the XAML islands which should allow to use some of the UWP controls in a desktop application.
  • Some APIs depend on an app’s package identity. This identity is managed by the Microsoft Store. Microsoft provides the Desktop Bridge which allows to deploy a desktop application via the Microsoft Store. Additionally, the application gets a package identity which enables the usage of APIs that depends on this identity.

New approach: How to use the Windows Runtime API in a WPF or Windows Forms application?

Microsoft published the Windows 10 WinRT API Pack on NuGet. It simplifies the usage of the latest Windows Runtime APIs in a .NET Framework or .NET Core application.

The Blog post Windows 10 WinRT API Packs released describes how to get started.

Old approach: How to use the Windows Runtime API in a WPF or Windows Forms application?

  1. Unload and edit the Visual Studio project (.csproj) file.
  2. Add the following two references:
<Reference Include="System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll</HintPath>
</Reference>
<Reference Include="Windows">
  <HintPath>$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\10.0.17763.0\Windows.winmd</HintPath>
</Reference>

Note:
1. The HintPath for Windows includes the Windows SDK version number: 10.0.17763.0. You might need to adapt this number.
2. I have replaced the absolute HintPath C:\Program Files (x86) with the MSBuild variable $(MSBuildProgramFiles32).

That is all. The Windows Runtime can be used now in the .NET project. Here is a little example to test if the Windows Runtime integration was successful:

var folder = await StorageFolder.GetFolderFromPathAsync(@"C:\Users");
Console.WriteLine(folder.DisplayName);

Further readings

  1. UWP vs. WPF
  2. Windows Blog: Windows 10 WinRT API Packs released
  3. Windows Blog: Calling Windows 10 APIs From a Desktop Application
Clone this wiki locally