How to launch .exe app with parameter uwp

≡放荡痞女 提交于 2019-12-08 05:28:52

问题


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.


回答1:


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




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



来源:https://stackoverflow.com/questions/45978066/how-to-launch-exe-app-with-parameter-uwp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!