我如何启动的.exe程序与参数UWP

问题描述:

知道我们可以用LaunchFullTrustProcessForCurrentAppAsync(String)方法 和我如何启动的.exe程序与参数UWP

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

启动和发送参数的Win32应用程序。但我最大的问题是:如何在我的win32应用程序中接收该参数(在我的情况下,win32应用程序是我的控制台应用程序)。有没有人有任何帮助。谢谢。在Win32的应用斯特凡答案

更新总是有主要(字串[] args),因此,如果另一个应用程序启动我们的Win32 .exe含有参数(例如: “我的参数” 字符串),参数字符串数组将包含该“我的参数”字符串,我确信。

参数作为参数在Win32进程的Main()函数中传递。

+0

被接受为答案,因为它专注于我的问题。 – GIANGPZO

更好的选择是使用应用服务。

应用程序服务可以允许您在两个应用程序之间来回通信。幸运的是,桌面应用程序存在UWP扩展,它可以帮助您在win32项目中使用应用程序服务。以下是步骤。

1.在您的Win32应用程序

Install-Package UwpDesktop 

2.在您的Win32应用程序创建一个应用程序服务端点

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(); 
       } 
      } 
     } 
    } 

如果您的.exe文件的一部分安装UwpDesktop UWP项目,您的Package.Current.Id.FamilyName应重定向到UWP的PFN。

3.创建UWP应用通道的另一侧

现在,在您UWP应用程序创建一个基本的应用服务

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拉手连接请求

最后,您需要处理传入连接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(); 
} 

虽然我们只返回valueSet只有一个项目,您可以包括其他项目,如特定说明或参数在valueSet。这些将在Win32端提供给您。

这是一个非常简单的例子,从百年队MSDN官方博客中缩减这里找到:

https://blogs.msdn.microsoft.com/appconsult/2016/12/19/desktop-bridge-the-migrate-phase-invoking-a-win32-process-from-a-uwp-app/

为了使其更加坚固,你可以确保你创建在UWP端应用服务连接只有当您的Win32应用程序使用AppServiceTriggerDetails时,才会在博客文章中使用

您还需要在包中声明应用程序服务。appxmanifest文件

<Extensions> 
    <uap:Extension Category="windows.appService"> 
    <uap:AppService Name="CommunicationService" /> 
    </uap:Extension> 
    <desktop:Extension Category="windows.fullTrustProcess" Executable="Migrate.WindowsForms.exe" /> 
</Extensions> 

您可以从此处的博客文章示例:

https://github.com/qmatteoq/DesktopBridge/tree/master/6.%20Migrate

编码愉快。 :)

+0

我认为你的新方法可能比启动exe文件方法更好,但你的答案不关注上述问题。谢谢。 – GIANGPZO