how to pass parameters from uwp app to pure c++ console app?

Surya Solanki 41 Reputation points
2020-12-29T20:26:22.977+00:00

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, and i can't find a way to access this file from the c++ console app. what's being sent as arguments to the c++ app is "/existingFile" from the Package.appxmanifest file.
how can i retrieve the real parameter from my c++ code?

Developer technologies Universal Windows Platform (UWP)
Developer technologies C++
0 comments No comments
{count} votes

Accepted answer
  1. Yan Gu - MSFT 2,676 Reputation points
    2020-12-30T08:00:24.307+00:00

    Hello,

    Welcome to Microsoft Q&A.

    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.

    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.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.