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:
With that, let’s get started. You can download the latest public release here:
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:
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")|
|using namespace winrt;|
|using namespace Windows::Networking;|
|using namespace Windows::Networking::Connectivity;|
|for (HostName const & name : NetworkInformation::GetHostNames())|
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.