Getting started with xlang and C++/WinRT

With the unveiling of the xlang project, C++/WinRT is now a completely open source project. While C++/WinRT has always been about providing an open source header-only library for Windows development, the cppwinrt compiler that generates the headers continued as a closed source project. The xlang project represents a rewrite of the C++/WinRT compiler. This isn’t as dramatic as it may seem. I’ve rewritten the C++/WinRT compiler numerous times during its history. I don’t subscribe to the “all of my code is precious” philosophy of software development. Notably, this is the first version of the compiler where I have my very own winmd parser written entirely in portable and standard C++. Previous iterations relied on the metadata APIs inherited from the .NET project and now included in Windows as a set of COM APIs. Tools like MIDL continue to use those APIs, but they are unnecessarily complex, somewhat buggy, and don’t lend themselves to efficient or concise C++ consumption.

I’ll write more specifically about xlang’s metadata library soon, but today I want to show you how you can get started with xlang in general and go from nothing to compiling your own C++ app. And I’ll use a console app for everything to once again reinforce the fact that there are no “store” or app container requirements, as a lot of developers still seem to believe. We are slowly dismantling all of the remaining requirements that code run in an app container and C++/WinRT specifically has never imposed any such restriction. Right, let’s get started.

These instructions are for Windows developers. You can also build xlang on Linux, as our daily build can testify, but I’m still a Windows guy so that’s what I’ll use.

Open a Visual Studio developer command. I would suggest using the latest Visual Studio 2019 preview release as it is slightly less buggy when it comes to handling cmake projects. I’m no cmake expert, but we are using cmake to make it easier to support cross-platform builds if necessary.

In the developer command prompt, clone the xlang repo:

C:\sample> git clone https://github.com/Microsoft/xlang

With the xlang repo in hand, one option is to open the project using Visual Studio:

C:\sample> devenv xlang

Visual Studio’s cmake support is still a bit rough, but it mostly works and is what I use for most of my development on this project.

Alternatively, there is a script that will allow you to build using cmake directly from the command prompt:

C:\sample> xlang\src\scripts\windows\build.cmd -b Release cppwinrt

Once this completes, you should find that the cppwinrt compiler has been built:

C:\sample> dir /s /b cppwinrt.exe
C:\sample\xlang\_build\Windows\x64\Release\tool\cpp\cppwinrt\cppwinrt.exe

You can then copy it locally for convenience:

C:\sample> copy C:\sample\xlang\_build\Windows\x64\Release\tool\cpp\cppwinrt\cppwinrt.exe

And you’re now ready to produce your own headers. One simple way to get started is to target the metadata for your current installation of Windows:

C:\sample> cppwinrt.exe -in local -verbose
tool: C:\sample\cppwinrt.exe (C++/WinRT v2.0.000000.0)
in: C:\WINDOWS\System32\WinMetadata\Windows.AI.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.ApplicationModel.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Data.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Devices.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Foundation.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Gaming.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Globalization.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Graphics.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Management.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Media.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Networking.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Perception.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Security.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Services.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Storage.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.System.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.UI.Xaml.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.UI.winmd
in: C:\WINDOWS\System32\WinMetadata\Windows.Web.winmd
out: C:\sample\
time: 801ms

As you can see, the “local” input directs cppwinrt to read the metadata from the system’s WinMetadata folder. The resulting winrt folder containing all of the C++/WinRT headers and is now available for you to include:

C:\sample> dir /b winrt
base.h
impl
Windows.AI.MachineLearning.h
Windows.AI.MachineLearning.Preview.h
Windows.ApplicationModel.Activation.h
Windows.ApplicationModel.AppExtensions.h
Windows.ApplicationModel.Appointments.AppointmentsProvider.h
Windows.ApplicationModel.Appointments.DataProvider.h
Windows.ApplicationModel.Appointments.h
Windows.ApplicationModel.AppService.h
Windows.ApplicationModel.Background.h
Windows.ApplicationModel.Calls.Background.h
Windows.ApplicationModel.Calls.h
Windows.ApplicationModel.Calls.Provider.h
Windows.ApplicationModel.Chat.h
Windows.ApplicationModel.CommunicationBlocking.h
Windows.ApplicationModel.Contacts.DataProvider.h
Windows.ApplicationModel.Contacts.h
.
.
.

Time to write some code! How about a simple console app that prints out my recent blog posts using the Syndication API:

C:\sample> type app.cpp

#pragma comment(lib, "windowsapp")
#include "winrt/Windows.Foundation.Collections.h"
#include "winrt/Windows.Web.Syndication.h"
#include <stdio.h>

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Web::Syndication;

int main()
{
    Uri uri{ L"https://kennykerr.ca/feed" };
    SyndicationClient client;
    SyndicationFeed feed = client.RetrieveFeedAsync(uri).get();

    for (auto&& item : feed.Items())
    {
        hstring title = item.Title().Text();

        printf("%ls\n", title.c_str());
    }
}

You can then compile it using the Visual C++ compiler as follows:

