Developing with WinRT

At the core of the Electron application is Node.js that can interact with the native code with native add-ons. With the help of dynamically linked shared object written in C or C++ code, you can closely work with native operating system environments. In Windows 8 and above, the underlying operating system environment API is called Windows Runtime APIs or WinRT. There is an open source project available called NodeRT that uses WinRT's descriptive metadata files and automatically generates native add-ons for each WinRT namespace. So, in our Electron application, this allows us to replace the native C++ add-ons with JavaScript code.

For example, you can import a WinRT namespace using standard require function. The following code imports the namespace windows.system.userprofile into the Node.js context:

const { LockScreen } = require('windows.system.userprofile');

Download the NodeRT from https://github.com/NodeRT/NodeRT/releases and extract it into the directory. Open the solution inside visual studio and build it into the executable files. As this is a native node add-on module, you need to build it against your native operating system. Install the node-gyp using npm:

npm install -g node-gyp

Once you build the executable for NodeRT, you need to generate native node modules that you want to use inside your application using the executable files inside the NodeRT project.

Launch the UI tool by running NodeRTUI.exe:

Choose a .winmd file to generate the NodeRT module. You can choose among Windows 8, Windows 8.1, and Windows 10 SDK. Choose a namespace to generate the list of the namespace. Choose the output directory and build the module.

Once you generate the NodeRT module, you need to build the module against the Electron. That is similar to building native node add-ons against Electron binaries. Install node-gyp globally using npm:

npm install -g node-gyp

Open the command-line prompt and navigate to the generated NodeRT module folder. Type the following command to build the module against Electron binaries:

node-gyp rebuild --target=1.3.1 --arch=x64 --dist-url=https://atom.io/download/atom-shell

Ensure that you used the correct Electron module in the target option. Here, we used 1.3.1. Once you finish the build, copy the folder into your project's node_modules folder. Then, you can use it in the same way as other Node.js modules inside your Electron application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.29.139