1

node Foreign Function Interface 中最基本的示例是atoi通过从 node 自己的进程加载来调用:

var ffi = require('ffi');

var current = ffi.Library(null, {
  'atoi': [ 'int', [ 'string' ] ]
});
console.log(typeof current.atoi('1234')); // 1234

但我得到这个错误:

    throw new Error('Dynamic Symbol Retrieval Error: ' + this.error())
    ^

Error: Dynamic Symbol Retrieval Error: Win32 error 127
    at DynamicLibrary.get (D:\web\node_modules\ffi\lib\dynamic_library.js:112:11)
    at D:\web\node_modules\ffi\lib\library.js:50:19
    at Array.forEach (native)
    at Object.Library (D:\web\node_modules\ffi\lib\library.js:47:28)
    at Object.<anonymous> (D:\web\native\winapi.js:5:19)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)

显然它与 32/64 位不兼容无关,因为我正在访问自己的进程。那么究竟有什么问题呢?

  • 平台:Windows 7x64
  • 节点:v6.2.2
  • 菲:2.2.0
  • 参考:1.3.3
4

3 回答 3

0

Visual Studio 使用 cpp 编译默认值,尝试用“extern c”包装你的 dll 函数,例如:

#ifdef __cplusplus
extern "C" {
#endif
    __declspec(dllexport) int add(int a, int b);
#ifdef __cplusplus
}
#endif

顺便说一句,一个正确的 ffi 示例:

首先,安装 ffi 和 node-gpy 包然后,在你的 js 代码中:

const ffi = require('ffi');
var libm = ffi.Library(__dirname + '/dll/add.dll', {
    'add': ['int', ['int', 'int']]
});

console.log(libm.add(1,2));
于 2018-04-18T02:28:24.293 回答
0

ffi.library 的第一个参数应该是您尝试加载的 dll 的名称,您遇到的错误与缺少符号有关。

于 2017-03-14T09:08:57.130 回答
0

最近,我也遇到了使用node-ffi加载DLL的错误。我的解决办法是: 1. 引入的DLL是否支持64位或32位等当前计算机系统。我之前犯过这个错误,当我将DLL文件转换为64位时,问题就解决了!

于 2018-05-31T02:45:05.487 回答