C:\sample> cl /EHsc /std:c++17 /permissive- /W4 /I . app.cpp

Of course, you can use Clang if you prefer. And then you can simply run the app as follows and, as you can see, this is just a good-old console app:

C:\sample> app.exe
The State of C++ on Windows
xlang
Compilers and Conformance
New in C++/WinRT: Mastering Strong and Weak References
New in C++/WinRT: Async Cancellation Callback for Coroutines
C++/WinRT on Wikipedia
New Features and Changes Coming to C++/WinRT: Header Isolation
C++/WinRT and Beyond
C++/WinRT: Creating Collections Simply and Efficiently
C++/WinRT: Coroutines and the Calling Context

I’m still putting the finishing touches on this update. Once concluded, the xlang version of C++/WinRT will become the next major update to the C++/WinRT project distributed through the regular channels.

And that’s all for today. I hope you enjoy exploring xlang and building apps using C++/WinRT. Stay tuned for more!

9 thoughts on “Getting started with xlang and C++/WinRT

  1. Krzysztof Lesiak

    Nice work! I’m very excited for the upcoming cross-platform functionality and how it all turns out. Keep it up!

    Reply
  2. Kelly Wilkerson

    Kenny – I’ve been looking through the C++/WinRT docs trying to find a good tutorial or book on C++/WinRT, but I’ve been unable to really find anything suitable for a beginner. I have years of experience in VB, Delphi, and C#. I started programming in the mid-80’s with BASIC and FORTRAN. Then on to Pascal in college.

    I currently work as a developer, so I’m not a new programmer. But, when it comes to learning C++/WinRT, I’m at a loss. I really like reading through books and following good real world tutorials while trying to apply what I’m learning in the tutorials.

    Do you have any good references to books and/or tutorials that will teach someone new to C++/WinRT technology but not new to programming. Can’t seem to find anything like that. Most seem to be for current C++ devs.

    Thanks,

    Kelly.

    Reply
    1. Kenny Kerr Post author

      I don’t have any official tutorials on C++/WinRT but there are a lot of samples and how-to or how-it-works articles on my website. The official documentation is also quite good. I would say that if you are new to C++ then your biggest hurdle is C++ itself. C++/WinRT is not difficult to use in itself, but if you are new to C++ then you may find it a steep learning curve. There are some great tutorials and courses on Pluralsight. Otherwise, if you prefer books I would definitely recommend Scott Meyers. A Tour of C++ is also quite a good introduction to C++.

      Reply
      1. sasklrm

        I think the sub-title of what he was saying is that the adoption of C++/WinRT will be limited by the lack of documentation which by extension means lack of interest in new users.

      2. Kenny Kerr Post author

        I’m not sure that’s entirely true. The documentation for C++/WinRT is quite good. There are certainly other reasons why adoptions may be limited. Lack of good Xaml support is chief among them. If there is something lacking in the documentation, I’d love to know what it is.

  3. J. Kelly Wilkerson

    It’s not that the documentation for C++/WinRT is lacking for current C++/CX developers. I’m sure it’s quite good FOR THEM. However, for developers looking to transition to C++/WinRT from C#, VB.NET, or other tools (even C++Builder, which has decent Modern C++ tooling,) there is a serious need for documentation to fill the gap. If C++/WinRT is only for those developers who already know and use Microsoft Visual C++, then that’s fine; I’ll stick with C# or Delphi/C++Builder. But when developers who have years of development experience in other tools see articles called “Getting Started with xlang and C++/WinRT, I feel certain we expect to see something more useful than a simple command-line application that reads an RSS feed. Or, looking through the documentation, find a UWP example that puts a button in the middle of the screen that does nothing.

    I know C++/WinRT is your ‘BABY’ (I’ve seen nearly all of the videos) and I think you and your team did an amazing job bringing what was becoming a train wreck more in line with Standard C++.

    I know Microsoft wants UWP to spread like wildfire so that applications written using it will run on all the different platforms they want to target, but honestly, for real LOB applications, it is, and will continue to be a complete failure. Even WPF leaves much to be desired – too many hoops to jump through just get some simple stuff done and out the door.

    I’d love to get on-board with serious C++ development for LOB applications, but without really good books, like Programming Windows with MFC by Jeff Prosise and Professional MFC with Visual C++ 6 by Mike ‘CanIByAVowelPlease’ Blaszczak, and tutorials for those developers looking to transition to the tooling, it will be nearly impossible.

    I’m totally not minimizing the work you and your team have done already…just would really like good documentation and tutorials for the newbies out here.

    Reply
  4. Ivan Bereziuk

    Hi, Kenny! I landed on this page when was googling “Getting started with C++/WinRT” to find any good tutorials. I like your posts and ideas behind C++/WinRT but I fill that lack of comprehensive tutorials doesn’t play good for this tech. Just wish that someone at Microsoft could take this task from you. C++/WinRT looks very promising.
    Would be nice to see road-map for C++/WinRT? When do you expect for XAML support to be added for C++/WinRT?

    Reply

Leave a Reply to Kenny Kerr Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s