【问题标题】:How to launch win32 executable desktop with 4 arguments from UWP project如何使用来自 UWP 项目的 4 个参数启动 win32 可执行桌面
【发布时间】:2019-05-30 22:49:33
【问题描述】:

我正在使用 UWP 项目启动使用 Qt 开发的 win32 应用程序,但我需要传递一些参数(args)。如果我在没有参数的情况下启动(.exe)它可以工作。 我是 UWP 的初学者

感谢您的帮助。

我尝试使用此代码,但没有成功。

ApplicationData.Current.LocalSettings.Values["Parameters"] = tbParameters.Text;            
           await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync("Parameters"); //Parameters

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    假设我正确理解您的问题,请查看以下内容。

    您不能将参数从 UWP 直接传递到您的 Win32 应用。你需要做的是首先将参数存储在你的包 LocalSettings 中,然后在你的 Win32 应用程序中检索它们。

    这是我将四个字符串保存到 LocalSettings 的代码,以便我们稍后可以在 Win32 应用程序中检索它们。再次注意,我们没有直接传递它们,我们只是将它们保存在 LocalSettings 中,我们的 Win32 应用也可以访问 只要它包含在我们的包中

    public static async void PrintFile(String string1, String, string2 String, string3 String string4 )
    {
    
        ApplicationData.Current.LocalSettings.Values["param1"] = string1;
        ApplicationData.Current.LocalSettings.Values["param2"] = string2;
        ApplicationData.Current.LocalSettings.Values["param3"] = string3;
        ApplicationData.Current.LocalSettings.Values["param4"] = string4;                
    
        if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
        {
            try
            {
                await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
            }
    
            catch (Exception Ex)
            {
                Debug.WriteLine(Ex.ToString());
            }
        }
    }
    

    然后在 Win32 应用程序中,只需添加类似以下内容即可从 LocalSettings 检索参数。

    static void Main(string[] args)
    {
        string string1 = ApplicationData.Current.LocalSettings.Values[@"param1"] as string;
        string string2 = ApplicationData.Current.LocalSettings.Values[@"param2"] as string;
        string string3 = ApplicationData.Current.LocalSettings.Values[@"param3"] as string;
        string string4 = ApplicationData.Current.LocalSettings.Values[@"param4"] as string;
    }
    

    编辑

    如果由于某种原因这是一个不可行的解决方案(可能 Win32 应用程序是 3rd 方,因此您无法修改代码),那么您仍然可以使用上述代码来达到相同的结果。

    1) 创建一个新的 Win32 应用(我的示例使用控制台应用)

    2) 像上面一样将参数从 UWP 传递到 LocalSettings。

    3) 在 Win32 应用启动后,将参数从 LocalSettings 拉入,如上。

    4) 从您的 Win32 应用程序启动您的第 3 方 .exe 文件,并使用指定参数,如下所示...

    private static Process CreateProcess(string exePath, string parameter)
    {
        return new Process
        {
            StartInfo =
                {
                    WindowStyle = ProcessWindowStyle.Hidden,
                    FileName = $@"""{exePath}""",
                    Arguments = $@"""{parameter}""",
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
        };
    }
    

    编辑 2

    为了完整起见,LocalSettings 存储在您的应用程序包文件夹中,可以在此处检索:C:\Users\"UserName"\AppData\Local\Packages\"PackageName"\Settings\settings.DAT

    【讨论】:

    • 这里是另一个解决方案:Process.Start(@path.exe", parameter);
    • 在 consol 应用程序中没问题,但在 grafic 模式下不工作,因为无法识别 Process 类,
    • 是的,当然,我只是使用控制台应用程序作为参考分享我的示例。很高兴它对您有用,如果您可以将此答案标记为正确,那么它可以帮助未来的用户,那就太好了:)
    • 谢谢@Adam McMahon,不幸的是我还没有解决我的问题。我在同一个 uwp 项目中创建了一个控制台解决方案,但我无法从按钮启动控制台应用程序。在控制台应用程序中,由于您的帮助,我设法运行带有参数的可执行文件。简而言之:1)app1 type uwp -> 点击一个按钮,启动控制台类型的app2。我被困在这里我不知道该怎么做。 2) app2 -> 自动启动带参数的外部app3。
    • 它工作,感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2018-03-04
    • 2021-04-30
    • 2023-03-21
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多