electron 下使用node-ffi

关于为什么要使用ffi就不多说了,在electron中ffi的实际使用过程中遇到了一系列的问题,踩了不少坑,网上搜了好久也没见到好的解决方案。最终功夫不负有心人总算是解决掉了,现在将实际安装步骤写下来,希望能为后来者提供些许帮助。

写的比较简单,如果有疑问可以留言


各种环境软件以及版本

软件 版本 架构
系统 win10 x64
终端 powershell -
nodejs 8.9.3 x86
electron 3.0.3 x86
python 2.7.15 x86

  1. 执行
npm install ffi
  1. 到github:https://github.com/node-ffi/node-ffi 下载下来源码,覆盖ffi目录
  2. 保证node-gyp环境已安装
npm install -g windows-build-tools
npm install -g node-gyp
  1. 到ffi和ref目录执行
node-gyp rebuild --arch=ia32 --dist-url=https://atom.io/download/atom-shell --runtime=electron --target=3.0.3

target 是electron版本号
arch 是系统架构 (x64)

  1. 编写dll
    注意不同位数electron要使用对应位数的dll
  2. js代码
const ffi = require('ffi');
const libm = ffi.Library(`${__dirname}/AINoteEncrypt`, {
  'add': [ 'int', [ 'int','int' ] ],
  'get_json_value': [ 'string', [ 'string','string' ] ],
  'create_table': [ 'int', [ 'int','int' ] ]
  
});


const ainote_encrypt = {
    add:(a,b)=>{
        return libm.add(a,b);
    }
}

module.exports = ainote_encrypt

7.常见异常

Error: Dynamic Symbol Retrieval Error: Win32 error 127
方法对不上,js的方法c++没有定义
Error: Dynamic Linking Error: Win32 error 193
dll 位数不对应,例如electron是32位的dll是64位的

你可能感兴趣的:(electron)