Electron如何通过ffi-napi调用dll导出接口

 更新时间:2025年02月24日 11:02:22   作者:野有蔓草W  
文章介绍了如何在Electron项目中使用ffi-napi模块调用DLL文件,并详细描述了环境搭建、安装Electron和ffi-napi、配置Visual Studio和Python环境、解决常见问题等步骤,感兴趣的朋友跟随小编一起看看吧

electron使用ffi-napi环境搭建

附打包好的ffi-napi可以直接放到项目目录下使用,避免以后麻烦

一、安装node.js

Node.js官网:https://nodejs.org/zh-cn/download,选择LTS长期稳定版本即可

需要注意Node.js 区分32和64位,32位版本只能加载32位的DLL,64位的版本只能加载64位的DLL
建议下载x64,nodejs的位数决定后续electron以及node-gyp的位数,我刚开始想编译win32但是一直编译不通过报错
报错:npm error ‘“call”’ 不是内部或外部命令,也不是可运行的程序列或批处理文件。
安装教程参考:https://www.runoob.com/nodejs/nodejs-install-setup.html
本来我在这里勾选了自动安装Visual Studio Build Tools和Python。但是后面发现下载需要3G,太大了,而且我本来电脑上装了VS2017,我想用自带的,所以后面又取消安装了。不知道这里直接安装的话,会不会就少了后续很多麻烦。。。。

cmd命令行查看安装已成功

二、安装Electron

方法一:使用node原始包管理工具npm安装。-g 全局安装

npm install -g electron

方法二:使用淘宝提供的cnpm

npm install -g cnpm --registry=https://registry.npm.taobao.org

然后使用cnpm安装

cnpm install -g electron

安装报错:

CERT_HAS_EXPIRED 错误通常是指在使用 HTTPS 协议进行请求时,SSL 证书已过期,导致请求失败
解决https改为http

注意:直接使用npm install electron他会下载npm包管理中的较新版本的electron(例如v34.2.0)
在electron20.3.8之后调用C++动态库会出现

Error: Error in native callback

原因:Electron 21 及更高版本将启用 V8 内存隔离区,这将对一些原生模块产生影响。
解决方案:降低electron版本,目前论坛大部分使用20.3.8(20.3.8版本太旧,我使用npm到国外官方网站下载,一直下载不成功…最后使用cnpm安装成功的)

cnpm install electron@20.3.8

三、安装ffi-napi 安装python

选择最新版本python3.13.2 64位
下载地址:https://devguide.python.org/versions/
安装时记得勾选写入环境变量

安装VS

VS2022 Community下载地址:https://visualstudio.microsoft.com/zh-hans/thank-you-downloading-visual-studio/?sku=Community
我安装的是vs2022 community(官方网站上写的,不知道专业版可不可行)
查看ffi-napi官网描述如下:

安装 Visual C++ 构建环境:
对于 Visual Studio 2019
或更高版本,请使用Visual Studio CommunityDesktop development with C++中的工作负载。对于
Visual Studio 2019 之前的版本,请使用选项安装Visual Studio 构建工具Visual C++ buildtools
论坛上看到的

我的系统时win10,并且我安装VS2017时已经安装了Windows 10 SDK, 所以我取消了默认的windows 11 SDK的安装

安装ffi-napi

此时安装的软件各个版本如下所示:

安装ffi-napi时会用到node-gyp编译工具,他对vs版本匹配非常严格
安装成功!!!!!

四、引用ffi-napi模块

主进程中:main.js

const { app, BrowserWindow, ipcMain} = require('electron');
const ffi = require('ffi-napi');
const ref = require('ref-napi');
const path = require('path');
const dllPath = path.join(__dirname, './test_api-x64.dll');
//test为dll中导出的接口,第一个int为返回值类型,第二、三个是参数类型
const libm = ffi.Library(dllPath, {
  'test': ['int',[ 'int','int']]
});
// 处理 IPC 消息
ipcMain.on('call-test', (event, args) => {
  const { a, b } = args;
  console.log('调用DLL函数 test,参数:', a, b);
  try {
    const result = libm.test(a, b);
    event.sender.send('test-result', { a, b, result });
  } catch (error) {
    console.error('DLL调用失败:', error);
    event.sender.send('error', { message: 'DLL调用失败', details: error.message });
  }
});

渲染进程render.js

const { ipcRenderer } = require('electron');
const button_test = document.getElementById('button_test');
const textBox_result = document.getElementById('textBox');
button_test.addEventListener('click', () => {
  const a = 5;
  const b = 6;
  ipcRenderer.send('call-test', { a, b });
});
ipcRenderer.on('test-result', (event, data) => {
  textBox_result.value = `test(${data.a}, ${data.b}) = ${data.result}`;
});
ipcRenderer.on('error', (event, data) => {
  alert(`错误: ${data.message}`);
});

五、其他问题

我在安装过程中遇到了各种各样的问题,这里记录一下,看是否用得到

1. 安装ffi-napi报错。

npm install ffi-napi

