Electron and C++ dll


#1

Hi. Is it possible to use dynamic libraries in electron applications? I have quite complex .dll written by C++ and C++ application using it, so I have an idea to provide electron-based GUI for it. Does it make any sense?
I’ve found out that it’s possible to create C++ addons for Node.js but it doesn’t seem to be an easy walk…


#2

Yes, it should be possible like any other Node application. It looks like you will need to write a wrapper layer in C++ using the Node header files (i.e. marshalling C++ data types into Javascript types) but it should be pretty straightforward.

Take a look at this. It is the Node.js documentation about C++ addons which contains a couple of useful examples.


#3

Plus, you can use https://github.com/nodejs/nan to ease your writing for the c++ wrapper.


#6

Thank you for answers, I tried this and got some problems.

I installed io.js v2.2.1.0 and created simple hello-world addon from the begining of this tutorial using pangyp. This addon works fine when used this way

//hello.js
var addon = require('./addon');

console.log(addon.hello()); // 'world'

and launched as

iojs hello.js

but it fails if launched as

node hello.js

with Error: Module did not self-register. (There’re two executables in io.js: iojs.exe and node.exe and they seem to be completely identical).

When I try to use this addon in Electron (with node.dll based on io.js v2.2.1.0) application:

var app = require('app'); 
var BrowserWindow = require('browser-window'); 

var addon = require('./addon');   //here

require('crash-reporter').start();
var mainWindow = null;
app.on('window-all-closed', function() {
  if (process.platform != 'darwin')
    app.quit();
});
app.on('ready', function() {
  mainWindow = new BrowserWindow({width: 800, height: 600});
  mainWindow.loadUrl('file://' + __dirname + '/index.html');
  mainWindow.on('closed', function() {
    mainWindow = null;
  });
});

it fails with exactly the same Error: Module did not self-register.

It looks like this:

Do you have any ideas what am I doing wrong?


#7

@sanyo this might be relevant to the conversation. There could be something else going on with how Electron works that causing the problem.


#8

How did you install this module? In case you used npm install ..., try removing the node_modules folder and running apm install.


#9

I didn’t install it by package managers, I just put it to node_modules folder inside Electron application, like this:

app/
   main.js
   index.html
   package.json
   node_modules/
        addon.node

This module was built on my computer by node-gyp (pangyp), should I use package managers somehow?


#10

I would advise to install it with a package manager yes. Native modules require to be built on your machine, as you say, but they have to be built with the header files of the iojs version it is to be used with. So if you want to use the module with electron, it has to be built with electron’s header files. Installing with apm does this for you.