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)
Open Visual studio 2017
File -> Visual C++ -> Windows Universal -> Blank App (Universal Windows) -> CreateCpp
Select the Windows SDK Version
Goto the project properties -> C/ C++ -> Additional Include Directories -> select the winRT folder (Download from GitHub project)
Set the Consume Windows Runtime Extension “NO” and set as Warning Level: Level4(/W4)
C/C++ -> Language -> C++ Language Standard: ISO C++ Latest Draft Standard (/std:C++latest)
C/C++ -> Command Line -> Add -> /Bigobj /permissive - /await
Delete App.xaml and MainPage.xaml files
Add the Midl File (.idl ) file , remove the default template code , add the below code,
Goto pch.h header file, remove the default code and add the below code
- #pragma once
-
- #pragma comment(lib,"windowsapp")
-
- #include <winrt\base.h>
- #include <winrt\Windows.ApplicationModel.h>
- #include <winrt\Windows.ApplicationModel.Activation.h>
- #include <winrt\Windows.Foundation.h>
- #include <winrt\Windows.UI.Xaml.Controls.h>
- #include <winrt\Windows.UI.Xaml.Controls.h>
- #include <winrt\Windows.UI.Xaml.Media.h>
- #include <winrt\Windows.Storage.Streams.h>
- #include <winrt\Windows.Graphics.Imaging.h>
- #include <winrt\Windows.Media.Ocr.h>
- #include <winrt\Windows.Networking.h>
- #include <winrt\Windows.UI.Popups.h>
Add the new class file name (App.CPP) and add the below code
- #include "pch.h"
-
- using namespace winrt;
- using namespace Windows::ApplicationModel;
- using namespace Windows::ApplicationModel::Activation;
- using namespace Windows::Foundation;
- using namespace Windows::UI;
- using namespace Windows::UI::Xaml;
- using namespace Windows::UI::Xaml::Controls;
- using namespace Windows::UI::Xaml::Controls::Primitives;
- using namespace Windows::UI::Xaml::Interop;
- using namespace Windows::UI::Xaml::Media;
- using namespace Windows::UI::Xaml::Navigation;
- using namespace Windows::UI::Popups;
- using namespace Windows::Storage;
-
- struct App:ApplicationT<App>
- {
- public:
- static void OnLaunched(LaunchActivatedEventArgs const&);
- };
-
- void App::OnLaunched(LaunchActivatedEventArgs const&)
- {
-
- TextBlock txtBlock;
- txtBlock.Text(L"Hello World ( Welcome to UWP/C++ tutorial)");
- txtBlock.TextAlignment(TextAlignment::Center);
- txtBlock.VerticalAlignment(VerticalAlignment::Center);
-
- Window window = Window::Current();
- window.Content(txtBlock);
-
- window.Activate();
- }
-
- int __stdcall wWinMain(HINSTANCE,HINSTANCE,PWSTR,int)
- {
- Application::Start([](auto &&) {make<App>(); });
- return 0;
- }
Rebuild the project and Run the application
Output
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.