Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron native add on : DLL initialization routine failed

I am trying to link my C++ library as a native add-on to my electron app. I am able to run node-gyp rebuild and generate a successful .node file.

But, when I try to call into it from main.js, I get an error which says: "A dynamic link library (DLL) initialization routine failed".

My binding.gyp file looks like this:

{
    'targets': [
    {
        # Usual target name/sources, etc.
        'target_name': 'myclass',
        'sources': [ 'myclass.cc', 'addon.cc' ],
        'libraries': ["../libs/api.lib",
                      "../libs/core.lib",
                      "../libs/camera.lib",
                      "../libs/algo.lib",
                      "../libs/ComCtl32.lib",
                      "../../deps/windows/opencv/lib/x64/*.lib",
                      "../../deps/windows/tbb/lib/x64/*.lib"],
        'include_dirs': ["<!(node -e \"require('nan')\")"],

        'configurations': {
            'Debug': {
                'msvs_settings': {
                            'VCCLCompilerTool': {
                                'RuntimeLibrary': '3' # /MDd
                    },
                },
            },
            'Release': {
                'msvs_settings': {
                            'VCCLCompilerTool': {
                                'RuntimeLibrary': '2' # /MD
                    },
                },
            },
        },
    },],
}

What could be wrong? Please let me know if any more information is needed.

like image 861
Giridhar Murali Avatar asked Oct 30 '22 12:10

Giridhar Murali


2 Answers

A couple of things could go wrong...

x64 vs. x86

You need to make sure you're getting your x86 v x64 binaries right. An x64 binary will only run on an x64 version of node for example. I see you are directly linking to some x64 libs, you probably need to conditionally link to the right libs based on the architecture you are targeting. And then make sure you are getting the right version of electron.

Dependent dlls

Make sure the dlls you are dependent on are in the right locations. Basically they should be in the same directory or next to the exe that is trying to load the dll.

It looks like you're using windows so try to use this tool to open your dll and see what its dependencies are depends.exe

The thing to note is that when you do the rebuild with node-gyp that dll will now only open in electron, you have to do some magic to get it to also load in node from the command line without further recompilation.

How are you actually trying to load the dll?

Versions

You have to have the exact right versions of node, electron and node-gyp. Triple check them all.

I can elaborate on any of these topics if you need more details.

like image 56
justin.m.chase Avatar answered Nov 03 '22 00:11

justin.m.chase


npm install -g prebuild

cd node_modules/ffi prebuild -t 1.3.1 -r electron

cd node_modules/ref prebuild -t 1.3.1 -r electron

'1.3.1' is version of the electron

like image 36
马立慧 Avatar answered Nov 02 '22 22:11

马立慧