6

我想从节点 js 加载一个 dll 文件。这是头文件:

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

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

在编译为,我选择“编译为 C 代码”

在主动解决方案平台,我选择x64

然后,我使用 ffi 模块来加载它:

var ffi = require('ffi');

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

但我得到一个错误:

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)
4

3 回答 3

2

以防万一其他人降落在这里...

在上面的例子中,ffi.Library(基本上)有两个参数:第一个是(dll)文件的路径名;第二个定义要引用的函数(函数名称:[return_type]、[parameter_type]、...)。

我不是 100% 确定错误编号,但我“认为”如果您收到错误 126,则表明第一个参数有问题 - 它找不到文件(测试只需尝试使用完整路径,然后从那里)。

如果您收到错误 127(此处报告),则表明第二个参数存在问题 - 它无法在指定的 dll 中找到列出的函数。

这通常表明 dll 的编译方式存在问题。在上面的示例中,它声明它被编译为“C”(不是 C++),这将否定包含由于#if __cplusplus 的导出。

我认为如果该项目被编译为 C++,它会工作。

作为替代,这是一个适用于我的示例(在我的 export.h 文件中)。

#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

你的旅费可能会改变

于 2019-10-18T16:53:02.310 回答
1

PSA:如果您声明了 FFI 在 DLL 中找不到的函数,也会发生此错误。

从头文件生成函数列表并恢复到手写文件后,我遇到了同样的错误,解决了这个问题。

于 2018-12-12T01:04:00.963 回答
1

我也遇到了同样的问题。我不知道这个错误的确切原因。但是我更改了以下内容(只需将 DLL 名称从 'test' 更改为 './test')并且它可以工作。你也试试同样的方法,让我知道它是否有效。谢谢

var ffi = require('ffi');

var Lib = ffi.Library('./test', {'Add' : ['int',['int','int']]});
于 2016-11-15T11:21:59.790 回答