With the recent public release of C++/WinRT, I thought I would start writing a few short “how to” and “how it works” articles to help developers understand some of the fundamental principles of the C++ language projection for the Windows Runtime. If you’re wondering what C++/WinRT is or need a more detailed introduction I can suggest the following resources:
moderncpp.com – The original home of C++/WinRT includes all the motivating demos, examples and white papers.
C++/WinRT with Kenny Kerr – My interview on CppCast.
Our CppCon talks on C++/WinRT:
Embracing Standard C++ for the Windows Runtime (Slides)
Putting Coroutines to Work with the Windows Runtime (Slides)
With that, let’s get started. You can download the latest public release here:
https://github.com/microsoft/cppwinrt
One option is to clone the git repository (you can also download it as a ZIP file):
git clone https://github.com/Microsoft/cppwinrt.git Cloning into 'cppwinrt'... remote: Counting objects: 1499, done. remote: Total 1499 (delta 0), reused 0 (delta 0), pack-reused 1499 Receiving objects: 100% (1499/1499), 3.20 MiB | 530.00 KiB/s, done. Resolving deltas: 100% (663/663), done. Checking connectivity... done. Checking out files: 100% (1465/1465), done.
You should now have a folder called cppwinrt that contains your own copy of the repository:
dir /b cppwinrt 10.0.14393.0 Docs Getting Started.md license.txt media README.md
The 10.0.14393.0 folder represents a build or projection of C++/WinRT for the Windows 10 build 14393 SDK (RS1). Within that folder are actually two folders:
dir /b cppwinrt\10.0.14393.0 Samples winrt
The winrt folder contains the actual projection, the header-only library that you need to #include to use C++/WinRT. The Samples folder contains a few Visual C++ projects to get you started. Let’s start with a simple app just to make sure everything’s working:
type HostNames.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma comment(lib, "windowsapp") | |
#include "winrt/Windows.Networking.Connectivity.h" | |
using namespace winrt; | |
using namespace Windows::Networking; | |
using namespace Windows::Networking::Connectivity; | |
int main() | |
{ | |
initialize(); | |
for (HostName const & name : NetworkInformation::GetHostNames()) | |
{ | |
printf("%ls\n", name.ToString().c_str()); | |
} | |
} |
You can then make sure it builds using the Visual C++ compiler as follows:
cl HostNames.cpp /I cppwinrt\10.0.14393.0 /EHsc /std:c++latest Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x64 Copyright (C) Microsoft Corporation. All rights reserved. HostNames.cpp Microsoft (R) Incremental Linker Version 14.00.24215.1 Copyright (C) Microsoft Corporation. All rights reserved. /out:HostNames.exe HostNames.obj
The output should be something like this:
HostNames.exe HOSTNAME 192.168.1.25
Join me next time as we continue to explore C++/WinRT. Got a question? Post it here and we’ll do our best to help.
This is what I get at step cl HostNames.cpp /I cppwinrt\10.0.14393.0 /EHsc /std:c++latest
f:\dev\github\cppwinrt\10.0.14393.0\winrt\internal\../base.h(6): fatal error C1083: Cannot open include file: ‘restrictederrorinfo.h’: No such file or directory
Am I supposed to have C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\um\ in my PATH environment variable ?
Right, the assumption was that you would run this from the Visual Studio tools command prompt where you have your paths already set to include the Windows SDK.
Of course! Thanks for the quick answer.
The C++/WinRT project is a terrific project.
I have a perhaps a kind of stupid question, but I cannot really find out which kind of Visual Studio project is suitable for using the C++/WinRT code. I tried with several but with most some kind of error occurs. What is the official recommendet project type? I have looked at the samples but that was not really helpfull to find out.
I’ll add some posts on creating projects.
Hi Kenny, Using other native windows API in C++ WinRT, is that supported? My understanding is that, since WinRT is running in a security sand box, so, still not all the Windows API are supported. Is that correct?