Search code examples
c#.netuwpappxmanifestfull-trust

UWP FullTrustProcessLauncher gives "Element not found" exception on launch


I have a solution in visual studio containing a Windows Application Packaging Project, a UWP project, and a Console Application project. The UWP app contains a single button that when pressed is supposed to launch the console application as a fulltrust process. Solution Explorer looks like this:

Solution Explorer

The Windows Application Packaging project is set as the startup project. It's entry point is set to the UWP app. Both the UWP app and the console app are added to the packaging project as references.

Package References

Configuration manager looks like this:

Configuration Manager

The console app is set as a dependency for the UWP app. The dependencies for the UWP app looks like this:

UWP dependencies

The build order look like this:

Build Order

Code

Here is the code for the console app:

using System;
using System.Diagnostics;

namespace ShellHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

As I mentioned the UWP app only has one page (MainPage.xaml) and it's code behind looks like this:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.ApplicationModel;

namespace OnScreenDeviceManager
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void button_Click(object sender, RoutedEventArgs e)
        {
            await FullTrustProcessLauncher.LaunchFullTrustProcessForAppAsync("Default");
        }
    }
}

The manifest for the UWP app is unmodified. The manifest for the Package has been modified to include fulltrust capability, and it contains the necessary extension for the fulltrust process. The xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>

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

  <Identity
    Name="d3e964dc-9265-4243-b97c-2dacb7c11dac"
    Publisher="CN=Dell"
    Version="1.0.0.0" />

  <Properties>
    <DisplayName>Package</DisplayName>
    <PublisherDisplayName>Dell</PublisherDisplayName>
    <Logo>Images\StoreLogo.png</Logo>
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
    <TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.14393.0" MaxVersionTested="10.0.14393.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="$targetentrypoint$">
      <uap:VisualElements
        DisplayName="Package"
        Description="Package"
        BackgroundColor="transparent"
        Square150x150Logo="Images\Square150x150Logo.png"
        Square44x44Logo="Images\Square44x44Logo.png">
        <uap:DefaultTile Wide310x150Logo="Images\Wide310x150Logo.png" />
        <uap:SplashScreen Image="Images\SplashScreen.png" />
      </uap:VisualElements>
      <Extensions>
        <desktop:Extension Category="windows.fullTrustProcess" Executable="ShellHost\ShellHost.exe">
            <desktop:FullTrustProcess>
                <desktop:ParameterGroup GroupId="SyncGroup" Parameters="/Sync"/>
                <desktop:ParameterGroup GroupId="OtherGroup" Parameters="/Other"/>
                <desktop:ParameterGroup GroupId="Default" Parameters=""/>
            </desktop:FullTrustProcess>
        </desktop:Extension>
      </Extensions>
    </Application>
  </Applications>

  <Capabilities>
    <Capability Name="internetClient"/>
    <rescap:Capability Name="runFullTrust" />
  </Capabilities>
</Package>

The package/debug/bin folder where the solution exe resides looks like this:

Package Directory

You can clearly see the console app exe (ShellHost.exe) in the ShellHost folder.

The console app has been tested and works just fine. The UWP app works just fine except when I click the button I get an 'Element not found' exception.

Exception message says:

System.Exception: 'Element not found. (Exception from HRESULT: 0x80070490)'

This exception was originally thrown at this call stack:
    [External Code]
    OnScreenDeviceManager.MainPage.button_Click(object, Windows.UI.Xaml.RoutedEventArgs) in MainPage.xaml.cs

Can anyone help me solve this? What am I missing?


Solution

  • LaunchFullTrustProcessForAppAsync API means you need to launch the full-trust process with the specified application ID.

    I suggest you try to use LaunchFullTrustProcessForCurrentAppAsync(String) API to replace your code.

     private async void button_Click(object sender, RoutedEventArgs e)
     {
         await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync("Default");
     }