Tag Archives: EXE

Under the hood Part 4 : C++ WinRT Component DLL & C++ XAML application – WinRT, Windows 8, C++, Metro

Calling WinRT Component from C++

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

Going further, I created a C++ Metro application and accessed the C++ WinRT Component DLL from this application. The interesting part here is that C++ applications is XAML based. No more .RC and resource.h files in C++ (for metro). In the previous post, we created a WinRT C++ DLL that contains a class calculatorSample. Now let us create  a C++ application to consume the C++ WinRT DLL.

To get started with creating a C++ XAML application, go to Visual Studio 2011 –> Solution Explorer–> New Project –> Go to Installed Templates section –> Visual C++ –>Select Application and name it as CPPApplication1 as shown in the following fig 1.

image

Fig 1: Creating a C++ XAML application.

Right click on the project, click on Add References and the following dialog shown in figure 2 comes up.

image

Fig 2: Reference manager dialog to add references to another components.

Click on Solution section –> Projects and Select  CppWinRTComponentDLL –> click on Add button and click on Close as shown in figure 3.

image

Fig 3: Selecting WinRT CppWinRTComponentDLL.

The CppWinRTComponentDLL reference appears in the References section as shown in the following figure 4.

image

Fig 4: CppWinRTComponentDLL dll appears in the References section.

Goto MainPage.xaml.cpp and include the namespace for CppWinRTComponentDLL

Code Snippet
using namespace CppWinRTComponentDll;

 

Then create an object of calculatorSample on the heap using ref new. In this scenario, ref new  is like cocreateinstance of COM. It’s a smart allocator. we also use ref class to indicate the authoring of a Windows Runtime class . using ^ to represent a “refcounted” pointer in ZW fits quite well

The following code shows how to use the ref new expression to create a new reference-counted Windows Runtime object. Note that you use the ^ (“hat”) symbol instead of the pointer dereference operator (*) when declaring the variable, but that you use the familiar -> operator to access the objects instance members. Note also that you do not call delete explicitly on the object. The object will be destroyed deterministically when the last remaining copy of it goes out of scope. At the lowest level, the object is basically a COM object owned by a smart pointer.

Code Snippet
CalculatorSample^ calcobj = ref new  CalculatorSample();
txtAddResult->Text = calcobj->Add(10,20).ToString();

 

So from the C++ application, we are calling the C++ Windows Runtime Component DLL. This is all native code. C++ calling C++ and everything is ref counted.

Compiler options: /ZW enable WinRT language extensions /AI<dir> add to assembly search path <dir> is the folder where the compiler searches the winmd files /FU<file> forced using assembly/module force the inclusion of the specified winmd file /D "WINAPI_FAMILY=2" set this define to compile against the ModernSDK subset of Win32

Linker options: /APPCONTAINER[:NO] marks the executable as runnable in the appcontainer (only) /WINMD[:{NO|ONLY}] emits a winmd; if “ONLY” is specified, does not emit the executable, but just the winmd /WINMDFILE:filename name of the winmd file to emit /WINMDDELAYSIGN[:NO] /WINMDKEYCONTAINER:name /WINMDKEYFILE:filename used to sign the winmd file

However, in a Metro style app or Windows Runtime component, all the C++ code is native. The /ZW compiler option causes the Component Extensions to be compiled for Windows Runtime. The /cli compiler option causes them to be compiled for C++/CLI. Currently, C++/CLI is not supported for Metro style apps

Code snippet of the complete MainPage class.

Code Snippet
//
// MainPage.xaml.cpp
// Implementation of the MainPage.xaml class.
//

#include "pch.h"
#include "MainPage.xaml.h"

using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Data;
using namespace CPPApplication1;

using namespace CppWinRTComponentDll;

MainPage::MainPage()
{
    InitializeComponent();

      CalculatorSample^ calcobj = ref newCalculatorSample();
    txtAddResult->Text = calcobj->Add(10,20).ToString();

    int result;
    HRESULT hr = calcobj->__cli_Add(20,30,&result);
    txtAddResult->Text = result.ToString();
}

MainPage::~MainPage()
{
}

All Windows Runtime types derive from the universal base class Platform::Object. There is therefore an implicit conversion from any Windows Runtime object to Platform::Object.

Code snippet of the XAML page.

