electron+dll调用

569 阅读1分钟

想要完成蓝牙window上通讯功能,找到一个第三方的dll库,需要在js中调用dll文件。 做了一番调研之后,选用Koffi。 已经能实现dll中方法调用,非常顺畅。

Koffi安装

你可以使用 npm 安装 Koffi:

npm install koffi

安装 Koffi 后,在主进程开始加载它:

// CommonJS syntax
const koffi = require('koffi');

调用示例

  • 调用示例如下

  testDll () {
    // 资源路径
    const dllFile = 'myDllDemo.dll';
     const dllPath = path.join(Ps.getExtraResourcesDir(), "dll", dllFile);
     const lib = koffi.load(dllPath);
     const add = lib.func('__stdcall', 'add', 'int', ['int', 'int']);
     const res = add(1, 2);
    console.log(`add method result of 1 + 2 is: ` + res);
    return true;
  }

参考文章

函数调用 | koffi中文 (gitbook.io)