RacoonHug
RacoonHug

Reputation: 13

How does a windows background service get UWP package information?

I am trying to get the package information of a installed UWP application from a windows background service. In this service I used the following code.

var packageManager = new Windows.Management.Deployment.PackageManager();
packageManager.FindPackage("myPackageName");

I keep getting the error "access denied" with no further information! According to the microsoft documents I need to add The packageQuery device capability to my app manifest file. But this "packageQuery" capability is not in the list.

I don't know what to do anymore. I hope you guys can help! Thanks in advance!

Upvotes: 1

Views: 693

Answers (2)

Xie Steven
Xie Steven

Reputation: 8611

No, a background task is part of UWP. I am talking about a Windows Service Application

If it's a classic windows service application, then, your issue was related to call UWP APIs from a classic desktop app.

So, how to check if the UWP APIs are callable from classic desktop app? The above MSDN document has explained it:

This is the process to follow whenever there's a particular UWP API that you'd like to call from your classic desktop app. This process will answer the question of whether the API is allowed to be called from a classic desktop app. First, visit the Windows API reference for Windows Runtime apps, find the reference topic for the class or member API you're interested in, scroll right to the bottom of the topic to the Attributes section, and check whether the DualApiPartition attribute is listed.

If the DualApiPartition attribute is listed, if the API does not need the calling app to have a package identity then the API is allowed to be called from a classic desktop app. If the API does need the calling app to have a package identity then the API is not allowed to be called from a classic desktop app. But the API can be called from a classic desktop app that has been converted to a UWP app.

If the DualApiPartition attribute is not listed, the API is not allowed to be called from a classic desktop app.

Then, checking the Windows.Management.Deployment.PackageManager document, the DualApiPartition attribute is not listed.

So, Windows.Management.Deployment.PackageManager APIs are not allowed to be called from your windows services application.

Upvotes: 0

Martin Zikmund
Martin Zikmund

Reputation: 39102

You have to add this capability manually to the list in the Package.appxmanifest, because it is not available in the manifest designer yet. Open the file as a XML file, there you can declare your capability. First modify the root element as follows:

<Package
    xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
    xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
    xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    IgnorableNamespaces="uap mp wincap rescap">

And now down below find the <Capabilities> element and add your capability:

<rescap:Capability Name="packageQuery" />

Upvotes: 1

Related Questions