Learn Universal Windows Programming Via Modern C++

Introduction

In this article, we are going to learn how to implement the C++/UWP features using Visual Studio 2017. 

Quick Introduction

Universal Windows Program has been developed based on the Windows Runtime (WinRT) technology. Windows Runtime (hereafter called as WinRT) runs based on the COM API, but is not designed to be directly used, instead using “language projections” concept to the call WinRT APIs.

Language projections has encapsulated complete implementation of COM concept and it provides a more natural programming experience to the call WinRT APIs. Ex: C# or Java script call the WinRT API as normal function calling. UWP (WinRT) application can be developed in C#, VB, Java script, C++ & more languages.

In C++ developer, you can’t directly use the WinRT API, you have to use C++/CX language extensions (i.e. this means, you can’t directly use the UWP (WinRT) into the C++ language) and is a little bit complicated Microsoft has brought UWP features directly into C++ and First RTM build has been released in Windows 10 Anniversary Update SDK.

Note

Current release does not support default template project and XAML design support, those concepts will be supported in the feature.

We can’t directly use the Visual studio to develop C++/UWP applications, we should do some changes to the Visual studio settings to use it. In this article I will mainly focus on how to develop C++/UWP applications using Visual Studio 2017.

“Microsoft focuses C++ on the Future”, and new  microsoft development has recommended to use the C++ not for C++/CX 

Prerequisites

Visual C++ 2015 with Update 3 or Visual studio 2017 with VC ++ component installation and Windows 10 Anniversary update SDK or higher version.

In this sample we haev developed in Visual studio 2017 (VC ++ UWP package has installed) and

Windows 10 build 16251 (Windows 10 Insider preview version) 

Download C++ WinRT header files

Download the WinRT header files into the GitHub (download Zip folder, Ref: below image) Extract the Zip Folder Copy the winrt folder to any drive (In my program sample, I have placed the folder into the C Drive)

UWP

Open Visual studio 2017

File -> Visual C++ -> Windows Universal -> Blank App (Universal Windows) -> CreateCpp

UWP 

Select the Windows SDK Version

UWP

Goto the project properties -> C/ C++ -> Additional Include Directories -> select the winRT folder (Download from GitHub project)

UWP

Set the Consume Windows Runtime Extension “NO” and set as Warning Level: Level4(/W4)

UWP

C/C++ -> Language -> C++ Language Standard: ISO C++ Latest Draft Standard (/std:C++latest)

UWP

C/C++ -> Command Line -> Add -> /Bigobj /permissive - /await

UWP

Delete App.xaml and MainPage.xaml files

UWP

Add the Midl File (.idl ) file , remove the default template code , add the below  code,

  1. namespaceCreateCpp    
  2. {    
  3. }  
UWP

 

Goto pch.h header file, remove the default code and add the below code

  1. #pragma once  
  2.  
  3. #pragma comment(lib,"windowsapp")  
  4.  
  5. #include <winrt\base.h>  
  6. #include <winrt\Windows.ApplicationModel.h>  
  7. #include <winrt\Windows.ApplicationModel.Activation.h>  
  8. #include <winrt\Windows.Foundation.h>  
  9. #include <winrt\Windows.UI.Xaml.Controls.h>  
  10. #include <winrt\Windows.UI.Xaml.Controls.h>  
  11. #include <winrt\Windows.UI.Xaml.Media.h>  
  12. #include <winrt\Windows.Storage.Streams.h>  
  13. #include <winrt\Windows.Graphics.Imaging.h>  
  14. #include <winrt\Windows.Media.Ocr.h>  
  15. #include <winrt\Windows.Networking.h>  
  16. #include <winrt\Windows.UI.Popups.h>  

Add the new class file name (App.CPP) and add the below code

  1. #include "pch.h"  
  2.   
  3. using namespace winrt;  
  4. using namespace Windows::ApplicationModel;  
  5. using namespace Windows::ApplicationModel::Activation;  
  6. using namespace Windows::Foundation;  
  7. using namespace Windows::UI;  
  8. using namespace Windows::UI::Xaml;  
  9. using namespace Windows::UI::Xaml::Controls;  
  10. using namespace Windows::UI::Xaml::Controls::Primitives;  
  11. using namespace Windows::UI::Xaml::Interop;  
  12. using namespace Windows::UI::Xaml::Media;  
  13. using namespace Windows::UI::Xaml::Navigation;  
  14. using namespace Windows::UI::Popups;  
  15. using namespace Windows::Storage;  
  16.   
  17. struct App:ApplicationT<App>  
  18. {  
  19. public:  
  20.     static void OnLaunched(LaunchActivatedEventArgs const&);  
  21. };  
  22.   
  23. void App::OnLaunched(LaunchActivatedEventArgs const&)  
  24. {  
  25.   
  26.     TextBlock txtBlock;  
  27.     txtBlock.Text(L"Hello World ( Welcome to UWP/C++ tutorial)");  
  28.     txtBlock.TextAlignment(TextAlignment::Center);  
  29.     txtBlock.VerticalAlignment(VerticalAlignment::Center);  
  30.   
  31.     Window window = Window::Current();  
  32.     window.Content(txtBlock);  
  33.   
  34.     window.Activate();  
  35. }  
  36.   
  37. int __stdcall wWinMain(HINSTANCE,HINSTANCE,PWSTR,int)  
  38. {  
  39.     Application::Start([](auto &&) {make<App>(); });  
  40.     return 0;  
  41. }  

Rebuild the project and Run the application

Output

UWP
Conclusion

Hope you have understood, how to use C++ language to develop UWP app, in upcoming articles we will learn more about on C++/UWP.


Similar Articles