coder123
coder123

Reputation: 3

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). I need the UWP app to pass a file path to the C++ console app so the console app can process it.

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-2/

As for the parameters, I have this code in my Package.appxmanifest file:

<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

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.

What is being sent as arguments to the C++ app is "/existingFile" from the Package.appxmanifest file.

How can I retrieve the real parameter?

Upvotes: 0

Views: 488

Answers (1)

YanGu
YanGu

Reputation: 3032

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:

  1. 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.
  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).
  4. In your c++ console project, add the necessary header file and code to test the ApplicationData API, for example:
#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.

Upvotes: 1

Related Questions