Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Node Native Addons (C++) in Electron

I'm completely new to Node JS and Electron. I'm trying to integrate C++ with HTML using Electron and Node JS. I have went through some examples given by: GIT

What i'm trying to do is calling the native function (hello()) from my web page's javascript which is loaded by electron. I have used node-gyp configure to generate my Visual Studio Solution File. (.sln). And later i compiled my code with Visual Studio 2013 Express which successfully generate my .node file in build\Release Folder.

This is my index.js file:

var addon = require('./build/Release/hello.node');
console.log(addon.hello());

when I simply run this with node index.js, it is giving me the desired output:

world

But the problem comes with when i Use Electron. I'm using electron binary (32 bit) to run my webpage.

The following is my main.js file:

var app = require('app');  // Module to control application life.
var BrowserWindow = require('browser-window');  // Module to create native browser window.


require('crash-reporter').start();

var mainWindow = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  if (process.platform != 'darwin') {
    app.quit();
  }
});

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
    mainWindow = new BrowserWindow({width: 1366, height: 768});
    mainWindow.loadUrl("file://" + __dirname + "/HtmlFile/index.html");
    mainWindow.on('closed', function() {
    mainWindow = null;
  });
});

Now this is my javascript where i'm calling the native addon:

//************* My Functional logic **************
 //************************************************

var addon = require('../build/Release/hello');
alert(addon.hello());

When i run this or load this page, I'm getting the following error:

Uncaught Error: %1 is not a valid Win32 application. ATOM_SHELL_ASAR.js:137
C:\Users\Administrator\Desktop\MyAPP\build\Release\hello.node

Following is my package.json:

{
  "name": "MyAPP",
  "version": "1.0.0",
  "description": "Desc",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nan": "^2.0.9"
  },
  "gypfile": true
}

This is my binding.gyp:

{
  "targets": [
    {
      "target_name": "hello",
      "sources": [ "hello.cc" ],
      "include_dirs": [
        "<!(node -e \"require('nan')\")"
      ]
    }
  ]
}
like image 583
user1636077 Avatar asked Oct 07 '15 08:10

user1636077


People also ask

What is the power of native node addons?

If you want to go deeper, consider the power of native Node Addons. They are dynamically-linked shared objects, written in C or C++, and can be used just as if they were an ordinary Node.js module.

How is node used as a library in electron?

Most people use Node for server-side applications, but because of Node's rich API set and thriving community, it is also a great fit for an embedded library. This post explains how Node is used as a library in Electron. Both Node and Electron use GYP as their build systems.

What are node addons and why should you care?

If you want to go deeper, consider the power of native Node Addons. They are dynamically-linked shared objects, written in C or C++, and can be used just as if they were an ordinary Node.js module. They are used primarily to provide an interface between JavaScript running in Node.js and C/C++ libraries.

What is node_shared in electron?

In Electron, Node is being linked as a shared library by setting the GYP variable node_shared to true, so Node's build type will be changed from executable to shared_library, and the source code containing the Node's main entry point will not be compiled.


2 Answers

It looks like you may not have the correct binary configured. Sorry not sure if this will work for the native module, but you could try rebuilding...

Note: Please insure you have the correct arguments for your node-gyp command (if that is how you will rebuild).

  • --target=<your electron version>
  • --target_platform=win32 (Not in the example link, but you seem to be using windows)
  • --arch=<your architecture> (x64 = 64bit, x86 = 32bit)
like image 131
Josh Avatar answered Oct 14 '22 03:10

Josh


As I mentioned in the comments I was running into this same issue. To resolve it you need to add a couple additional flags:

node-gyp rebuild --target=<your electron version> --arch=<insert your arch> --dist-url=https://atom.io/download/atom-shell

This will get the proper requirements from the atom.io site and build the add-on correctly. For more information you can checkout electron's specific docs on using native modules.

like image 44
devonbleibtrey Avatar answered Oct 14 '22 03:10

devonbleibtrey