Quantcast
Channel: Adventures on the edge - WinRt
Viewing all articles
Browse latest Browse all 5

C++/CX WinRt and Windows Phone code sharing

$
0
0

Before EHR certification requirements made the development of EHR applications cost prohibitive, I wrote an EHR program for a medical group. It started as a web application which soon opened up a requirement for a desktop application (the Internet was not available at all locations visited by the doctors).  Within a short period of time the requirement for mobile application soon arose.  Within one year I was maintaining three applications.

The doctors wanted each application to perform identically and had a hard time understanding why I had to bill them for each application when only one set of changes were required; the problem was that each application had a separate code base.  This prompted my journey to create a single code base for all platforms which I had just completed with my open source password manager application (link below), but then C++/CX was released and it was time to start over….

SOURCE: http://PasswordMgr.CodePlex.com  Change set: 102777

With the preliminary infrastructure started, and unit test in place, it was time to “share” the completed source code with a Windows Phone project.  I was surprised that the Windows Phone project was a C# project and that I was expected to add references to a C++ Windows Runtime Component (WRC) project (versus being 100% native).   We know the saying that “beggars can’t be choosey”, so I’ll take what is available but it almost makes me feel that C++/CX is not the first class citizen all the Build conference hype made it out to be….  It is still early in the game (I hope)… <ends frustrated rant after falling for the hype and abandoning C# for Windows 8 development>  

I created my C++ Phone WRC project GwnLibraryPhone and proceeded to COPY/PASTE the files from the GwnLibrary (a Windows Store App C++ WRC project).   When I got done I reviewed the file locations and had expected results since I copy/pasted from Visual Studio filtersonly the GwnLibrary project folder have files, the GwnLibraryPhone folder has no files and the project points to the GwnLibrary files.

NoFilesWhenShared
Figure 1 sharing source code (single code base)

Since templates are not supported by the C++/CX Application Binary Interface (ABI), my templated methods such as Resolve and Register had to be in an external project; I share C++/ C X templates with other projects.  The GwnLibraryInclude WRC project contains all of the C++/CX shared components which are unusable in my C# Windows Phone application.  I had to create a GwnLibraryPhoneInclude project which has a C# extension class with generic methods to match its C++/CX counterpart.   This extension class allows me to use the same source for both of the C# and C++/CX applications (ref figures 2 and 3 below).

Code
Figure 2 Utilizing C++/CX classes in a Windows Phone application

RegisterResolve
Figure 3 C++ templates and their C# counterparts

Note: The Visual Studio compiler will complain if the namespace does not match the project name so I have to have the following preprocessor directives on all of my classes:

#if PHONE
namespace GwnLibraryPhone {
#else
namespace GwnLibrary {
#endif


Preprocessor


Viewing all articles
Browse latest Browse all 5

Trending Articles