How to launch win32 executable desktop with 4 arguments from UWP project

Deadly 提交于 2021-02-11 12:10:56

问题


I'm using UWP project to launch an win32 apps developed with Qt, but I need to pass some parameters (args). If I launch (.exe) without parameters it works. I'm a beginner in UWP

Thanks for your help.

I've tried to use this code but was not succesful.

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

回答1:


Assuming I understand your question correctly, please have a look at the following.

You can not pass parameters from UWP directly to your Win32 app. What you have to do is store the parameters in your packages LocalSettings first and then retrieve them within your Win32 app.

This is my code for saving four strings to the LocalSettings so that we can retrieve them later in our Win32 app. Again, note that we're not passing them directly, we're just saving them in the LocalSettings which our Win32 app can also access as long as it is included in our Package.

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

And then in the Win32 app, simply add something like the following in order to retrieve the parameters from the 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;
}

EDIT

If for whatever reason this is an unworkable solution (maybe the Win32 app is 3rd party and therefor you cannot modify the code), then you can still use the above code to achieve the same result.

1) Create a new Win32 app (My example uses a console app)

2) Pass your parameters from UWP to LocalSettings like above.

3) Pull the parameters from LocalSettings into the Win32 app when it has launched, like above.

4) Launch your 3rd party .exe file from your Win32 app with the specified parameters with something like the following...

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

EDIT 2

For completeness, the LocalSettings are stored inside your app package folder and can be retrieved here: C:\Users\"UserName"\AppData\Local\Packages\"PackageName"\Settings\settings.DAT



来源:https://stackoverflow.com/questions/56360532/how-to-launch-win32-executable-desktop-with-4-arguments-from-uwp-project

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