{"id":12226,"date":"2016-11-29T07:00:39","date_gmt":"2016-11-28T22:00:39","guid":{"rendered":"https:\/\/jirak.net\/wp\/standard-c-and-the-windows-runtime-cwinrt\/"},"modified":"2016-11-29T07:34:32","modified_gmt":"2016-11-28T22:34:32","slug":"standard-c-and-the-windows-runtime-cwinrt","status":"publish","type":"post","link":"https:\/\/jirak.net\/wp\/standard-c-and-the-windows-runtime-cwinrt\/","title":{"rendered":"Standard C++ and the Windows Runtime (C++\/WinRT)"},"content":{"rendered":"<p>Standard C++ and the Windows Runtime (C++\/WinRT)<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/jirak.net\/wp\/wp-content\/uploads\/2016\/11\/Picture1_revised-1024x579.png\" width=\"1024\" height=\"579\"><\/p>\n<p>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.\u00a0 Most of UWP can also be used by developers targeting traditional desktop applications.<\/p>\n<p>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.\u00a0 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.<\/p>\n<p>Now there\u2019s an easier way for native C++ developers to use the standard, modern C++ language with no extensions, and target the Windows Runtime.\u00a0 <strong>C++\/WinRT<\/strong> is a standard C++ language projection for the Windows Runtime implemented solely in header files.\u00a0 It allows developers to both author and consume Windows Runtime APIs using any standards-compliant C++ compiler.\u00a0 C++\/WinRT is designed to provide C++ developers with first-class access to the modern Windows API.<\/p>\n<p>Let\u2019s look at a simple \u201cHello World\u201d code sample to better understand this new library:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\">\r\n\r\nstruct App : ApplicationT&amp;amp;lt;App&amp;amp;gt;\r\n{\r\n    void OnLaunched(LaunchActivatedEventArgs const &amp;amp;amp;)\r\n    {\r\n        TextBlock block;\r\n\r\n        block.FontFamily(FontFamily(L&amp;amp;quot;Segoe UI Semibold&amp;amp;quot;));\r\n        block.FontSize(72.0);\r\n        block.Foreground(SolidColorBrush(Colors::Orange()));\r\n        block.VerticalAlignment(VerticalAlignment::Center);\r\n        block.TextAlignment(TextAlignment::Center);\r\n        block.Text(L&amp;amp;quot;Hello World!&amp;amp;quot;);\r\n\r\n        Window window = Window::Current();\r\n        window.Content(block);\r\n        window.Activate();\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>And now, the C++\/CX version of the same code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\">\r\n\r\nref class App sealed : public Application\r\n{\r\n    protected:\r\n        virtual void OnLaunched(LaunchActivatedEventArgs^ e) override\r\n        {\r\n            TextBlock^ block = ref new TextBlock();\r\n            block-&amp;amp;gt;FontFamily = ref new FontFamily(&amp;amp;quot;Segoe UI Semibold&amp;amp;quot;);\r\n            block-&amp;amp;gt;FontSize = 72.0;\r\n            block-&amp;amp;gt;Foreground = ref new SolidColorBrush(Colors::Orange);\r\n            block-&amp;amp;gt;VerticalAlignment = VerticalAlignment::Center;\r\n            block-&amp;amp;gt;TextAlignment = TextAlignment::Center;\r\n            block-&amp;amp;gt;Text = &amp;amp;quot;Hello World!&amp;amp;quot;;\r\n\r\n            Window^ window = Window::Current;\r\n            window-&amp;amp;gt;Content = block;\r\n            window-&amp;amp;gt;Activate();\r\n        }\r\n};\r\n\r\n<\/pre>\n<p>As you can see, compared to the second snippet using C++\/CX, the first snippet using C++\/WinRT contains no \u201chats\u201d (^) and no other language extensions.\u00a0 It is 100% pure, modern C++ code.<\/p>\n<p>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.\u00a0 With C#, this is easily done with the <strong>async<\/strong>\/<strong>await<\/strong> construct.\u00a0 With the C++\/WinRT framework, this becomes just as easy using C++ coroutines (<strong>co_await<\/strong>).\u00a0Here\u2019s an example:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\">\r\n\r\nfire_and_forget Async(TextBlock block)\r\n{\r\n    FileOpenPicker picker;\r\n    picker.FileTypeFilter().Append(L&amp;amp;quot;.png&amp;amp;quot;);\r\n    picker.SuggestedStartLocation(PickerLocationId::PicturesLibrary);\r\n    auto file = co_await picker.PickSingleFileAsync();\r\n\r\n    if (file == nullptr)\r\n        return;\r\n\r\n    thread_context ui_thread;\r\n    co_await resume_background();\r\n\r\n    auto stream = co_await file.OpenAsync(FileAccessMode::Read);\r\n    auto decoder = co_await BitmapDecoder::CreateAsync(stream);\r\n    auto bitmap = co_await decoder.GetSoftwareBitmapAsync();\r\n    auto engine = OcrEngine::TryCreateFromUserProfileLanguages();\r\n    auto result = co_await engine.RecognizeAsync(bitmap);\r\n\r\n    co_await ui_thread;\r\n    block.Text(result.Text());\r\n}\r\n<\/pre>\n<p>But what about performance?\u00a0 As it turns out, C++\/WinRT performs better and produces smaller binaries than C++\/CX with identical code.\u00a0 For example, the code sample above produces binaries sized as shown in the table below:<\/p>\n<table>\n<tr>\n<td> <\/td>\n<td><b>C++\/WinRT<\/b><\/td>\n<td><b>C++\/CX<\/b><\/td>\n<td><b>C#<\/b><\/td>\n<\/tr>\n<tr>\n<td>Smallest Binary<\/td>\n<td>53KB + 594KB<\/td>\n<td>86KB + 594KB<\/td>\n<td>261KB + 3.31MB<\/td>\n<\/tr>\n<\/table>\n<p>But, in addition to size savings, code runs faster as well.\u00a0 Here are two examples contrasting the performance of C#, C++\/CX, and C++\/WinRT:<\/p>\n<p><a href=\"https:\/\/winblogs.azureedge.net\/win\/2016\/11\/Picture1_revised.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-42182\" src=\"https:\/\/winblogs.azureedge.net\/win\/2016\/11\/Picture1_revised-1024x579.png\" alt=\"picture1_revised\" width=\"1024\" height=\"579\" \/><\/a><\/p>\n<p><a href=\"https:\/\/winblogs.azureedge.net\/win\/2016\/11\/Picture2_revised.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-42185\" src=\"https:\/\/winblogs.azureedge.net\/win\/2016\/11\/Picture2_revised-1024x577.png\" alt=\"picture2_revised\" width=\"1024\" height=\"577\" \/><\/a><\/p>\n<p>C++\/WinRT is an open source project and ready for use today with Visual Studio 15 Preview and Windows 10 Anniversary Update.\u00a0 Here are some important links to get you started.<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/microsoft\/cppwinrt\">GitHub project<\/a><\/li>\n<li>CppCon talks which cover the project in far more detail\n<ul>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=lm4IwfiJ3EU\">Embracing Standard C++ for the Windows Runtime<\/a>(<a href=\"https:\/\/github.com\/CppCon\/CppCon2016\/raw\/master\/Presentations\/Embracing%20Standard%20C%2B%2B%20for%20the%20Windows%20Runtime\/Embracing%20Standard%20C%2B%2B%20for%20the%20Windows%20Runtime%20-%20Kenny%20Kerr%20and%20James%20McNellis%20-%20CppCon%202016.pdf\">Slides<\/a>)<\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=v0SjumbIips\">Putting Coroutines to Work with the Windows Runtime<\/a>(<a href=\"https:\/\/github.com\/CppCon\/CppCon2016\/raw\/master\/Presentations\/Putting%20Coroutines%20to%20Work%20with%20the%20Windows%20Runtime\/Putting%20Coroutines%20to%20Work%20with%20the%20Windows%20Runtime%20-%20Kenny%20Kerr%20and%20James%20McNellis%20-%20CppCon%202016.pdf\">Slides<\/a>)<\/li>\n<\/ul>\n<\/li>\n<li><a href=\"http:\/\/cppcast.com\/2016\/10\/kenny-kerr\/\">An interview with Kenny Kerr on CppCast<\/a><\/li>\n<\/ul>\n<p>In the future, C++\/WinRT will be expanded to allow WinRT component authoring as well as XAML designer support.\u00a0 Stay tuned for those features, but in the meantime, get started with modern, pure C++ development with the Windows Runtime today!<\/p>\n<p>Source: <a href=\"http:\/\/blogs.windows.com\/buildingapps\/2016\/11\/28\/standard-c-windows-runtime-cwinrt\/\" target=\"_blank\">Standard C++ and the Windows Runtime (C++\/WinRT)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>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.\u00a0 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.\u00a0 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\u2019s an easier way for native C++ developers to use the standard, modern C++ language with <a class=\"mh-excerpt-more\" href=\"https:\/\/jirak.net\/wp\/standard-c-and-the-windows-runtime-cwinrt\/\" title=\"Standard C++ and the Windows Runtime (C++\/WinRT)\">[ more&#8230; ]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":12227,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[201],"class_list":["post-12226","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","tag-windows"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/posts\/12226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/comments?post=12226"}],"version-history":[{"count":1,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/posts\/12226\/revisions"}],"predecessor-version":[{"id":12228,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/posts\/12226\/revisions\/12228"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/media\/12227"}],"wp:attachment":[{"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/media?parent=12226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/categories?post=12226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jirak.net\/wp\/wp-json\/wp\/v2\/tags?post=12226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}