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 XM
L 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:

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:
- Create a console with AllocConsole
- Get the standard output handle with GetStdHandle(STD_OUTPUT_HANDLE)
- Write on the console with WriteConsole
- 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.
cout << "Some String";
?printf
is really oldschool ;-)