简体   繁体   English

如何将参数从 UWP 应用程序传递到纯 C++ 控制台应用程序?

[英]How to pass parameters from UWP app to pure C++ console app?

I have a UWP app that launches a C++ console app (not a Windows runtime component or anything related to UWP).我有一个 UWP 应用程序,它启动一个 C++ 控制台应用程序(不是 Windows 运行时组件或与 UWP 相关的任何内容)。 I need the UWP app to pass a file path to the C++ console app so the console app can process it.我需要 UWP 应用程序将文件路径传递给 C++ 控制台应用程序,以便控制台应用程序可以处理它。

For reference, I followed these blog posts:作为参考,我关注了这些博客文章:

https://stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-1/ https://stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-1/

https://stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-2/ https://stefanwick.com/2018/04/06/uwp-with-desktop-extension-part-2/

As for the parameters, I have this code in my Package.appxmanifest file:至于参数,我的Package.appxmanifest文件中有此代码:

<Extensions>
  <desktop:Extension xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" 
                     Category="windows.fullTrustProcess"
                     Executable="PTSExtractionWRT\PTSExtractionWRT.exe">
    <desktop:FullTrustProcess>
      <desktop:ParameterGroup GroupId="ExistingFile" Parameters="/existingFile"/>
    </desktop:FullTrustProcess>
  </desktop:Extension>
</Extensions>

and I launch the console app like so from MainPage.xaml.cs我从 MainPage.xaml.cs 启动控制台应用程序

if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
{
  // store command line parameters in local settings so Launcher can retrieve them 
  ApplicationData.Current.LocalSettings.Values["parameters"] = filePath;
  var appData = ApplicationData.Current.LocalSettings; 
  await Windows.ApplicationModel.FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync("ExistingFile");
}

The problem is that the filePath variable I'm sending is getting stored in the C:\Users\14087\AppData\Local\Packages\23930191-5d12-44d5-81c3-808263a5b2f9_qe1bgctg42gkj\Settings\settings.dat file with this path, and I can't find a way to access this file from the C++ console app.问题是我发送的filePath变量存储在C:\Users\14087\AppData\Local\Packages\23930191-5d12-44d5-81c3-808263a5b2f9_qe1bgctg42gkj\Settings\settings.dat中我找不到从 C++ 控制台应用程序访问此文件的方法。

What is being sent as arguments to the C++ app is "/existingFile" from the Package.appxmanifest file.作为 arguments 发送到 C++ 应用程序的内容是Package.appxmanifest文件中的"/existingFile"

How can I retrieve the real parameter?如何检索真实参数?

Referring to the document , you could configure your pure c++ console app with the Microsoft.Windows.CppWinRT NuGet package to enable the c++ console app use C++/WinRT APIs, so that you can get the parameters by using ApplicationData API in C++ console project. Referring to the document , you could configure your pure c++ console app with the Microsoft.Windows.CppWinRT NuGet package to enable the c++ console app use C++/WinRT APIs, so that you can get the parameters by using ApplicationData API in C++ console project.

Please check the following steps for your c++ console project:请检查您的 c++ 控制台项目的以下步骤:

  1. Open the NuGet Package Manager (option Tools > NuGet Package Manager > Manage NuGet Package for Solution… ). Open the NuGet Package Manager (option Tools > NuGet Package Manager > Manage NuGet Package for Solution… ).
  2. Input cppwinrt in Browse page, find Microsoft.Windows.CppWinRT and install it for your c++ console project.Browse页面输入cppwinrt ,找到Microsoft.Windows.CppWinRT并为您的 c++ 控制台项目安装它。
  3. Open the Properties page for your c++ console project, in Configuration Properties > General page, set the option C++ Language Standard as ISO C++ 17 Standard(/std:c++ 17) . Open the Properties page for your c++ console project, in Configuration Properties > General page, set the option C++ Language Standard as ISO C++ 17 Standard(/std:c++ 17) .
  4. In your c++ console project, add the necessary header file and code to test the ApplicationData API, for example:在您的 c++ 控制台项目中,添加必要的 header 文件和代码以测试ApplicationData API,例如:
#include <iostream>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>

int main()
{
    std::cout << "Hello World!\n";

    winrt::Windows::Storage::ApplicationDataContainer localSettings= winrt::Windows::Storage::ApplicationData::Current().LocalSettings();
    auto values = localSettings.Values();
    //values.Insert(L"exampleSetting", winrt::Windows::Foundation::PropertyValue::CreateString(L"Hello Windows"));
    winrt::hstring val = winrt::unbox_value<winrt::hstring>(values.Lookup(L"parameters"));
    std::wcout << val.c_str() << std::endl;
    system("PAUSE");
}

For more information about C++/WinRT, you could refer to the document .有关 C++/WinRT 的更多信息,您可以参考文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM