I'm trying to load .dll in simple js file, following official docs here.
If I try from official doc this, it works
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
But if I try to load the provided .dll that I have in the same folder as the dll.js file - the code looks like this
const ffi = require('ffi-napi');
const path = require('path');
const file = path.join(__dirname, '/testLibrary');
const pos = ffi.Library(file, {
testFunction: ['void', ['int']],
});
I get this error:
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
I'm confused by the .dylib extension that is added there automatically.
I tried to add .dll extension, but then in the log I can see /Users/martin/Projects/serialPortTest/testLibrary.dll.dylib.
I stackoverflowed/googled it as I could, but nothing helped.
Any help is appreciated.
In short: you can't load a Windows DLL on macOS for a variety of reasons, including but not limited to: Windows-specific things the DLL may depend on, the binary file format (Mach-O vs PE), the CPU architecture the DLL has been compiled against (especially so if you're on Apple Silicon).
If you need to develop a solution against a Windows-specific DLL, you will need to use Windows – either virtualized or not.