Calling UWP APIs from a Desktop Application

This post is partly an aide memoir. If you have a desktop application then MSDN tells you that you can call UWP APIs from that application;

UWP APIs callable from a classic desktop app

Some of these ‘just work’ from any desktop app whereas others need an ‘app identity’ in order to function correctly and so could only be used by a desktop app that had been packaged as a UWP app by the ‘Centennial’ technology.

But how do you make this work? For a long time, the ever-brilliant Lucian Wischik has been maintaining a project that he’s now moved over to Github named ‘UwpDesktop’;

uwp-desktop project on github

which makes this fairly easy and so I can make a .NET Framework (>4.5) project in Visual Studio 2015 on my Windows 10 machine and then let’s say I add a button to call a click event handler which looks like this.

    void OnClick(object sender, RoutedEventArgs e)
      System.Windows.Clipboard.SetText("I am on the clipboard via .NET");

So far, so good Smile I’m using .NET APIs from a .NET app so everything there is as it should be. If I want to make calls to UWP APIs then I can add the NuGet package via the NuGet console as below;


and I then get a reference in my project to WindowsBase;


and I generally forget how to do this each and every time I try it and that’s why I thought I’d write the post Smile

Now, I can change my click handler to make use of a UWP API as per below;

    void OnClick(object sender, RoutedEventArgs e)
      var package = new Windows.ApplicationModel.DataTransfer.DataPackage();
      package.SetText("I am on the clipboard via UWP");


Note that I left the full namespace names in the code just to make it clear what was going on.

Now, admittedly, I’m not sure that’s a very complete example because there’s clipboard functionality in both UWP and .NET and so you perhaps don’t need the UWP APIs to do this.

Perhaps a more realistic example might be something like the face detection APIs that are present in UWP but aren’t present in .NET although (of course) you can always call out across the network to the Cognitive APIs if you need face detection (and more) from a RESTful endpoint.

But how would I use UWP face detection (e.g.) from a WPF application? Here’s an example click handler bringing in that API to find some faces;

 async void OnClick(object sender, RoutedEventArgs e)
      // Image from the '3 amigos' film...
      var url =

      using (var httpClient = new System.Net.Http.HttpClient())
        var bytes = await httpClient.GetByteArrayAsync(url);

        using (var randomAccessStream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
          // Note that AsBuffer here is System.Runtime.InteropServices.WindowsRuntime.AsBuffer extension.
          await randomAccessStream.WriteAsync(bytes.AsBuffer());

          var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(

          // I don't think there's a common format between the decoder and the
          // face detector so I first go for BGRA8 and then I'll convert it
          // to a format supported by the FaceDetector.
          using (var bitmap = await decoder.GetSoftwareBitmapAsync(
            var detectorFormat = FaceDetector.GetSupportedBitmapPixelFormats().First();

            // Convert for detection.
            using (var detectorBitmap = 
              Windows.Graphics.Imaging.SoftwareBitmap.Convert(bitmap, detectorFormat))
              var detector = await Windows.Media.FaceAnalysis.FaceDetector.CreateAsync();

              // Detect.
              var faces = await detector.DetectFacesAsync(detectorBitmap);

              // Report.
              MessageBox.Show($"I see {faces.Count} faces in that image");

Note again that I’ve tried to leave all the full namespace names in that code to show where the APIs are coming from and also note that I put that together in about 5 minutes so there’s no doubt other/better ways of doing it but it’s the idea that I’m focused on here.

Note that I stuck with MessageBox from .NET here rather than trying to use MessageDialog from the UWP because that latter API depends on having a top level window handle to operate with and in the context of a WPF application that top level window handle wouldn’t be there.

Similarly, if I were to invoke some API that involved the notion of having a ‘package identity’ then I’d get into trouble. A good example might be code that did something like;

    void OnClick(object sender, RoutedEventArgs e)
      var appFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

then I’m going to see an exception reported;


because that API relies on the notion of the calling app being installed as a UWP app on the system and that can only happen if it’s packaged as a UWP app in the first place and that leads back ‘Centennial’ as the means to produce a .appx package from a desktop app which can then run as a ‘true UWP’ app rather than a desktop app calling UWP APIs.

2 thoughts on “Calling UWP APIs from a Desktop Application

Comments are closed.