【问题标题】:How to pass parameters from UWP app to pure C++ console app?如何将参数从 UWP 应用程序传递到纯 C++ 控制台应用程序?
【发布时间】:2021-04-06 10:41:27
【问题描述】:

我有一个启动 C++ 控制台应用程序的 UWP 应用程序(不是 Windows 运行时组件或与 UWP 相关的任何内容)。我需要 UWP 应用将文件路径传递给 C++ 控制台应用,以便控制台应用可以处理它。

作为参考,我关注了这些博客文章:

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

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

至于参数,我的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>

然后我从 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");
}

问题是我发送的 filePath 变量正在使用此路径存储在 C:\Users\14087\AppData\Local\Packages\23930191-5d12-44d5-81c3-808263a5b2f9_qe1bgctg42gkj\Settings\settings.dat 文件中,我找不到从 C++ 控制台应用程序访问此文件的方法。

作为参数发送到 C++ 应用程序的是来自 Package.appxmanifest 文件的 "/existingFile"

我怎样才能找回真正的参数?

【问题讨论】:

标签: c++ uwp parameter-passing console-application


【解决方案1】:

参考document,您可以使用 Microsoft.Windows.CppWinRT NuGet 包配置纯 c++ 控制台应用程序,以使 c++ 控制台应用程序使用 C++/WinRT API,这样您就可以在 C++ 控制台项目中使用ApplicationData API 获取参数。

请检查您的 c++ 控制台项目的以下步骤:

  1. 打开 NuGet 包管理器(选项 Tools > NuGet Package Manager > Manage NuGet Package for Solution...)。
  2. 浏览页面输入cppwinrt,找到Microsoft.Windows.CppWinRT并为你的c++控制台项目安装它。
  3. 打开您的 c++ 控制台项目的 Properties 页面,在 Configuration Properties > General 页面中,将选项 C++ Language Standard 设置为 ISO C++ 17 Standard(/ std:c++ 17).
  4. 在您的 c++ 控制台项目中,添加必要的头文件和代码来测试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");
}

有关 C++/WinRT 的更多信息,您可以参考document

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-21
    • 2012-05-08
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多