How to execute Process commands (or similar) using a Universal Windows Platform (UWP) App?
Asked Answered
T

2

31

I'm working on creating custom Cortana commands. The commands are registered and executed using a Universal Windows Platform Application. (GitHub)

For instance, I've registered the following command

<Command Name="ShutDown">      
  <ListenFor>Shut down</ListenFor>
  <Navigate/>    
</Command>

To run this function in a UWP application

static async void ShutDown()
{
    var dialog = new MessageDialog("This is where I would shut the computer down.");
    await dialog.ShowAsync();
    //System.Diagnostics.Process.Start("Shutdown", "-s -t 10");
}

But after setting this up I learned System.Diagnostics.Process isn't supported in UWP.

The custom commands I want to run involve some sort of execution such as launching external programs, running other scripts, or opening websites.

It makes sense that UWP doesn't support them given that it's universal and an XBox or a phone might not be able to do these, but I was hoping there was some alternative or hacky way to accomplish this on a Windows 10 PC.

Is there a way for me to execute Process commands or something else with similar functionality in a UWP application? It seems like even though I can get Cortana to execute my C# code, UWP doesn't support much that would be useful in this situation.

Thanks in advance.

Thompson answered 25/11, 2015 at 19:39 Comment(3)
does UWP support using WMI if so then do it using WMIJezabella
@Jezabella Thanks for the idea, it was worth checking out. Unfortunately it doesn't look that WMI is supported either. That's what I mean, I thought registering and executing the commands would be the hard part, I didn't realize the C# would be so limited in UWP.Thompson
bummer. Microsoft wants more apps but they tie our hands tighter. I want to send command to a "trusted" app process - what's the big deal?? Man in the middle attacks? The developer can send and receive json from an app to a server, why not app to app?Neve
S
21

There are - limited - ways to achieve similar behavior.

  1. You could use LaunchUri to trigger other apps which registered for a certain URI-Scheme. This should work for your webbrowser scenario. More details here: https://msdn.microsoft.com/en-us/library/windows/apps/windows.system.launcher.launchuriasync.aspx

  2. You could trigger another app and get results back from it using LaunchForResults. The called app has to support this. More details here: https://msdn.microsoft.com/en-us/library/windows/apps/mt269386.aspx

  3. You could trigger App Services provided by another app. The called app has to support this. The app service will be executed in background. ( I think this is pretty cool.) More details here:http://blogs.msdn.com/b/mvpawardprogram/archive/2015/06/11/writing-windows-10-app-services-in-javascript.aspx

  4. This is a little hacky: I'm not sure if this still works but it did work for Windows 8.1: You could create a so called "Brokered Component". This allows you to trigger everything from you app on you machine, but you won't be able to publish a brokered component into the store. This also allowed Process.Start() on Windows 8.1. It only worked for sideloaded apps. I'm not sure if it still works on Windows 10. More info here: https://msdn.microsoft.com/en-us/library/windows/apps/dn630195.aspx

Summary: Starting another app is pretty easy as long as the target app registered as app service or registered a protocol handler (Uri scheme). Starting scripts or other *.exe is impossible if option 4 doesn't work any longer.

Settle answered 25/11, 2015 at 20:11 Comment(3)
Thanks for your response! I'll try some of these tonight.Thompson
Have anyone try to use AppService to "Shutdown" or "Restart" the tablet? I can't seem to find a way to do it.Bigotry
This document says option 4 is still usable in Windows 10, however it can't be published onto general consumer Windows Store. learn.microsoft.com/en-us/windows/uwp/winrt-components/…Conoid
G
18

With the Windows 10 Anniversary Update (1607) there is an option to enable this scenario on PC. With this API in the Desktop Extension SDK you can launch a fulltrust process that runs at the full user privileges:

https://learn.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.FullTrustProcessLauncher

This way you can light it up on the platforms where it is supported, i.e. PCs running 1607 or above. And your app will still be universal:

if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
{
    await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
}
Granddad answered 16/5, 2017 at 15:45 Comment(2)
But, if I use this, still I can publish my app in the Store?Araroba
Yes, in general you can. You will have to go through the Desktop App publishing process, which will include an onboarding review of your app, since it has the fulltrust capability. You can get that process started here: developer.microsoft.com/en-us/windows/projects/campaigns/…Granddad

© 2022 - 2024 — McMap. All rights reserved.