Better option is to use App Services.
An app service can allow you to communicate to and fro between two applications. Fortunately there exists a UWP extension for desktop application which can help you consume app service in your win32 project. Here are the steps.
1. Install UwpDesktop in your Win32 app
Install-Package UwpDesktop
2. Create an App Service endpoint in your Win32 app
private async void btnConfirm_Click(object sender, EventArgs e)
{
AppServiceConnection connection = new AppServiceConnection();
connection.AppServiceName = "CommunicationService";
connection.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
var result = await connection.OpenAsync();
if (result == AppServiceConnectionStatus.Success)
{
ValueSet valueSet = new ValueSet();
valueSet.Add("name", txtName.Text);
var response = await connection.SendMessageAsync(valueSet);
if (response.Status == AppServiceResponseStatus.Success)
{
string responseMessage = response.Message["response"].ToString();
if (responseMessage == "success")
{
this.Hide();
}
}
}
}
If your .exe file is part of the UWP project, your Package.Current.Id.FamilyName
should redirect to the UWP's PFN.
3. Create the other side of the channel in UWP app
Now create a basic app service in your UWP app
AppServiceConnection connection = new AppServiceConnection();
connection.AppServiceName = "CommunicationService";
connection.PackageFamilyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;
connection.RequestReceived += Connection_RequestReceived;
var result = await connection.OpenAsync();
4. Handle connection request
Last, you need to handle incoming connection in Connection_RequestReceived
private async void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
var deferral = args.GetDeferral();
string name = args.Request.Message["name"].ToString();
Result.Text = $"Hello {name}";
ValueSet valueSet = new ValueSet();
valueSet.Add("response", "success");
await args.Request.SendResponseAsync(valueSet);
deferral.Complete();
}
Though we are only returning valueSet
with only one item, you can include other items like specific instructions or parameters in the valueSet
. These will be available to you as result at Win32 side.
This is a very simple example scaled down from official MSDN blog post by centennial team found here:
https://blogs.msdn.microsoft.com/appconsult/2016/12/19/desktop-bridge-the-migrate-phase-invoking-a-win32-process-from-a-uwp-app/
To make it more robust you can make sure you create the app service connection at UWP end only once your Win32 application is up by using AppServiceTriggerDetails
as in the blog post
You will also need to declare app service in Package.appxmanifest file
<Extensions>
<uap:Extension Category="windows.appService">
<uap:AppService Name="CommunicationService" />
</uap:Extension>
<desktop:Extension Category="windows.fullTrustProcess" Executable="Migrate.WindowsForms.exe" />
</Extensions>
You can find the sample from the blog post here:
https://github.com/qmatteoq/DesktopBridge/tree/master/6.%20Migrate
Happy Coding. :)