We have developed a C++ WinRT Component DLL & C#.NET application in the post here Under the hood Part 1 : C++ WinRT Component DLL & C#.NET Metro application
we have seen the compiler generated components for making the C# application access the C++ WinRT component here Under the hood Part 2 : C++ WinRT Component DLL & C#.NET Metro application
We have seen the packaging and installation process that happens in the background during building and deploying of the applications here Under the hood Part 3 : WinRT, Windows 8, C++, C#.NET, Metro, WinRT Component DLL
We have seen the C++ XAML application calling the C++ WinRT Component DLL here Under the hood Part 4 : C++ WinRT Component DLL & C++ XAML application – WinRT, Windows 8, C++, Metro
Generally, in a LOB application, we might have to build a C++ Component DLL to take advantage of the performance of C++ in complex or computationally-intensive operations. The C++ component can access Windows operating system services that are not accessible through the Windows Runtime in the current version. Mostly, to reuse existing code that is already written and tested.
Download the source code here.
Create a Windows Runtime C++ Component DLL:
Open Visual Studio 2011 –> File –> New Project –> Go to Installed Templates section –> Visual C++ –>Select WinRT Component DLL and name it as CPPWinRTComponentDll as shown in the following figure.
Fig 1: CPPWinRTComponentDll project
Open WinRTComponent.h file and create an Employee class as shown in the following code snippet. The Platform namespace is where C++ defines its classes that are specific Windows Runtime types.
Create a private variables address_ and employeeid_. These are used as backing store to hold the values. We will use get() & set() properties to set or get these values.
// Backing store for property address_ & employeeid_.
In the public section, add the following code which are properties for the above backing store.
// Property with custom setter/getter
property int EmployeeId
// ‘set’ accessor is missing its value parameter
void set(int value)
if(value <= 0)
throw ref new Platform::InvalidArgumentException();
employeeid_ = value;
We can also add some trivial get/set property with the compiler generating the backing store.
// Trivial get/set property with compiler-generated backing store.
property Platform::String^ Name;
Following is the complete code of the Employee class.
In the Platform::String^ address_, the ^ operator signifies a handle to a Windows Runtime string type that under the covers is reference-counted and deleted when the count reaches zero. Instances of these types are created by using the ref new keywords. Do not explicitly call delete on these instances.
Types must be passed to and from the public methods as Windows Runtime types. If you use the C++ built-in types such as int, double and so on, the compiler will automatically convert them to the appropriate Windows Runtime type. No such conversion occurs unless you are passing the type across the ABI. Complex types such as Platform::String^ must be specified explicitly.
To create a project in this solution, right-click the solution node in Solution Explorer. and select Add Project > Blank Application. Name it as WinWebApp1.
Add a reference to the component project
Fig 3: Adding reference to the C++ WinRT Component DLL.
Add the following HTML into the <body> node of the default.html page
<button id="callwinrt" onclick="CallWinRT()">Call WinRT</button>
<label id ="Label1" style="background-color: grey;">Activation Object Result -> </label><label id ="loaded" style="background-color: #51B65A;"></label>
<label id ="Label2" style="background-color: grey;">Calling Employee::SayHello() method </label><label id ="callmethod" style="background-color: #51B65A;"></label>
<label id ="Label3" style="background-color: grey;" >Getting Employee.Name Property value </label><label id ="retrievedproperty" style="background-color: #51B65A;"></label>
The default.html page appears as following.
// activate the native Windows Runtime component
var nativeObject = new CppWinRTComponentDll.Employee();
document.getElementById(‘loaded’).innerHTML = nativeObject;
//call a method
var num = nativeObject.sayHello();
document.getElementById(‘callmethod’).innerHTML = num;
nativeObject.name = "Kishore Babu";
// get the value of the string property
var propValue = nativeObject.name;
document.getElementById(‘retrievedproperty’).innerHTML = propValue;
The complete code of default.js file is as follows.
Build the solution & deploy the application. If you go to start window, you will find an application WinWebApp1. Click on it.
Be sure to select appropriate capabilities in the package designer. For example, if you are attempting to open a file using the Windows Runtime APIs, be sure to select the Document Library Access checkbox in the Capabilities pane of the package designer.
Download the source code here.
"The task of the leader is to get his people from where they are to where they have not been." — Henry Kissinger