Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access output to stdout from a UWP console application in Windows 10?

I have created a test C++ UWP console application ("app3.exe") in Visual Studio 2017 that prints a string to stdout. When run on Command Prompt or PowerShell, I can see the output, but nothing else can. For example, I can write the command output with app3.exe > a_file.txt and it creates a blank file. I suspect this has something to do with Windows wrapping the actual function (the one with output to stdout) in another calling layer that has no output to stdout. I just want to be able to access the stdout output from my program. Any advice is appreciated.

Here is the sample code:

int main()
{
    printf("This is output");
    std::cout << " to stdout.";
    return(0);
}

I've trimmed it down to the bare bones.

For what it's worth, I'm running on Windows 10 Pro version 1803, build 17134.915.

Updated output.

Further explanation

I want to get built-in sensor data (accelerometer, etc.) from a Microsoft Surface Pro into MATLAB. As I understand things, to access the sensors I need the UWP. I have tried all of the provided UWP console application templates with the same problem: no output registers from the command, as in the above image. Ideally I would run the application from MATLAB using the system command and parse the output there, however there is no output MATLAB or anything else (besides my own eyeballs) can detect when the application is run. After much trouble, I can write the output to a temporary file, but the location changes from device to device so telling MATLAB where to find it is nontrivial. I just want to be able to use the stdout output.

like image 300
Anger Density Avatar asked Jul 26 '19 12:07

Anger Density


People also ask

How do I export from UWP?

After setting that up close the editor settings, go to Project > Export, and select the UWP preset. Under the Signing options click on the folder next to Certificate and go to the certificate file. Then enter the pfxPassword in the password field. Your project will now be automatically signed on export.

How do I run a UWP application?

If you want to open a UWP app, you can go through the Start Menu, the apps list in the Start menu, you can create a desktop shortcut for them, or add them to the start up folder. If you want to open UWP apps from the command line on Windows, you can.

Are UWP apps cross platform?

Well this is where the Uno Platform kicks in – the platform today uses UWP as the definition for the cross platform APIs. In essence, the apps you build for Windows using UWP can be taken cross platform to iOS, Android, Web etc by leveraging Uno.


1 Answers

2019/08/12 16:10 PM

I apologize for previous answers being not really that much specific to UWP, I was unaware of all its details, and I was answering considering more the use of the C++ language on the Windows platform in general.

So it turned out that Universal Windows Platform is a technology developed by Microsoft to allow developers write applications that runs on all Microsoft devices by accessing a common development and security model and a common API.

To be able to write UWP console applications you need to have installed in your Microsoft Visual Studio copy the UWV Console Templates. After you've installed them, you'll need to create a new Console App C++/CX (Universal Windows).

As you will see the template itself adds a Program.cpp file compiled with the following code:

#include "pch.h"

using namespace winrt;

int __cdecl main(){

    wprintf(L"Command line arguments:\n");

    for (int i = 0; i < __argc; i++){
        wprintf(L"__argv[%d] = %S\n", i, __argv[i]);
    }

    wprintf(L"Press 'Enter' to continue: ");
    getchar();
}

Here is the reference to the UWP console example. As specified in the documentation, by dfault UWP applications won't be able to access the filesystem beside the folder they are run from and below. In order to broaden the access to other filesystem location you need to edit the application capability declaration XML file by adding the rescap namespace and the broadFileSystemAccess capability as follows:

<Package
  ...
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp uap5 rescap">
...
<Capabilities>
    <rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>

Without further ado I advice you to carefully read the documentation about the subject at https://docs.microsoft.com/it-it/windows/uwp/files/file-access-permissions to be able to achieve your goal.

As last note: when you will open the documentation with your browser by default it will show you C# code examples, which are not what you want. To see corresponding C++ examples you have to select the language at the top of the page as the following screnshot shows:

Changing the programming language of the examples


2019/07/30 6:41 PM

Considering setting the Subsystem to Console from linker options did not solve your issue, I digged al litte more. By reading at the Windows API documentation for creating a new console I found the following statements:

A graphical user interface (GUI) or console process that is not currently attached to a console can use the AllocConsole function to create a new console. (GUI processes are not attached to a console when they are created. Console processes are not attached to a console if they are created using CreateProcess with DETACHED_PROCESS.)

So basically after having included the Windows.h header this is the workflow you should follow:

  1. Create a console with AllocConsole
  2. Get the standard output handle with GetStdHandle(STD_OUTPUT_HANDLE)
  3. Write on the console with WriteConsole
  4. Release the console with FreeConsole

Here there is a full example that rebinds also C++ standard streams so that you can use directly them as usual in c++.


2019/07/30 3:45 PM

Did you try to set the SUBSYSTEM option in the solution properties or in the command line the Linker section?

/SUBSYSTEM:Console

It actually tells the Windows operating system how to execute the produced executable file. Specifically this option influence the choice of the entry point operated by linker. This seems exactly the behaviour you are explaining in the question.

like image 123
Giova Avatar answered Sep 26 '22 15:09

Giova