gyp ERR! find VS gyp ERR! find VS msvs_version not set from command
line or npm config gyp ERR! find VS running in VS Command Prompt,
installation path is: gyp ERR! find VS “C:\Program Files
(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC” gyp
ERR! find VS - will only use this version gyp ERR! find VS could not
use PowerShell to find Visual Studio 2017 or newer gyp ERR! find VS
looking for Visual Studio 2015 gyp ERR! find VS - not found gyp ERR!
find VS not looking for VS2013 as it is only supported up to Node.js 8
gyp ERR! find VS gyp ERR! find VS
************************************************************** gyp ERR! find VS You need to install the latest version of Visual Studio
gyp ERR! find VS including the “Desktop development with C++”
workload. gyp ERR! find VS For more information consult the
documentation at: gyp ERR! find VS
https://github.com/nodejs/node-gyp#on-windows gyp ERR! find VS
************************************************************** gyp ERR! find VS gyp ERR! configure error gyp ERR! stack Error: Could not
find any Visual Studio installation to use gyp ERR! stack at
VisualStudioFinder.fail
(D:\soft\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:121:47)
gyp ERR! stack at
D:\soft\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:74:16
gyp ERR! stack at VisualStudioFinder.findVisualStudio2013
(D:\soft\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:351:14)
gyp ERR! stack at
D:\soft\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:70:14
gyp ERR! stack at
D:\soft\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:372:16
gyp ERR! stack at
D:\soft\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
gyp ERR! stack at
D:\soft\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
gyp ERR! stack at ChildProcess.exithandler
(child_process.js:390:5) gyp ERR! stack at ChildProcess.emit
(events.js:400:28) gyp ERR! stack at maybeClose
(internal/child_process.js:1088:16) gyp ERR! System Windows_NT
10.0.19045 gyp ERR! command “D:\soft\nodejs\node.exe” “D:\soft\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js”
“rebuild” gyp ERR! cwd D:\DemoCode\electronFFI\node_modules\ffi-napi
gyp ERR! node -v v14.21.3 gyp ERR! node-gyp -v v5.1.1 gyp ERR! not ok
npm WARN enoent ENOENT: no such file or directory, open
‘D:\DemoCode\electronFFI\package.json’ npm WARN electronFFI No
description npm WARN electronFFI No repository field. npm WARN
electronFFI No README data npm WARN electronFFI No license field.

npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! ffi-napi@4.0.3
install: node-gyp-build npm ERR! Exit status 1 npm ERR! npm ERR!
Failed at the ffi-napi@4.0.3 install script. npm ERR! This is probably
not a problem with npm. There is likely additional logging output
above.

npm ERR! A complete log of this run can be found in: npm ERR!
C:\Users\weidongcao\AppData\Roaming\npm-cache_logs\2025-02-11T07_45_19_949Z-debug.log

这里是说node-gyp找不到vs build tools,先确认你是否安装了vs build tools,如果已经安装了,网上搜索会让你配置各种环境变量,都没什么用,其实就是当前node-gyp版本和vs版本不匹配

2. 编译x86报错:’“call”’ 不是内部或外部命令,也不是可运行的程序或批处理文件。

这个感觉是32位下的一个bug,我目前没有找到解决方案

3. 如果安装失败,管理员运行试一下 4. 实在不行就官网拉ffi-napi源码,自己编译

源码地址:https://github.com/node-ffi-napi/node-ffi-napi
在ffi-napi目录下直接编译

node-gyp clean 
node-gyp configure --msvs_version=2022
node-gyp build

提示安装addon

npm install node-addon-api

到此这篇关于Electron通过ffi-napi调用dll导出接口的文章就介绍到这了,更多相关Electron ffi-napi调用dll内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • NodeJS如何优雅的实现Sleep休眠

    NodeJS如何优雅的实现Sleep休眠

    这篇文章主要介绍了NodeJS如何优雅的实现Sleep休眠问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-09-09
  • node异步使用await和不用await的区别实例分析

    node异步使用await和不用await的区别实例分析

    这篇文章主要介绍了node异步使用await和不用await的区别,结合实例形式分析了node.js异步使用await和不用await的实例中,同步与异步执行的区别,需要的朋友可以参考下
    2023-06-06
  • Node服务端实战之操作数据库示例详解

    Node服务端实战之操作数据库示例详解

    这篇文章主要为大家介绍了Node服务端实战之操作数据库示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • docker中编译nodejs并使用nginx启动

    docker中编译nodejs并使用nginx启动

    这篇文章主要介绍了docker中编译nodejs并使用nginx启动的相关资料,需要的朋友可以参考下
    2017-06-06
  • node.js如何自定义实现一个EventEmitter

    node.js如何自定义实现一个EventEmitter

    我们了解到,Node采用了事件驱动机制,而EventEmitter就是Node实现事件驱动的基础,本文主要介绍了node.js自定义实现EventEmitter,感兴趣的可以了解一下
    2021-07-07
  • 安装node-sass的方法步骤

    安装node-sass的方法步骤

    本文主要介绍了安装node-sass的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Node.js中安全调用系统命令的方法(避免注入安全漏洞)

    Node.js中安全调用系统命令的方法(避免注入安全漏洞)

    这篇文章主要介绍了Node.js中安全调用系统命令的方法(避免注入安全漏洞),本文讲解的一般是连接字符串会时出的安全问题情况,需要的朋友可以参考下
    2014-12-12
  • 预防NodeJS命令注入的方法详解

    预防NodeJS命令注入的方法详解

    Node.js和npm为前端生态中提供了统一的开发语言、强大的包管理和模块生态系统、灵活的构建工具和任务自动化、以及丰富的前端框架和库等等,本文给大家介绍了如何预防NodeJS命令注入,文中有详细的代码讲解,需要的朋友可以参考下
    2023-12-12
  • Node.js API详解之 V8模块用法实例分析

    Node.js API详解之 V8模块用法实例分析

    这篇文章主要介绍了Node.js API详解之 V8模块用法,结合实例形式分析了Node.js API中V8模块基本功能、函数、使用用法及操作注意事项,需要的朋友可以参考下
    2020-06-06
  • Node.js 使用AngularJS的方法示例

    Node.js 使用AngularJS的方法示例

    这篇文章主要介绍了Node.js 使用AngularJS的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05

最新评论