Node-ffi: Loading a C++ dll(with external dependencies) in js is getting crashed

834 Views Asked by At

Iam trying to load a c++ dll in JS using node-ffi. When the dll does not have any external dependencies, it works fine as expected. But it crashes, when it has any external dependencies.

const ffi = require("@saleae/ffi");

const MyDll = new ffi.Library("./MyDll",{
    "Sum":[ "int32", ["int32", "int32"] ],
    "Subtraction":[ "int32", ["int32", "int32"] ],
    "Execute":[ "int32",["string"] ]
});

let a = 10;
let b = 20;
let AddRes = MyDll.Sum(a, b);
let SubRes = MyDll.Subtraction(10, 20);

When "MyDll" has a external dependency, it crashes and throws the below mentioned error.

 throw new Error('Dynamic Linking Error: ' + err)
    ^

Error: Dynamic Linking Error: Win32 error 126
    at new DynamicLibrary (D:\Workspace\AddOnsChk1LoadDll\node_modules\@saleae\ffi\lib\dynamic_library.js:74:11)
    at new Library (D:\Workspace\AddOnsChk1LoadDll\node_modules\@saleae\ffi\lib\library.js:45:12)
    at Object.<anonymous> (D:\Workspace\AddOnsChk1LoadDll\app.js:5:15)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
1

There are 1 best solutions below

0
On

This github issue helps.

Summary

  1. (Windows) Put dependencies in System32 sometimes work. Ref
  2. Put required libraries in project root path. Ref
  3. Set runpath/rpath while you create the dynamic library. Ref
  4. Dll path changed with path.join. Ref
    const dllFunctions = ffi.Library(path.join(static__, myDll.dll), { // some code });
    (I'm not sure how does #4 work, but someone has a thumb up on it)