简体   繁体   中英

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.

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.

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

如果您使用CMake.js构建,请确保${CMAKE_JS_SRC}包含在源列表中,否则请确保包含以下内容: https ://github.com/cmake-js/cmake-js/blob/master/ lib/cpp/win_delay_load_hook.cc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM