最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Electron如何通過(guò)ffi-napi調(diào)用dll導(dǎo)出接口

 更新時(shí)間:2025年02月24日 11:02:22   作者:野有蔓草W  
文章介紹了如何在Electron項(xiàng)目中使用ffi-napi模塊調(diào)用DLL文件,并詳細(xì)描述了環(huán)境搭建、安裝Electron和ffi-napi、配置Visual Studio和Python環(huán)境、解決常見(jiàn)問(wèn)題等步驟,感興趣的朋友跟隨小編一起看看吧

electron使用ffi-napi環(huán)境搭建

附打包好的ffi-napi可以直接放到項(xiàng)目目錄下使用,避免以后麻煩

一、安裝node.js

Node.js官網(wǎng):https://nodejs.org/zh-cn/download,選擇LTS長(zhǎng)期穩(wěn)定版本即可

需要注意Node.js 區(qū)分32和64位,32位版本只能加載32位的DLL,64位的版本只能加載64位的DLL
建議下載x64,nodejs的位數(shù)決定后續(xù)electron以及node-gyp的位數(shù),我剛開(kāi)始想編譯win32但是一直編譯不通過(guò)報(bào)錯(cuò)
報(bào)錯(cuò):npm error ‘“call”’ 不是內(nèi)部或外部命令,也不是可運(yùn)行的程序列或批處理文件。
安裝教程參考:https://www.runoob.com/nodejs/nodejs-install-setup.html
本來(lái)我在這里勾選了自動(dòng)安裝Visual Studio Build Tools和Python。但是后面發(fā)現(xiàn)下載需要3G,太大了,而且我本來(lái)電腦上裝了VS2017,我想用自帶的,所以后面又取消安裝了。不知道這里直接安裝的話,會(huì)不會(huì)就少了后續(xù)很多麻煩。。。。

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

安裝報(bào)錯(cuò):

CERT_HAS_EXPIRED 錯(cuò)誤通常是指在使用 HTTPS 協(xié)議進(jìn)行請(qǐng)求時(shí),SSL 證書(shū)已過(guò)期,導(dǎo)致請(qǐng)求失敗
解決https改為http

注意:直接使用npm install electron他會(huì)下載npm包管理中的較新版本的electron(例如v34.2.0)
在electron20.3.8之后調(diào)用C++動(dòng)態(tài)庫(kù)會(huì)出現(xiàn)

Error: Error in native callback

原因:Electron 21 及更高版本將啟用 V8 內(nèi)存隔離區(qū),這將對(duì)一些原生模塊產(chǎn)生影響。
解決方案:降低electron版本,目前論壇大部分使用20.3.8(20.3.8版本太舊,我使用npm到國(guó)外官方網(wǎng)站下載,一直下載不成功…最后使用cnpm安裝成功的)

cnpm install electron@20.3.8

三、安裝ffi-napi 安裝python

選擇最新版本python3.13.2 64位
下載地址:https://devguide.python.org/versions/
安裝時(shí)記得勾選寫(xiě)入環(huán)境變量

安裝VS

VS2022 Community下載地址:https://visualstudio.microsoft.com/zh-hans/thank-you-downloading-visual-studio/?sku=Community
我安裝的是vs2022 community(官方網(wǎng)站上寫(xiě)的,不知道專(zhuān)業(yè)版可不可行)
查看ffi-napi官網(wǎng)描述如下:

安裝 Visual C++ 構(gòu)建環(huán)境:
對(duì)于 Visual Studio 2019
或更高版本,請(qǐng)使用Visual Studio CommunityDesktop development with C++中的工作負(fù)載。對(duì)于
Visual Studio 2019 之前的版本,請(qǐng)使用選項(xiàng)安裝Visual Studio 構(gòu)建工具Visual C++ buildtools
論壇上看到的

我的系統(tǒng)時(shí)win10,并且我安裝VS2017時(shí)已經(jīng)安裝了Windows 10 SDK, 所以我取消了默認(rèn)的windows 11 SDK的安裝

安裝ffi-napi

此時(shí)安裝的軟件各個(gè)版本如下所示:

安裝ffi-napi時(shí)會(huì)用到node-gyp編譯工具,他對(duì)vs版本匹配非常嚴(yán)格
安裝成功?。。。?!

四、引用ffi-napi模塊

主進(jìn)程中: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中導(dǎo)出的接口,第一個(gè)int為返回值類(lèi)型,第二、三個(gè)是參數(shù)類(lèi)型
const libm = ffi.Library(dllPath, {
  'test': ['int',[ 'int','int']]
});
// 處理 IPC 消息
ipcMain.on('call-test', (event, args) => {
  const { a, b } = args;
  console.log('調(diào)用DLL函數(shù) test,參數(shù):', a, b);
  try {
    const result = libm.test(a, b);
    event.sender.send('test-result', { a, b, result });
  } catch (error) {
    console.error('DLL調(diào)用失敗:', error);
    event.sender.send('error', { message: 'DLL調(diào)用失敗', details: error.message });
  }
});

渲染進(jìn)程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(`錯(cuò)誤: ${data.message}`);
});

五、其他問(wèn)題

我在安裝過(guò)程中遇到了各種各樣的問(wèn)題,這里記錄一下,看是否用得到

