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:
[code lang=”cpp”]
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();
}
}
[/code]
And now, the C++/CX version of the same code:
[code lang=”cpp”]
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();
}
};
[/code]
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:
[code lang=”cpp”]
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());
}
[/code]
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:
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.
- GitHub project
- CppCon talks which cover the project in far more detail
- An interview with Kenny Kerr on CppCast
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!