Electron native addon failing on windows

2019-07-31 16:14发布

问题:

I have a native addon that uses openSSL library on a unpacked electron app. On a windows 10 it works and on a windows 7 it's not working , I am receiving this:

    Error: The specified module could not be found.
    \\?\C:\Program Files (x86)\AppX Player\resources\app\src\addon\foo.node
        at Error (native)
        at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:167:20)

        at Object.Module._extensions..node (module.js:568:18)
        at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:167:20)
        at Module.load (module.js:458:32)
        at tryModuleLoad (module.js:417:12)
        at Function.Module._load (module.js:409:3)
        at Module.require (module.js:468:17)
        at require (internal/module.js:20:19)
        at Object.<anonymous> (C:\Program Files (x86)\AppX Player\resources\app\s
    rc\addon\index.js:11:3)

I am targeting a windows ia32 architecture for electron and I rebuild it like the following:

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

The binding-gyp of the file looks like this and is based on this. It uses the openSSL static library

{

            "targets": [
                {
                    "target_name": "addon",
                    "sources": [ 
                        "./src/encryptor.cpp" ,
                        "./src/EncryptorHandler.cpp",
                        "./src/SetupHandler.cpp",
                        "./src/RC4Handler.cpp",
                        "./src/HardwareInfoHandler.cpp",
                        "../Encryptions/RC4.cpp",
                        "../Encryptions/AES.cpp",
                        "../Encryptions/utils.cpp",
                        "../oggEncDec/src/FileHandler.cpp",
                        "../oggEncDec/src/OGGSelectiveEncryptor.cpp",
                        "../machineIdentification/common.cpp"
                    ],
                    "cflags!": [ "-fno-exceptions" ],
                    "cflags_cc!": [ "-fno-exceptions" ],
                    'cflags': ['-fexceptions'],
                    'cflags_cc': ['-fexceptions -Wall -Wextra -Wconversion'],
                    "include_dirs": [
                        "<!(node -e \"require('nan')\")",
                        "../"
                    ],
                    'conditions': [
                        ['OS=="linux"', {
                                "sources": [ 
                                    "../machineIdentification/linuxHardwareInfo.cpp"
                                ],
                                'libraries': [ 
                                    '-lcrypto',
                                ],
                            }
                        ],
                        ['OS=="mac"', {
                                "sources": [ 
                                    "../machineIdentification/macHardwareInfo.cpp"
                                ],
                                'libraries': [ 
                                    '-lcrypto',
                                ],
                        }],
                        ['OS=="win"', {
                            'msvs_settings': {
                                'VCCLCompilerTool': {
                                    'AdditionalOptions': [ '/EHsc' ],
                                    'ExceptionHandling': 1
                                }
                            },
                            "sources": [ 
                                "../machineIdentification/windowsHardwareInfo.cpp"
                            ],
                            'conditions': [
                                # "openssl_root" is the directory on Windows of the OpenSSL files.
                                # Check the "target_arch" variable to set good default values for
                                # both 64-bit and 32-bit builds of the module.
                                ['target_arch=="x64"', {
                                    'variables': {
                                        'openssl_root%': 'C:/OpenSSL-Win64'
                                    },
                                }, {
                                    'variables': {
                                        'openssl_root%': 'C:/OpenSSL-Win32'
                                    },
                                }],
                              ],
                              'libraries': [ 
                                '-l<(openssl_root)/lib/libeay32.lib',
                              ],
                              'include_dirs': [
                                '<(openssl_root)/include',
                              ],
                        }]
                  ]
                }
            ]
        }

Just in case it was a dll missing (it should not be as I was linking the static library) I added the openSSL dll on the same level of the exe. What else may be causing this behaviour?

Edit Installing OpenSSL binaries make it works, i thought the static linking would take care of that so I wouldn't depend on external dll's

Edit 2 Everything would be solved if I could pack the static library and bundle it on the ".node" file. Using dependency walker on the .node file shows me that it is requiring the dll and what I need is for it to have the dll code on it.

回答1:

Turns out the lib on http://slproweb.com/products/Win32OpenSSL.html seems to be just a wrapper around the dll that still uses it. I misguided thought that node-gyp was just compiling the thing without the dependency which is not true. I found another precompiled openssl lib here that did the trick.

So to wrap up: I needed to ship something with OpenSSL under electron but electron does not expose OpenSSL like node does and switchs it for borinSSL. I followed tooTallNate article that recommended a static library and assumed the static library was right and I was somehow needing the DLL and I assumed node-gyp was not bundling the used static library. Switching the lib for another one (or better yet compiling it myself) did the trick.