Configure Windows Live Writer Drafts Folder with SkyDrive
Deep Dive Windows Store Apps in Wien

Calling WinRT from Windows Desktop Apps

The .NET Framework is an API that can be used from server applications, Windows desktop applications, and is available for Windows Store apps. For Windows Store apps just a subset of the framework is available. The Windows Runtime (WinRT) is an API for Windows Store apps. However, some parts of it cannot be used from Windows Store apps, only from Windows desktop apps. The Visual Studio 2012 templates doesn’t give you a direct way to reference this runtime. This article shows how this can be done.

WinRT APIs

Some of the APIs from the Windows Runtime (WinRT) cannot be called from Windows Store apps, they are only available for Windows desktop apps. The documentation of the PackageManager class (namespace Windows.Management.Deployment) shows that this API is only available for desktop apps.

image

References with Windows Store Apps

Creating a Windows Store C# project, the Reference Manager shows a Windows category along with Assemblies, Solution, and Browse:

image

References with Windows Desktop Apps

Creating a desktop application, the Reference Manager lists Assemblies, Solution, COM, and Browse. The Windows category is missing.

image

The way how to add the missing Windows category cannot be found easily. I found it by comparing the project file of a Windows store application with the project file of a console application and found this missing TargetPlatformVersion element that needs to be defined within the PropertyGroup element.

    <TargetPlatformVersion>8.0</TargetPlatformVersion>

This element can be easily added from within Visual Studio by selecting the project in Solution Explorer, using the context menu Unload Project which gives a new context menu to edit the project file.

With the RTM version of Windows Store project templates, this element is no longer used. Maybe the project type GUIDs make the difference now. However, using the TargetPlatformVersion element is documented in the MSDN library. See Windows tab –> Extension Subgroup for more information.

With this change, the Windows tab is available in the Reference Manager:

image

There’s still something missing. Compiling code including the WinRT API gives clear errors that the System.Runtime assembly is missing. This assembly needs to be referenced. This assembly is not in the default list of assemblies in the Reference Manager, but you can browse for it in the directory C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades, or add it directly to the project file.

image

Now the PackageManager from the Windows.Management.Deployment namespace together with the Package class from the Windows.ApplicationModel namespace can be used to show information about installed Windows Store apps.

      var pm = new PackageManager();
      IEnumerable<Package> packages = pm.FindPackages();
      foreach (var package in packages)
      {
        try
        {
          Console.WriteLine("Architecture: {0}", package.Id.Architecture.ToString());
          Console.WriteLine("Family: {0}", package.Id.FamilyName);
          Console.WriteLine("Full name: {0}", package.Id.FullName);
          Console.WriteLine("Name: {0}", package.Id.Name);
          Console.WriteLine("Publisher: {0}", package.Id.Publisher);
          Console.WriteLine("Publisher Id: {0}", package.Id.PublisherId);
          if (package.InstalledLocation != null)
            Console.WriteLine(package.InstalledLocation.Path);
          Console.WriteLine();
        }
        catch (FileNotFoundException ex)
        {
          Console.WriteLine("{0}, file: {1}", ex.Message, ex.FileName);
        }
      }

Thís API also requires administrative privileges, that’s why I’m adding an application manifest file to the project setting the level attribute of the requestedExecutionLevel element to requireAdministrator.

    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
        <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
          <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
        </requestedPrivileges>
      </security>
    </trustInfo>

Now the application runs successfully showing installed Windows Store apps.

More information about the Windows Runtime can be found in my new book Professional C# 2012, and of course in my workshops.

Christian

CN innovation

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

rate professor

The data needs to be synchronized. This is the same with Windows Live Writer. Sometimes I start writing several blog posts, continue working on it, and publishing it on other days.

Sam Royerson

Thanks for this discussion. I have read articles about this subject but yours explains the topic clearly.

Usb Game Controller Drivers

It's really a great and helpful piece of info. I am glad that you simply shared this useful information with us. Please stay us informed like this. Thanks for sharing.

gapps

Excellent. That explains a lot. I've been searching for a solution for some time now.

Alsec

Had some issues running the program for some reason.

sous vide

Thanks, great post.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)