Code Snippet
<UserControl x:Class="CPPApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008&quot;
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
    mc:Ignorable="d"
    d:DesignHeight="768" d:DesignWidth="1366">
    
    <Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
        <TextBox x:Name="txtAddResult" HorizontalAlignment="Left" Text="TextBox" VerticalAlignment="Top" Margin="343,90,0,0" Width="212"/>
        <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Calling C++ component Add method from C++ XAML Application" VerticalAlignment="Top" Margin="81,90,0,0" Height="45" Width="258" FontSize="14" FontWeight="Bold"/>

    </Grid>
    
</UserControl>

 

Build the solution and deploy the application. The application output is as follows.image

Configuration settings:

Enable Windows Runtime Extensions enables the runtime extensions throughout the type system which includes the ability to do Boxing. I.e. Boxing to WinRT type system. Every fundamental types and WinRT types are derived from Platform.Object.image

heap-allocated objects with heap semantics:

Calculator^ calc = ref new Calculator(); // Calculator is a ref class from a custom WinRT component

txtResult->Text = calc->Add(10, 20).ToString();

The ^syntax will fire a destructor when the refcount on the object drops to 0, or if you explicitly call delete. (So if you handed the object out it’s not necessarily at the end of your scope)

heap-allocated objects with Stack semantics:

Calculator calc;

txtResult->Text = calc.Add(10, 20).ToString();

Both of those create heap-allocated objects behind the scenes, but the difference is whether you logically have heap semantics vs. stack semantics.

The stack syntax will fire a destructor when the object goes out of scope (or on an exception etc.). This is important when you e.g. handed of the object to another thread or async callback, so there may still be a refcount on it, but you need to get rid of it right away. (E.g. a file handle that needs to be closed otherwise the file is locked). The main advantage is exception-safe deterministic destruction.

PS: Because of the nature of refcounting, it’s a little bit less important with WinRT to have deterministic destruction than with e.g. the /clr and a garbage collected heap (where the point of destruction is virtually random). However, you will find that with async patterns it is common to get into a situation where you’re transfer the ownership of an object from one thread to another (e.g. via a lambda). There is then a race condition between the two threads for releasing the object. This is generally still ok, but if the object represents a file or other exclusive resource it might be critical to perform the destruction at a specific time, rather than relying on the timing between the two threads.

value classes & Ref classes

Int32 x(15); // Compiles and x is initialized and works as expected.

String str("test1"); // Doesn’t compile and compiler complains C3149: ‘Platform::String’ : cannot use this type here without a top-level ‘^’

Int32 is a value class, and String or the custom winRT component are all ref class. They are different.

Differences between C++/CLI and WinRT C++

In terms of the differences like flags are as follows.

Basic types:
/clr: From mscorlib.dll (System::* types)
/ZW: From vccorlib.dll (Platform::* types)

Lifetime management:
/clr: Garbage collected
/ZW: Refcounted

Cycles Broken:
/clr: By garbage collector
/ZW: Broken by user (weak references or explicit delete)

Code generation:
/clr: MSIL + native code. Can create a cross-platform binary if MSIL-only.
/ZW: Native code only. Binaries target a specific platform.

Object Creation:
/clr: gcnew
/ZW: ref new

interior_ptr:
/clr: Supported
/ZW: Not supported

pin_ptr:
/clr: Supported
/ZW: Not supported

V% (% when it refers to a byref (kind’a like an "interior_ref") ):
/clr: Supported
/ZW: Not supported

R% (% when it refers to an implicitly dereferenced ref type):
/clr: Supported
/ZW: Supported

Ref to ^:
/clr: R^%
/ZW: R^&

Boxing:
/clr: syntax V^
/ZW: IReference<V>^

Dereferenced box type:
/clr: V%
/ZW: const V

Generics:
/clr: Generics classes, interfaces & delegates allowed.
/ZW: Generic interfaces & delegates only.

Static constructors:
/clr: Supported
/ZW: Not supported

Address of member of ref class:
/clr: Returns an interior_ptr
/ZW: Returns a type*

friends:
/clr: Not supported
/ZW: Supported

C++ class embedded in ref class:
/clr: Not supported
/ZW: Supported

ref class embedded in C++ class:
/clr: Not supported
/ZW: Supported (R-on-stack)

