NodeJS ffi-napi无法加载.dll -在mac os上运行

v8wbuo2f  于 5个月前  发布在  Node.js
关注(0)|答案(1)|浏览(76)

我试图加载.dll在简单的js文件,以下官方文档here
如果我尝试从官方文件,它的工作

var ffi = require('ffi-napi');

var libm = ffi.Library('libm', {
  'ceil': [ 'double', [ 'double' ] ]
});
libm.ceil(1.5); // 2

// You can also access just functions in the current process by passing a null
var current = ffi.Library(null, {
  'atoi': [ 'int', [ 'string' ] ]
});
console.log(current.atoi('1234')); // 1234

字符串
但是,如果我尝试加载与dll.js文件在同一个文件夹中的.dll-代码如下所示

const ffi = require('ffi-napi');
const path = require('path');

const file = path.join(__dirname, '/testLibrary');

const pos = ffi.Library(file, {
  testFunction: ['void', ['int']],
});


我得到这个错误:

MacBook-Pro-3:serialPortTest martin$ node dll.js 
/Users/martin/Projects/serialPortTest/node_modules/ffi-napi/lib/dynamic_library.js:75
    throw new Error('Dynamic Linking Error: ' + err);
    ^

Error: Dynamic Linking Error: dlopen(/Users/martin/Projects/serialPortTest/testLibrary.dylib, 0x0002): tried: '/Users/martin/Projects/serialPortTest/testLibrary.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/martin/Projects/serialPortTest/testLibrary.dylib' (no such file), '/Users/martin/Projects/serialPortTest/testLibrary.dylib' (no such file)
    at new DynamicLibrary (/Users/martin/Projects/serialPortTest/node_modules/ffi-napi/lib/dynamic_library.js:75:11)
    at Object.Library (/Users/martin/Projects/serialPortTest/node_modules/ffi-napi/lib/library.js:47:10)
    at Object.<anonymous> (/Users/martin/Projects/serialPortTest/dll.js:6:17)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.18.2


我对自动添加到那里的.dylib扩展名感到困惑。
我试图添加.dll扩展名,但在日志中我可以看到/Users/martin/Projects/serialPortTest/testLibrary.dll.dylib
我尽可能地用谷歌搜索它,但没有任何帮助。
任何帮助都很感激。

8qgya5xd

8qgya5xd1#

简而言之:由于各种原因,您无法在macOS上加载Windows DLL,包括但不限于:DLL可能依赖的Windows特定内容,二进制文件格式(Mach-O vs PE),DLL编译所针对的CPU架构(特别是如果您使用Apple Silicon)。
如果您需要针对Windows特定的DLL开发解决方案,则需要使用Windows -无论是否虚拟化。

相关问题