Building Multi Device applications with C++ and Visual Studio

Here are some trough’s about consuming a C++ library project from a Windows Phone Store app using a separate intermediary WinRT component. All this steps can equally be applied to a Windows Store app and therefore a solution that utilizes the Universal App template. 

Building a Windows Phone Store app, are occasions when you wish to consume logic within a library that you have previously written in C++. In order to consume this logic in a Windows Phone Store app you need to wrap that C++ logic using a WinRT component. 

WinRT component acts as a wrapper around the C++ code and is a means of projecting the correct types across the Abstract Binary Interface (such that it may be consumed in languages other than C++). There is documentation and sample code demonstrating how to achieve this, but they tend to show the C++ logic inside of the WinRT component. In the real-world, you will likely have the C++ code in a separate project and therefore you need to reference this from the WinRT component. This scenario is not well documented and so it is the objective of this article to walk through how you can go about setting this up.

Before getting started, the diagram in Figure 1 shows the projects that are, or will be involved.

Figure 1

First on the left you have the “DemoApp.WindowsPhone” project, which contains the application’s user interface. On the right you have the “DemoWin32Project”, which contains the C++ logic that you wish to call. In this example, the project is assumed to be a Win32 DLL project type. The grey box in the middle of the diagram represents the C++ WinRT component that wraps the calls to the Win32 DLL project. Assumes that you already have the components in blue already at your disposal, whilst the component in grey needs to be created.

First step is going to be to create the C++ WinRT component. You can do this by adding a new project to the solution and selecting the appropriate project type as demonstrated in following  Figure . For the purposes of this example, the project type will be “Windows Runtime Component (Windows Phone) – Visual C++” (highlighted in green) as the application calling it is a Windows Phone Store app. If this was a Windows Store app or a Universal app, then you would instead need to select the appropriate project type shown in amber.

Figure 2

When you have created the component, you can add a project reference to it from the application project (in this case “DemoApp.WindowsPhone”). This is shown in following Figure .

Figure 3

As the application project is going to be referencing C++ code, it is also important to add a reference to the Visual C++ runtime as shown in above Figure.

Figure 4

Add reference to Visual C++ runtime

The actual code logic that sits inside of these projects will not be discussed here as they are largely irrelevant.

At this point, you will want to add a reference from the Windows Runtime Component to the Win32 DLL project. It is this step that is not so well documented. Unfortunately there is a little bit of extra work to do here to get this working, although it is extremely straightforward once you know how.

First Figure implies that the C++ component is a Win32 Project type. This is intentional as it is likely the project type your existing code sits inside of – however, in order to be able to reference this logic, the code needs to sit inside of a subtly different project type. The next step is to create that other project type, which in this example will be “DLL (Windows Phone) – Visual C++” – highlighted in green in following Figure . If this was a Windows Store app or a Universal app, then you would instead need to select the appropriate project type shown in amber.

Figure 5

Creating the appropriate DLL project type

When  this is done, you need to either move across the C++ source files from your Win32 DLL project, or add a link to their existing location – either way, this entails just a few manual steps depending on how many source files you have. Finally, you will need to make sure you are referencing the appropriate header files in the DLL project from the Windows Runtime Component project so that you can code against the DLL. Following Figure  shows the configuration that needs to be applied in green.

Figure 6

Add ‘Include Directories’ to point to appropriate .h files

Now that these steps have been completed, you are in good shape for referencing the DLL project from the Windows Runtime Component project. Simply, go to the project properties of the Windows Runtime Component and navigate to “Common Properties” -> “References”. In this dialog you can point to the project created in above Figure  as demonstrated in following Figure .

It is worth pointing out that you can’t simply reference any and every Win32 DLL project as there is only a subset of Win32 APIs that are supported inside of a Windows Runtime project.

Figure 7

Add a reference to the newly created DLL project type

At this point, you have created the chain shown in first Figure – in other words, the application code on the left-hand side of the diagram may now call into the C++ logic held by the DLL project type by means of the Windows Runtime Component.

These steps may be followed for either a Universal App template or a Windows Store app as appropriate. Furthermore, Visual Studio allows you to right-click each the projects represented by the first two boxes in first Figure and select “Add Windows 8.1” to automatically create the Universal App project structure. This can also be performed on the project created in above Figure  which essentially gives the solution structure represented in following Figure .

Figure 8

Universal App calling a C++ library

Just only step that remain is to update the reference to point to the correct platform implementation as illustrated by the arrows in the diagram.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s