How to launch .exe app with parameter uwp
Asked Answered
O

2

1

I know we can use LaunchFullTrustProcessForCurrentAppAsync(String) method and

<desktop:Extension Category="windows.fullTrustProcess" Executable="fulltrustprocess.exe"> 
  <desktop:FullTrustProcess> 
    <desktop:ParameterGroup GroupId="SyncGroup" Parameters="/Sync"/> 
    <desktop:ParameterGroup GroupId="OtherGroup" Parameters="/Other"/> 
  </desktop:FullTrustProcess> 

to launch and send parameter to win32 app. But my big question is: How to receive that parameter in my win32 app (in my case win32 app is my Console Application). Does anyone has any help. thank you.

Update for Stefan answer
in win32 app always have Main(string[] args), so if another app launch our win32 .exe with parameter (for example: "my parameter" string), args string array will contains that "my parameter" string, I sure that.

Os answered 31/8, 2017 at 9:33 Comment(0)
C
2

The parameters are delivered as arguments in the Main() function of your Win32 process.

Chifley answered 31/8, 2017 at 16:24 Comment(1)
accepted as answer because it focus on my question.Os
S
2

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. :)

Syrian answered 31/8, 2017 at 12:31 Comment(1)
I think your new approach may be better than launching exe file approach, but your answer doesn't focus on above question. thanks.Os
C
2

The parameters are delivered as arguments in the Main() function of your Win32 process.

Chifley answered 31/8, 2017 at 16:24 Comment(1)
accepted as answer because it focus on my question.Os

© 2022 - 2024 — McMap. All rights reserved.