Search code examples
c#desktop-bridge

Terminate the win32 process of UWP app from main UWP process, or send message to it


I'm creating a UWP application which has a main part which runs on all devices, and also a win32 process which adds some extra features to the app, and runs when the app is running on PCs. (As described here)

Now, this win32 part is optional, and I want users to be able to enable or disable it. So when user enables it, I can run the win32 process (which is located in the same package). But when user disables it, how can I terminate the win32 process?

Somehow I need to send a message to the app asking it to terminate, or killing it. Is there any way to achieve this?

I've looked into app services, but it seems it's only possible to initiate conversation from the Desktop app to UWP app, not the other way round.


Solution

  • When the Win32 process launches, establish an app service connection to the UWP. Now you can tie the lifetime of the Win32 process to the app service connection (by listening to the ServiceClosed event). This way the Win32 process will go away when the UWP process closes, or when UWP app decides to close the app service connection.

    Here is a sample that uses that technique: https://github.com/Microsoft/DesktopBridgeToUWP-Samples/tree/master/Samples/UWP%20Office%20Interop