November 28, 2016 2:00 pm

Standard C++ and the Windows Runtime (C++/WinRT)

The Windows Runtime (WinRT) is the technology that powers the Universal Windows Platform, letting developers write applications that are common to all Windows devices, from Xbox to PCs to HoloLens to phones.  Most of UWP can also be used by developers targeting traditional desktop applications.

WinRT APIs are easily accessible from managed languages like C#, however for native C++ developers, using WinRT either requires a lot of complex COM code, or the use of Visual C++ component extensions, better known as C++/CX.  These language extensions allow C++ to understand the metadata describing WinRT objects, provide automatic reference counting of WinRT objects, and a variety of other things, but are not part of the standard C++ language.

Now there’s an easier way for native C++ developers to use the standard, modern C++ language with no extensions, and target the Windows Runtime.  C++/WinRT is a standard C++ language projection for the Windows Runtime implemented solely in header files.  It allows developers to both author and consume Windows Runtime APIs using any standards-compliant C++ compiler.  C++/WinRT is designed to provide C++ developers with first-class access to the modern Windows API.

Let’s look at a simple “Hello World” code sample to better understand this new library:


struct App : ApplicationT<App>
{
    void OnLaunched(LaunchActivatedEventArgs const &)
    {
        TextBlock block;

        block.FontFamily(FontFamily(L"Segoe UI Semibold"));
        block.FontSize(72.0);
        block.Foreground(SolidColorBrush(Colors::Orange()));
        block.VerticalAlignment(VerticalAlignment::Center);
        block.TextAlignment(TextAlignment::Center);
        block.Text(L"Hello World!");

        Window window = Window::Current();
        window.Content(block);
        window.Activate();
    }
}

And now, the C++/CX version of the same code:


ref class App sealed : public Application
{
    protected:
        virtual void OnLaunched(LaunchActivatedEventArgs^ e) override
        {
            TextBlock^ block = ref new TextBlock();
            block->FontFamily = ref new FontFamily("Segoe UI Semibold");
            block->FontSize = 72.0;
            block->Foreground = ref new SolidColorBrush(Colors::Orange);
            block->VerticalAlignment = VerticalAlignment::Center;
            block->TextAlignment = TextAlignment::Center;
            block->Text = "Hello World!";

            Window^ window = Window::Current;
            window->Content = block;
            window->Activate();
        }
};

As you can see, compared to the second snippet using C++/CX, the first snippet using C++/WinRT contains no “hats” (^) and no other language extensions.  It is 100% pure, modern C++ code.

If you have ever done any WinRT development, you already know that there are many APIs which are asynchronous and are required to be called as such.  With C#, this is easily done with the async/await construct.  With the C++/WinRT framework, this becomes just as easy using C++ coroutines (co_await). Here’s an example:


fire_and_forget Async(TextBlock block)
{
    FileOpenPicker picker;
    picker.FileTypeFilter().Append(L".png");
    picker.SuggestedStartLocation(PickerLocationId::PicturesLibrary);
    auto file = co_await picker.PickSingleFileAsync();

    if (file == nullptr)
        return;

    thread_context ui_thread;
    co_await resume_background();

    auto stream = co_await file.OpenAsync(FileAccessMode::Read);
    auto decoder = co_await BitmapDecoder::CreateAsync(stream);
    auto bitmap = co_await decoder.GetSoftwareBitmapAsync();
    auto engine = OcrEngine::TryCreateFromUserProfileLanguages();
    auto result = co_await engine.RecognizeAsync(bitmap);

    co_await ui_thread;
    block.Text(result.Text());
}

But what about performance?  As it turns out, C++/WinRT performs better and produces smaller binaries than C++/CX with identical code.  For example, the code sample above produces binaries sized as shown in the table below:

C++/WinRT C++/CX C#
Smallest Binary 53KB + 594KB 86KB + 594KB 261KB + 3.31MB

But, in addition to size savings, code runs faster as well.  Here are two examples contrasting the performance of C#, C++/CX, and C++/WinRT:

picture1_revised

picture2_revised

C++/WinRT is an open source project and ready for use today with Visual Studio 15 Preview and Windows 10 Anniversary Update.  Here are some important links to get you started.

In the future, C++/WinRT will be expanded to allow WinRT component authoring as well as XAML designer support.  Stay tuned for those features, but in the meantime, get started with modern, pure C++ development with the Windows Runtime today!

Updated November 29, 2016 8:55 am

Join the conversation

    • There is no announced date as of yet. Keep checking the GitHub project for updates…

  1. Very promising technology. Authoring components and application with C++/WinRT may be an interesting alternative to using C# because the resulting code is so much faster.

  2. Is it preferable to use Coroutines (co_await) or to use ppltasks’s create_task(…).then(…) model for asynchronous code with C++/WinRT? This article seems to use Coroutines while MSDN and the C++/WinRT github guide point to ppltasks

    • I don’t believe that there is a preference for one versus the other. Either will work.

    • Some further info…

      PPL support for C++/WinRT is discouraged and will be removed entirely in the new year. It only exists today for better backcompat with some existing projects. Coroutines are the way to go moving forward.

  3. does this “projection” throw exceptions like C++/CX?
    I am using WRL now