C++ and HTML as UI?

Ever Since I saw the post Raman Sharma: Building Metro Style Apps with C++ and JavaScript I wanted to create one such Hybrid app, after seeing the disaster the Win32 Resources look with it's pixelated resource file and Headache of a UI maintanence, I wondered if such an hybrid app could resolve some of those issues?

The blog actually mentioned many motivations as to why we want to have such an hybrid app
and the following stood out the strengths

JavaScript

  • Web
  • Dynamic and Responsive UI

C++

  • CPU/ GPU access and usage
  • Image/Audio/Video Processing
  • Compression/Encryption
  • Prasers
  • useful in Statics/Finance
He also mentioned about the advantages of such Hybrid Apps
but for me the following stood out
  • Code Reusability
  • Reuse of Complex and powerful C++ Native code and make a Dynamic HTML UI
Creating one such interoperability between the HTML and Javascript can be explained by following figure
we need to create a C++ WinRT Layer for an existing C++ code 
there are three methods in which we can do it

Update:-windows has changed from WinRT to UWP(Universal Windows Platform) as of 2015

To port a native DLL to the WinRT without creating a new project

  • If I have a native DLL that exports functions by using __declspec(dllexport), you can call those functions from a WinRT app by recompiling the DLL as a WinRt project.
  • After Opening the Project Properties for the DLL project, and Configuration needs to be set to All Configurations.
  • In the Project Properties, under C/C++General tab, Consume Windows Runtime Extension to Yes (/ZW). This enables component extensions (C++/CX).
  • In Solution Explorer, select the project node, open the shortcut menu and I selected Unload Project. Then, opened the shortcut menu on the unloaded project node, and complete the edit of the project file
  • <AppContainerApplication>true</AppContainerApplication>  
    <ApplicationType>Windows Store</ApplicationType>  
    <WindowsTargetPlatformVersion>8.1.0</WindowsTargetPlatformVersion>  
    <WindowsTargetPlatformMinVersion>8.0.0</WindowsTargetPlatformMinVersion>  
    <ApplicationTypeRevision>8.0</ApplicationTypeRevision>  
  • Close the .vcxproj file, open the shortcut menu again and choose Reload Project.

    Solution Explorer now identifies the project as a Universal Windows project.
  1. Make sure your precompiled header file name is correct. In the Precompiled Headers section, change Precompiled Header File from pch.h to stdafx.h. If you don't do this one might get nasty errors, The issue is that the WinRt projects use a different naming convention for the precompiled header file.
  2. Some functions are not available when you compile for the WinRt. You will see compiler errors about any problems. Address these until you have a clean build.
  3. To use the DLL in a WinRt app in the same solution, open the shortcut menu for the WinRt project node, and choose Add, Reference.
    Under Projects, Solution, select the checkbox next to the DLL project, and choose the OK button.
  4. Include the library's header file(s) in your WinRt app's pch.h file.
  5. Add code as usual in the WinRt project to invoke functions and create types from the DLL.

To use a native C++ static library in a WinRt project

  • In the project properties for the WinRt project, in the Linker section, add the path to the library in the Input property. For example, for a library in the project that places its output in SolutionFolder\Debug\MyNativeLibrary\MyNativeLibrary.lib, add the relative path Debug\MyNativeLibrary\MyNativeLibrary.lib.
  • Add a reference in the References node in Solution Explorer. That mechanism only works for Windows Runtime Components.

To port a C++ library to a Windows Runtime Component

  • Create a Windows Runtime Component project.->Close the project.
  • In the Windows File Explorer, locate the project. By default, Visual Studio uses the Visual Studio 2013\Projects folder in your Documents folder. Locate the C++ library project that contains the code you want to port. Copy the source files (header files, code files, and any other resources, including in subdirectories) from your C++ library project, and paste them into the project folder, making sure to preserve the same folder structure.
  • Reopen the Windows Runtime Component project, and open the shortcut menu for the project node in Solution Explorer, and choose Add, Existing Item.
  • Select all the files to add from your original project, and choose OK. Repeat if necessary for subfolders.
  • You might now have some duplicated code. If you have more than one precompiled header (say stdafx.h and pch.h), choose one to keep. Copy any required code, such as include statements, into the one you're keeping. Then, delete the other, and in the project properties, under Precompiled Headers, make sure that the name of the header file is correct.
  • Add public ref types to your project, or convert ordinary types to ref types, to expose entry points into the functionality you want to call from WinRT apps.
  • Test the component by adding a reference to it from a WinRt app project, and add some code to call the public APIs you created.

Comments

Popular posts from this blog

Traits Design - Inspirations and Design The Dead Universe

Introduction to DirectX and Creating the Main Game Loop using DirectX11