^ embedded in C++ class:
/clr: Not supported (Needs GCHandle)
/ZW: Supported

^ embedded in value class with value class on native heap:
/clr: Not supported
/ZW: Supported (for String^)

Global ^:
/clr: Not supported
/ZW: Supported

Global R-on-stack:
/clr: Not supported
/ZW: Supported

Finalizer:
/clr: Supported
/ZW: Not supported

Destructor:
/ZW: Runs on IDisposable::Dispose (delete / stack unwind) only
/clr: Runs on IDisposable::Dispose (delete / stack unwind) -or- last release (never both)

T::typeid:
/clr: Supported
/ZW: Not supported

R-on-stack (ref class on stack) syntax is supported on C++/cli and C++/CX, but you’ll notice that unfortunately the /CX implementation in the developer preview release has a code generation bug that will make it impractical to test this right now. (R^ and R% should be fine though). The most important reason for R-on-stack is exception-safe destruction (Like ‘using’ gives you in C#) – other than that it is purely compiler syntactic sugar.

Download the source code here.

"If one advances confidently in the direction of his dreams, and endeavors to live the life which he has imagined, he will meet with success unexpected in common hours." — Henry David Thoreau

Advertisements

Under the hood Part 3 : Internals of how the application is installed and run – WinRT, Windows 8, C++, C#.NET, Metro

Under the hood Part 3 : WinRT, Windows 8, C++, C#.NET, Metro, WinRT Component DLL

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

See the post on First Look at What’s New in Windows 8 here.

Going further, I created a C++ Metro application and accessed the C++ WinRT Component DLL from this application. The interesting part here is that C++ applications is XAML based. No more .RC and resource.h files in C++ (for metro). We will explore the C++ application in another post. In this post, let us walk through the packaging and installation process that happens in the background during building and deploying of the applications.

Basically, there are two registrations for our application.

  1. Extension registration
  2. Class registration.

The figure 1 (from Build con) shows the relation between the two.

image

Fig : Slide related to Extension Catalog and Class Catalog shown @ Build con.

Go to  Visual Studio 2011 –> Solution Explorer –> CSharpApplication project, here you can find a file named Package.appxmanifest as shown in figure 1 . This file has most of the information needed to deploy the application. This is the information that windows uses to identify the application. Package Name is the name used in most of the identification process.

image

Fig 1: Package.appxmanifest file in Visual Studio 2011 Solution Explorer.

Code snippet of Package.appxmanifest file.

Code Snippet
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest">
  <Identity Name="CSharpApplicationCallingCPPComponent" Publisher="CN=Kishore" Version="1.0.0.0" />
  <Properties>
    <DisplayName>CSharpApplication</DisplayName>
    <PublisherDisplayName>Kishore</PublisherDisplayName>
    <Logo>Images\StoreLogo.png</Logo>
    <Description>CSharpApplication</Description>
  </Properties>
  <Prerequisites>
    <OSMinVersion>6.2</OSMinVersion>
    <OSMaxVersionTested>6.2</OSMaxVersionTested>
  </Prerequisites>
  <Resources>
    <Resource Language="en-us" />
  </Resources>
  <Applications>
    <Application Id="App" Executable="csharpapplication.exe" EntryPoint="CSharpApplication.App">
      <VisualElements DisplayName="CSharpApplication" Logo="Images\Logo.png" SmallLogo="Images\SmallLogo.png" Description="CSharpApplication" ForegroundText="light" BackgroundColor="#222222" InitialRotationPreference="portrait">
        <SplashScreen Image="Images\SplashScreen.png" />
      </VisualElements>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="internetClient" />
  </Capabilities>
</Package>

 

The applications implement contract like search contract, share, play queue contract. Those contract registrations are the extensions registration for the operating system.  Tile that you see in the Windows start page is just another contract activation. I,e windows.launch contract. So if you go to HKEY_CURRENT_USER\Software\Classes\Extensions\ContractId\Windows.Launch, you can find your application package id that we saw in figure 1. Everything under Windows.Launch is organized by package ID as shown in the figure 2 below.

image

Fig 2: Registry settings showing the registered Extensions.

From figure 2, we can see our package HKEY_CURRENT_USER\Software\Classes\Extensions\ContractId\Windows.Launch\ PackageId\csharpapplicationcallingcppcomponent_1.0.0.0_x86_neutral_kb63pw67p0swp. This package has a  ActivatableClassId key . Under that we see App as shown in figure 3. This is the class registration for this extension. image

Fig 3: App registry settings for your application.

Let us see how the class registration for the applications looks like. First, We have extensions which say I implement this contract for ex I implement this launch and then we have classes. The extensions point to the classes. classes are actually the implementation. All applications are just Windows Runtime objects to the Windows OS, this is where all it starts. This is the windows runtime class that the OS knows about for your application.

The interesting part is in the class registration for the application. If we go up in the registry editor, we should see a registry key called ActivatableClasses at  HKEY_CURRENT_USER\Software\Classes\ActivatableClasses, this is where the class registrations are for all of our applications are. Here again we find package. All extensions, all classes are organized based on the package. That means our application or package has unique set of classes and they don’t share extension points or classes with other applications. They are all unique for our app. If we expand it, we see ActivatableClassId, the same name we saw in extensions. Here we should find the App classId. This is the WinRT class registration. This is the same for all types of applications, libraries etc.

Here we can see some registration attributes . The ActivationType is an important one. Windows Runtime supports two activation styles.

  1. InProcess activation
  2. Out of process activation

So Windows Runtime supports InProcess activation where we provide the DLL and windows load that into the process and it also supports out of process activation, where we will provide an exe and windows will launch the exe as your class implementation.

Registry key values:

ActivationType = 1 indicates its out of process activation class. out of process classes have a server. we have to know where the executable is, so there is a server registion here.

ActivationType = 0 indicates its in process activation

Out of process has a server and so there is a server registration here. Server with value App.AppXpdnr4x0evrk1yjmz5xfw2ksncfcjc5er.mca as shown in figure 4.image

Fig 4: Server registry values for your application.

In figure 3, below the ActivationType , we can find Server attribute and it contains the App.AppXpdnr4x0evrk1yjmz5xfw2ksncfcjc5er.mca we got from the above Server value.  The server registration, tells the windows runtime enough information about what code we actually need to get from the disk and start running to get the app up and running. The ExePath attribute gives the path to the exe.

Windows runtime supports two apartment models. MTA & STA. If you have worked in VC++ COM, COM+ earlier, then you might have programmed the components for Multi Threaded Apartment model & Single Threaded Apartment model.

Let us see some more ActivatableClassId entries for Windows Runtime present at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsRuntime as shown in figure 5.

image

Fig 5: Windows Runtime classes information.

Finally, the slide that caught my attention was the deployment pipeline process shown in Fig 6 that shows the overall view of the above mentioned information.

image

Figure 6 showing the role of the deployment engine @ Build Con.

"Only those who risk going too far can possibly find out how far they can go." – T.S. Eliot

Under the hood Part 2 : C++ WinRT Component DLL & C#.NET Metro application

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

The important point here is that the  C# application is accessing the C++ WinRT component through RCW mechanism. The same application can be developed with JavaScript also that can use the C++ DLL that we developed. Wow, cool right? 

To understand the simplicity of the new programming model, we created a C++ WinRT Component DLL in part 1, which is all the code you need to author a Windows Runtime component.

Please download the code and extract the files.

Code Snippet
#pragma once

using namespace Windows::Foundation;

namespace CppWinRTComponentDll
{

    public ref class CalculatorSample sealed
    {
    public:
        int Add(int x, int y);
        int Sub(int x, int y);
        int Mul(int x, int y);

    };
}

In this example, you will immediately notice some keywords that are not part of standard C++. Visual C++ defines a few Component Extensions that, in a Metro style app, are essentially syntactic sugar over the underlying COM calls involved in creating and consuming Windows Runtime types. You typically use these extensions in the public interfaces where your code is passing Windows Runtime types back and forth across the ABI to JavaScript, C#, or Visual Basic (or even another C++ module). Visual C++ provides a variety of implicit conversions between Windows Runtime types and standard C++ types. The typical pattern is to use standard C++ types and libraries internally as usual, and convert to Windows Runtime types in the public interfaces.

So how did the C++ component get pulled into .net application? When we build the CppWinRTComponentDll project, we see the following in the output window. Notice the highlighted CppWinRTComponentDll.winmd text. This means the compiler is generating a file of type windmd.

1>—— Build started: Project: CppWinRTComponentDll, Configuration: Debug Win32 ——
1>  CppWinRTComponentDll.vcxproj -> C:\Users\Kishore\Documents\Visual Studio 11\Projects\CppWinRTComponentDll With CSharp App\Debug\CppWinRTComponentDll\CppWinRTComponentDll.winmd
1>  LINK : C:\Users\kishore\Documents\Visual Studio 11\Projects\CppWinRTComponentDll With CSharp App\Debug\CppWinRTComponentDll\CppWinRTComponentDll.dll not found or not built by the last incremental link; performing full link
1>     Creating library C:\Users\kishore\Documents\Visual Studio 11\Projects\CppWinRTComponentDll With CSharp App\Debug\CppWinRTComponentDll\CppWinRTComponentDll.lib and object C:\Users\kishore\Documents\Visual Studio 11\Projects\CppWinRTComponentDll With CSharp App\Debug\CppWinRTComponentDll\CppWinRTComponentDll.exp
1>  CppWinRTComponentDll.vcxproj -> C:\Users\kishore\Documents\Visual Studio 11\Projects\CppWinRTComponentDll With CSharp App\Debug\CppWinRTComponentDll\CppWinRTComponentDll.dll
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

You can notice the CppWinRTComponentDll.winmd file in the debug folder (CppWinRTComponentDll With CSharp App\Debug\CppWinRTComponentDll) as shown in Figure 1.

image

Fig 1: .WinMD File present in CppWinRTComponentDll folder inside the Debug folder.

This WinMD file is essentially a CLI meta data file and can be opened in ILDASM as shown in figure 2. CppWinRTComponentDll is exposed using API metadata present in CppWinRTComponentDll.winmd file. The format of WinMD file is Ecma-335 and this is the same format used by the .NET framework. It just used the ECMA-335 file format standard & the framework and nothing more than that. The underlying binary contract makes it easy for us to access the CppWinRTComponentDll APIs directly in the development language of our choice. The shape and structure of the CppWinRTComponentDll APIs can be understood by both static languages such as C# and dynamic languages such as JavaScript. IntelliSense is available in JavaScript, C#, Visual Basic, and C++ as you can see in the C# application (CSharpApplication project).

image

Fig 2: Opening CppWinRTComponentDll.winmd file using ILDASM.

This CppWinRTComponentDll.winmd file is created by c++ compiler and this can be seen as a cross language header file because c# cannot understand c++ header and neither JavaScript. so we need to have the file format that all languages understand and that is the .winmd file format. Although we authored a CalculatorSample class, what the compiler did for us is that, it also created a interface __ICalculatorSamplePublicNonVirtuals as shown in figure 3, which is the com interface used to talk to the calculator class. This adds it to the public surface.image

Fig 3: Interface ICalculatorSamplePublicNonVirtuals created by the compiler

Manifest of the CppWinRTComponentDll.winmd file can be seen in figure 4.image

Fig 4: Manifest of the CppWinRTComponentDll.WinMD File. Note the first entry, mscorlib.

we can see the Add method signature stored in the WinmD file in figure 5.image

Figure 5: Add method signature present in CppWinRTComponentDll.winmd file.

Code Snippet
.method public newslot abstract virtual instance int32
Add(int32 x,
int32 y) cil managed
{
} // end of method __ICalculatorSamplePublicNonVirtuals::Add

 

In the ILDASM, click on the .class interface private abstract auto ansi windowsruntime. The GUID attribute. this is like specifying the GUID of the interface as shown in figure 6. This is the GUID for the component.

image

Fig 6: GUID of the interface.

The WinMD file does not contain any IL. This is just like a header information so that other languages can understand. This makes the Projections to happen. The calculator is a ref class itself. The CppWinRTComponentDll.CalculatorSample class implements from .__ICalculatorSamplePublicNonVirtuals and it implements Add,Sub and Mul methods as shown in the following figure 7.

image

Fig 7: CppWinRTComponentDll.CalculatorSample class implements from __ICalculatorSamplePublicNonVirtuals Interface.

Note that every object inside WinRT is ref counted.

Details about the C++ WinRT Component DLL created in part 1 (MSDN): C++/CX (Component Extensions)

Visual C++ in Visual Studio 11 for has a new programming model for creating Metro style apps and components. One of the primary features of the new model is the abstract binary interface, or ABI, which defines an interface for inter-language communication. In Windows Developer Preview, native C++ can communicate across the ABI with JavaScript and with the managed .NET languages C# and Visual Basic. This means you can create high-performance components in native C++ and consume them directly from one of these other languages. It also means you can consume Windows Runtime types directly in native C++, without having to interop with a managed layer as when you consume .NET Framework types.

At a low level, the new programming model is based on an updated version of COM, but the way that you program against this new model is more simple and natural than old-style COM programming. When you create a consumable Windows Runtime components or Metro style apps in native C++ you work primarily with classes and members just as in standard C++. JavaScript or .NET code instantiates your object by using new (or New) and the object is destroyed according to the mechanisms of the client language. When native C++ consumes a Windows Runtime object, destruction is handled by means of reference counting.

Download the code and extract the files.

"Cultivate optimism by committing yourself to a cause, a plan or a value system. You’ll feel that you are growing in a meaningful direction which will help you rise above day-to-day setbacks." — Dr. Robert Conroy

Note: The code is developed using Windows 8 & Visual Studio 2011 developer preview, which might change when the actual versions are released.

Under the hood Part 1 : C++ WinRT Component DLL & C#.NET application

In order to better understand  the concepts of WinRT, C#.NET, C++, etc. in the new Windows 8 environment, let us write some code. Then we will explore what happens under the hood when we compile code and run the applications. This will be explained in many parts as we progress.

First let us understand what WinRT is and what makes a WinRT DLL or WinRT component. There is no better place to start other than creating a WinRT component ourselves and examining it. So lets get started.

Creating a WinRT Component DLL:

Step 1:

Open VS 2011 Developer preview. Click on New Project. In the New Project Dialog, from Installed section, select Visual C++ in the left hand side navigation tree. Select WinRT Component DLL from the project templates and name the component as CppWinRTComponentDll. Click OK as shown in Fig 1.image

Fig 1: Selecting Visual C++ template for WinRT Component DLL project.

Note: As a test, I tried to create a component with name C++WinRTComponentDll and VS 2011 C++ compiler shows many errors in the class files created by the wizard. So try to avoid ++ in the names.

Glimpse of new Visual Studio 2011 Developer preview edition:

In the new Solution Explorer, hover over the files and for each file you will find that there is an icon on the far right edge; View additional information (Ctrl+Right) icon.
When you select an item in Solution Explorer, whether it be a file or class or method, etc., the icon appears on the right edge of the selection. The tooltip says "View additional information (Ctrl+Right)". Clicking on the icon will invoke the "View Additional Information" menu which, for class files and members will provide relationships that you can view in Solution Explorer. Examples include "Base/Derived Types", "Find References", "Call Hierarchy", Calls, Is Called By, Is Used By etc. as shown in the following Fig 2.

image

Fig 2: Figure showing the View additional information (Ctrl+Right)icon.

Step 2:

Goto WinRTComponent.cpp file and delete the WinRTComponent definitions. Goto WinRTComponent.h and delete some of the default code. The code of the .h & .cpp files will look as shown in the following code snippets.

Code Snippet
// WinRTComponent.cpp

#include "pch.h"
#include "WinRTComponent.h"

using namespace CppWinRTComponentDll;

 

Code Snippet
#pragma once

using namespace Windows::Foundation;

namespace CppWinRTComponentDll
{

}
// WinRTComponent.h

 

We need the namespace CppWinRTComponentDll. Everything in WinRT has to be part of a namespace because the resolution of the DLL is based on a namespace that the class is in and that’s how other programming languages will find the class based on namespaces and DLL name.

Let us declare a sealed class called CalculatorSample in  WinRTComponent.h as shown in the following code snippet. The class is sealed as we don’t want others to derive from it. Sealed means no Inheritance.

Code Snippet
public ref class CalculatorSample sealed
{
public:
    int Add(int x, int y);
    int Sub(int x, int y);
    int Mul(int x, int y);

};

 

Why use Ref in front of the class:

Previously some of us worked on COM and created COM components. For that we have to create an interface, we have to specify that we are deriving from IUnknown (or derived) , put this in an MIDL file, use the MIDL Compiler to generate files, then go to C++ component, implement this interface, then create a class factory for it to create an instance and register this component. and the list goes on.. Well, all of this is done by using the keyword Ref in front of the class. So here we are creating WinRT Component that is accessible by dynamic languages as well. Here we are saying that CalculatorSample is a ref class, a class for WinRT. In other words, a ref class is a user-defined class that can be passed across the ABI boundary.

Step 3:

In the WinRTComponent.cpp, define the CalculatorSample  class member functions as shown in the following code snippet.

Code Snippet
int CalculatorSample::Add(int x, int y)
{
    return x+y;
}

int CalculatorSample::Sub(int x, int y)
{
    return x-y;
}

int CalculatorSample::Mul(int x, int y)
{
    return x*y;
}

 

So by now we have declared and defined the methods for the WinRT class called CalculatorSample. This class can be used by C++,C#.NET, VB.NET, JavaScript apps.

To sum it up, the WinRTComponent.h file should have the following code.

Code Snippet
#pragma once

using namespace Windows::Foundation;

namespace CppWinRTComponentDll
{

    public ref class CalculatorSample sealed
    {
    public:
        int Add(int x, int y);
        int Sub(int x, int y);
        int Mul(int x, int y);

    };
}
// WinRTComponent.h

WinRTComponent.cpp file contains the following code.

Code Snippet
// WinRTComponent.cpp

#include "pch.h"
#include "WinRTComponent.h"

using namespace CppWinRTComponentDll;

int CalculatorSample::Add(int x, int y)
{
    return x+y;
}

int CalculatorSample::Sub(int x, int y)
{
    return x-y;
}

int CalculatorSample::Mul(int x, int y)
{
    return x*y;
}

Step 4:

Do not Compile the project. pch.h and pch.cpp are like stdafx.h and stdafx.cpp in VC++ MFC.

This completes the creation of a WinRT Component DLL.

Let us create a new application and try to access the WinRT Component DLL that we just created. For that, I will use C#.NET, so that we can know how to call a WinRT Component DLL from C#.NET application.

Right Click on the Solution CppWinRTComponentDll, Click on Add and select the New Project menu item as shown in Fig 3.

image

Fig 3: Adding  a New project to the CppWinRTComponentDll solution.

Select Visual C# –> Application template and name the project as CSharpApplication as shown in Fig 4.

 

image

Fig 4: Creating a Visual C# Application project in VS 2011 Developer Preview.

Step 5:

Add a reference to the WinRT C++ calculator class we created to the C#.NET application as shown in the following figures.

image

Fig 5: Right click on References folder and Select Add Reference…

image

Fig 6: Select Solution tab in the left hand side navigation panel and select Projects. You should be able to see the CppWinRTComponentDll that we just created. Select CppWinRTComponentDll and click on Add button. click Close and go to step 6 if you have not encountered any error.

If you have compiled the C++ DLL by mistake before adding the reference, you will get a Failed to add reference dialog as shown below.

image

Fig 7: Failed to add reference dialog.

To fix the Failed to add reference dialog issue, clean up the CppWinRTComponentDll project as shown in the following figure 8.

image

Fig 8: Right Click on CppWinRTComponentDll  project, select Project Only, then select Clean Only CppWinRTComponentDll

Now repeat step 5 again to add a reference to the CppWinRTComponentDll and you should see the green circle icon with tick inside it as shown in Fig 9. This means the component is successfully referenced in the project.

image

Fig 9: Component successfully referenced in the project.

Click close.

Step 6:

Open MainPage.xaml file and add three text boxes to show the result of addition, subtraction and multiplication values returned from the C++ WinRT Component DLL.

Add the following XAML code.

Code Snippet
       <TextBox x:Name="txtAdd" HorizontalAlignment="Left" Text="TextBox" VerticalAlignment="Top" Margin="582,97,0,0" Height="36" Width="246" FontSize="20"/>
        <TextBox HorizontalAlignment="Left" Text="Calling Add method of C++ WinRT Component DLL" VerticalAlignment="Top" Margin="114,97,0,0"/>
       
        <TextBox x:Name="txtSub" HorizontalAlignment="Left" Text="TextBox" VerticalAlignment="Top" Margin="582,180,0,0" Height="36" Width="246" FontSize="20"/>
        <TextBox HorizontalAlignment="Left" Text="Calling Sub method of C++ WinRT Component DLL" VerticalAlignment="Top" Margin="114,180,0,0"/>
        
        <TextBox x:Name="txtMul" HorizontalAlignment="Left" Text="TextBox" VerticalAlignment="Top" Margin="582,260,0,0" Height="36" Width="246" FontSize="20">
        </TextBox>
        <TextBox HorizontalAlignment="Left" Text="Calling Mul method of C++ WinRT Component DLL" VerticalAlignment="Top" Margin="114,260,0,0"/>

 

Step 7:

Open MainPage.xaml.cs file and add the following code.

To use the C++ WinRT Component DLL that we just created we need to specify it in the C#.NET project by mentioning using CppWinRTComponentDll;

In the MainPage(), add the following code.

Code Snippet
CalculatorSample calcobj = new CalculatorSample();
            txtAdd.Text = calcobj.Add(10,20).ToString();
            txtMul.Text = calcobj.Mul(10, 20).ToString();
            txtSub.Text = calcobj.Sub(20, 10).ToString();

 

The complete MainPage.xaml file XAML code is

Code Snippet
<UserControl x:Class="CSharpApplication.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008&quot;
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
    mc:Ignorable="d"
    d:DesignHeight="768" d:DesignWidth="1366">
    
    <Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
        <TextBox x:Name="txtAdd" HorizontalAlignment="Left" Text="TextBox" VerticalAlignment="Top" Margin="582,97,0,0" Height="36" Width="246" FontSize="20"/>
        <TextBox HorizontalAlignment="Left" Text="Calling Add method of C++ WinRT Component DLL" VerticalAlignment="Top" Margin="114,97,0,0"/>
       
        <TextBox x:Name="txtSub" HorizontalAlignment="Left" Text="TextBox" VerticalAlignment="Top" Margin="582,180,0,0" Height="36" Width="246" FontSize="20"/>
        <TextBox HorizontalAlignment="Left" Text="Calling Sub method of C++ WinRT Component DLL" VerticalAlignment="Top" Margin="114,180,0,0"/>
        
        <TextBox x:Name="txtMul" HorizontalAlignment="Left" Text="TextBox" VerticalAlignment="Top" Margin="582,260,0,0" Height="36" Width="246" FontSize="20">
        </TextBox>
        <TextBox HorizontalAlignment="Left" Text="Calling Mul method of C++ WinRT Component DLL" VerticalAlignment="Top" Margin="114,260,0,0"/>

    </Grid>
    
</UserControl>

 

The complete MainPage.xaml.cs file C#.NET code is

Code Snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using CppWinRTComponentDll;

namespace CSharpApplication
{
    partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();

            CalculatorSample calcobj = new CalculatorSample();
            txtAdd.Text = calcobj.Add(10,20).ToString();
            txtMul.Text = calcobj.Mul(10, 20).ToString();
            txtSub.Text = calcobj.Sub(20, 10).ToString();
            

        }
    }
}

 

Step 8: Build the solution.

The following information will be displayed in the output window. See the highlighted information. The Visual Studio 2011 is compiling the projects as well as deploying the projects. Later, we will see in detail what happens during deployment and how the applications is run by Windows 8.

1>—— Build started: Project: CSharpApplication, Configuration: Debug Any CPU ——
1>  CSharpApplication -> C:\Users\Prathima\Documents\Visual Studio 11\Projects\CppWinRTComponentDll With CSharp App\CSharpApplication\bin\Debug\csharpapplication.exe
1>  CSharpApplication -> C:\Users\Prathima\Documents\Visual Studio 11\Projects\CppWinRTComponentDll With CSharp App\CSharpApplication\bin\Debug\CSharpApplication.build.appxrecipe
2>—— Deploy started: Project: CSharpApplication, Configuration: Debug Any CPU ——
========== Build: 1 succeeded, 0 failed, 1 up-to-date, 0 skipped ==========
========== Deploy: 1 succeeded, 0 failed, 0 skipped ==========

 

The output of the programs is the application

image

Download the source code from here.

"The future belongs to those who see possibilities before they become obvious."  – John Scully

Note: This code is developed using Windows 8 & Visual Studio 2011 developer preview, which might change when the actual versions are released.