Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Got Error "Dynamic Symbol Retrieval Error: Win32 error 127" when load DlL from nodejs

I want to load a dll file from node js. Here is the header file:

#pragma once
#ifdef __cplusplus
#define EXAMPLE __declspec(dllexport) 

extern "C" {
    EXAMPLE int Add(int, int);
}
#endif

In compile as, i choose "compile as C code"

In active solution platform, I choose x64

And then, I use ffi module to load it:

var ffi = require('ffi');

var Lib = ffi.Library('test', {'Add' : ['int',['int','int']]});

But I got an Error:

C:\Users\TheHai\node_modules\ffi\lib\dynamic_library.js:112
    throw new Error('Dynamic Symbol Retrieval Error: ' + this.error())
    ^

Error: Dynamic Symbol Retrieval Error: Win32 error 127
    at DynamicLibrary.get (C:\Users\TheHai\node_modules\ffi\lib\dynamic_library.js:112:11)
    at C:\Users\TheHai\node_modules\ffi\lib\library.js:50:19
    at Array.forEach (native)
    at Object.Library (C:\Users\TheHai\node_modules\ffi\lib\library.js:47:28)
    at Object.<anonymous> (C:\Users\TheHai\Downloads\Compressed\nodejs-websocket-master\samples\chat\server.js:8:15)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
like image 688
Kyuubi Avatar asked Sep 04 '16 17:09

Kyuubi


3 Answers

Just in case someone else lands here...

In the example above, ffi.Library has (basically) two parameters: The first is the path name to the (dll) file; the second defines the functions that are to be referenced (function name : [return_type], [parameter_type],...).

I'm not 100% sure on the error numbers, but I "think" if you get error 126 it indicates an issue with the first parameter - it can't find the file (to test just try using a fullpath, and debug from there).

If you get error 127 (which is being reported here) it indicates an issue with the 2nd parameter - it can't find the listed function in the specified dll.

This usually indicates an issue with how the dll is being compiled. In the above example it states it was compiled as 'C' (not C++), this would negate the inclusion of the export due to #if __cplusplus.

I think if the project was compiled as C++, it would work.

As an alternate, this is an example that works for me (in my export.h file).

#if defined(WIN32) || defined(_WIN32)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif

#ifdef __cplusplus
extern "C" {
#endif

EXPORT int PWDownload_Start(int iHeartbeat);

#ifdef __cplusplus
}
#endif

Your mileage may vary

like image 154
WorldTurnedUpsideDown Avatar answered Oct 18 '22 10:10

WorldTurnedUpsideDown


PSA: This error can also happen if you declare a function that FFI can not find in the DLL.

I had the same error after generating a function list from a header file and reverting to the hand written file fixed the issue.

like image 2
Ray Hulha Avatar answered Oct 18 '22 10:10

Ray Hulha


I too faced the same problem.I don't the exact reason for this error.But I changed the following(Just change DLL name from 'test' to './test') and it works. You too try the same and let me know if it works.Thanks

var ffi = require('ffi');

var Lib = ffi.Library('./test', {'Add' : ['int',['int','int']]});
like image 1
vairav Avatar answered Oct 18 '22 10:10

vairav