Anton2319
Anton2319

Reputation: 160

How to launch Win32 service inside a UWP app?

I am trying to package my UWP app with Desktop Bridge including my Win32 service. My service is a low-level driver that requires admin privileges to run and has a localhost TCP socket to interact with. Service must start as soon as the user opens the app, or be registered with sc.exe to run in the background.

I figured out that I could achieve that by using this code:

Process process = new Process();
process.StartInfo.FileName = exePath;
process.StartInfo.Verb = "runas";
process.Start();

The next thing to do is to determine the exePath. I didn't find any clear explanation of where my executable should be located. This wouldn't be a problem if i deployed my app via .exe installer, but I want to use .msix packaging format.

My win32 driver is mentioned in my packaging project, but I don't see the exe being deployed anywhere when I install .msixbundle to my machine. How can I find this exe?

Upvotes: 0

Views: 501

Answers (1)

Roy Li - MSFT
Roy Li - MSFT

Reputation: 8681

If you want to run the exe from your app with admin privileges, it is correct to use desktop bridge but there is some more work needs to be done. UWP can't directly launch .exe with admin privileges, so it needs another app to do it. Something like this:

UWP app (launch and give parameter) -> desktop app (launch with admin privileges)-> driver

You will need to user WinRT API FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync() to launch the desktop app and then use Process to launch your driver with admin privileges. Also, you will need to modify the manifest file of the project file to add allowElevation capability. The steps should like the following:

  1. Create a UWP application
  2. Create the desktop app, for example a console application
  3. Add the .exe of the driver into the desktop app
  4. Create a Windows Application Packaging Project to package UWP app and desktop app together.
  5. Modify the manifest file of the Packaging Project to add allowElevation capability.
  6. Call LaunchFullTrustProcessForCurrentAppAsync from UWP app to launch the desktop app.
  7. Desktop app will launch the .exe of the driver with admin privileges using Process API.

More details and sample you could find here: Stefan Wick- App Elevation Samples – Part 3

Upvotes: 2

Related Questions