Node.js调用动态链接库

创建日期:2024-06-21
更新日期:2024-12-27

依赖项

{
  "name": "invoke-cpp-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "rebuild": "./node_modules/.bin/electron-rebuild",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "liteng",
  "license": "ISC",
  "dependencies": {
    "electron": "^8.5.5",
    "electron-rebuild": "^1.10.0",
    "ffi-napi": "^4.0.3",
    "ref-array-napi": "^1.2.2",
    "ref-napi": "^3.0.3",
    "ref-struct-napi": "^1.1.1",
    "ref-wchar-napi": "^1.0.3"
  },
  "devDependencies": {}
}

示例代码

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

const dll = path.resolve('./vcdll/elec_test.dll');

const lib = ffi.Library(dll, {
    elec_add: ['int', ['int', 'int']]
});

console.log('1 + 2 = ' + lib.elec_add(1, 2));