1. 安裝ffi-napi報(bào)錯(cuò)。

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

這里是說(shuō)node-gyp找不到vs build tools,先確認(rèn)你是否安裝了vs build tools,如果已經(jīng)安裝了,網(wǎng)上搜索會(huì)讓你配置各種環(huán)境變量,都沒(méi)什么用,其實(shí)就是當(dāng)前node-gyp版本和vs版本不匹配

2. 編譯x86報(bào)錯(cuò):’“call”’ 不是內(nèi)部或外部命令,也不是可運(yùn)行的程序或批處理文件。

這個(gè)感覺(jué)是32位下的一個(gè)bug,我目前沒(méi)有找到解決方案

3. 如果安裝失敗,管理員運(yùn)行試一下 4. 實(shí)在不行就官網(wǎng)拉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

到此這篇關(guān)于Electron通過(guò)ffi-napi調(diào)用dll導(dǎo)出接口的文章就介紹到這了,更多相關(guān)Electron ffi-napi調(diào)用dll內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • NodeJS如何優(yōu)雅的實(shí)現(xiàn)Sleep休眠

    NodeJS如何優(yōu)雅的實(shí)現(xiàn)Sleep休眠

    這篇文章主要介紹了NodeJS如何優(yōu)雅的實(shí)現(xiàn)Sleep休眠問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • node異步使用await和不用await的區(qū)別實(shí)例分析

    node異步使用await和不用await的區(qū)別實(shí)例分析

    這篇文章主要介紹了node異步使用await和不用await的區(qū)別,結(jié)合實(shí)例形式分析了node.js異步使用await和不用await的實(shí)例中,同步與異步執(zhí)行的區(qū)別,需要的朋友可以參考下
    2023-06-06
  • Node服務(wù)端實(shí)戰(zhàn)之操作數(shù)據(jù)庫(kù)示例詳解

    Node服務(wù)端實(shí)戰(zhàn)之操作數(shù)據(jù)庫(kù)示例詳解

    這篇文章主要為大家介紹了Node服務(wù)端實(shí)戰(zhàn)之操作數(shù)據(jù)庫(kù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • docker中編譯nodejs并使用nginx啟動(dòng)

    docker中編譯nodejs并使用nginx啟動(dòng)

    這篇文章主要介紹了docker中編譯nodejs并使用nginx啟動(dòng)的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • node.js如何自定義實(shí)現(xiàn)一個(gè)EventEmitter

    node.js如何自定義實(shí)現(xiàn)一個(gè)EventEmitter

    我們了解到,Node采用了事件驅(qū)動(dòng)機(jī)制,而EventEmitter就是Node實(shí)現(xiàn)事件驅(qū)動(dòng)的基礎(chǔ),本文主要介紹了node.js自定義實(shí)現(xiàn)EventEmitter,感興趣的可以了解一下
    2021-07-07
  • 安裝node-sass的方法步驟

    安裝node-sass的方法步驟

    本文主要介紹了安裝node-sass的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Node.js中安全調(diào)用系統(tǒng)命令的方法(避免注入安全漏洞)

    Node.js中安全調(diào)用系統(tǒng)命令的方法(避免注入安全漏洞)

    這篇文章主要介紹了Node.js中安全調(diào)用系統(tǒng)命令的方法(避免注入安全漏洞),本文講解的一般是連接字符串會(huì)時(shí)出的安全問(wèn)題情況,需要的朋友可以參考下
    2014-12-12
  • 預(yù)防NodeJS命令注入的方法詳解

    預(yù)防NodeJS命令注入的方法詳解

    Node.js和npm為前端生態(tài)中提供了統(tǒng)一的開(kāi)發(fā)語(yǔ)言、強(qiáng)大的包管理和模塊生態(tài)系統(tǒng)、靈活的構(gòu)建工具和任務(wù)自動(dòng)化、以及豐富的前端框架和庫(kù)等等,本文給大家介紹了如何預(yù)防NodeJS命令注入,文中有詳細(xì)的代碼講解,需要的朋友可以參考下
    2023-12-12
  • Node.js API詳解之 V8模塊用法實(shí)例分析

    Node.js API詳解之 V8模塊用法實(shí)例分析

    這篇文章主要介紹了Node.js API詳解之 V8模塊用法,結(jié)合實(shí)例形式分析了Node.js API中V8模塊基本功能、函數(shù)、使用用法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-06-06
  • Node.js 使用AngularJS的方法示例

    Node.js 使用AngularJS的方法示例

    這篇文章主要介紹了Node.js 使用AngularJS的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05

最新評(píng)論

湾仔区| 新绛县| 锦州市| 张家港市| 那曲县| 新晃| 资源县| 清苑县| 济源市| 大厂| 日照市| 都兰县| 泽库县| 栖霞市| 洛浦县| 河间市| 泉州市| 铜川市| 洛阳市| 舟山市| 竹溪县| 青州市| 宁化县| 安阳县| 揭东县| 石狮市| 靖江市| 扬州市| 冀州市| 图片| 尼木县| 浦江县| 综艺| 清镇市| 义马市| 和顺县| 什邡市| 依安县| 探索| 安福县| 平遥县|