electron-egg DLL使用

Dll文件是动态链接库,桌面软件中经常需要使用。

因为该库的调用,涉及许多工具的安装,因此不集成到框架中;但本文提供完整的安装使用说明,供大家参考。

  • 准备环境
# 编译工具
npm i -g node-gyp

# C++构建工具
1. 管理员模式打开PowerShell
2. npm --vs2015 i -g --production windows-build-tools
   或者 npm i -g --production windows-build-tools 
   
# 外部接口调用库
1. npm install ref-napi         // 基本类型
2. npm install ref-array-napi   // 数组类型
3. npm install ref-struct-napi  // 结构体类型
4. npm install ffi-napi         // 连接c代码和js代码

# 编译 ref-napi 库
1. cd ./node_modules/ref-napi
2. node-gyp configure  // 配置
3. node-gyp build			 // 编译
  • 测试代码
const ffi = require('ffi-napi');
var ref = require('ref-napi');
var ArrayType = require('ref-array-napi');
    
/**
 * exec dll file
 */
async execDll () {
  // 资源路径
  let dllPath = '';
  const dllFile = 'myDllDemo.dll';
  if (electronApp.isPackaged) {
    // 打包后  
    dllPath = path.join(this.app.config.execDir, "resources", "extraResources", "dll", dllFile);
  } else {
    // 打包前
    dllPath = path.join(this.app.config.execDir, "build", "extraResources", "dll", dllFile);
  }

  // 映射到C语言 int数组类型
  var IntArray = ArrayType(ref.types.int);

  // 加载 DLL文件,无需写扩展名,将DLL中的函数映射成JS方法
  const MyDellDemo = new ffi.Library(dllPath, {
    // 方法名必须与C函数名一致
    add: [
      'int', // 对应 C函数返回类型
      ['int', 'int'] // C函数参数列表
    ],
    // 使用 ffi中内置类型的简写类型
    addPtr: ['void', ['int', 'int', 'int*']],
    // IntArray 是上面通过 ArrayType 构建出来的类型
    initArray: ['void', [IntArray, 'int']]
  });

  // 调用add 方法
  const res = MyDellDemo.add(1, 2);
  console.log(`add method result of 1 + 2 is: ` + res);

  // 调用addPtr 方法
  // 使用Buffer类在C代码和JS代码之间实现了内存共享,让Buffer成为了C语言当中的指针。
  // C函数使用指针操作函数外部的内存,所以首先需要 分配一个int类型的内存空间 第一个参数为 C语言数据类型,第二个参数为 默认值
  var intBuf = ref.alloc(ref.types.int, 100);
  console.log('addPtr 调用前数据>>', ref.deref(intBuf)); //获取指向的内容
  MyDellDemo.addPtr(2, 2, intBuf); // 调用函数,传递指针
  console.log('addPtr 调用后数据>>', ref.deref(intBuf));

  // 调用initArray 方法
  // IntArray 是前面使用ref-napi 和 ref-array-napi 库创建的数据类型,数组的长度为 8
  // 这里一定要分配内存空间,否则 函数内的指针无法操作内存
  let myArray = new IntArray(8);
  MyDellDemo.initArray(myArray, 8);
  console.log('初始化数组执行结果:');
  for (var i = 0; i < myArray.length; i++) {
    console.log(myArray[i]);
  }

  return true;
} 
  • 测试使用的dll文件位置:
./build/extraResources/dll/myDllDemo.dll

猜你喜欢

转载自blog.csdn.net/qq_32660241/article/details/124347956