1.创建electron空工程
npm init
安装electron,21版本以上有兼容问题,故用20
npm install --save-dev electron
npm install --save-dev electron@20.0.0
具体创建项目过程参考https://www.electronjs.org/zh/docs/latest/tutorial/tutorial-first-app
2.创建C++工程
3.调用C++dll
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<br/>
<br/>
<input id="numA" type="text"/>
<input id="numB" type="text"/>
<button id="CalcButtonA">调用dll计算+</button>
<button id="CalcButtonM">调用dll计算*</button>
</body>
<script>
const { electron, BrowserWindow, dialog }= require("electron")
const exec = require('child_process').exec
let Open1Window = null;
let Open2Window = null;
let Open3Window = null;
//call ffi test
const ffi = require("ffi-napi");
const myAddDll = new ffi.Library('./dll/myAddDll', {
'funAdd':
[
'int', ['int', 'int'],
],
'funMul':
[
'int', ['int', 'int'],
],
});
document.getElementById("CalcButtonA").onclick = function(){
console.log("Add--------")
const result = myAddDll.funAdd(parseInt(document.getElementById("numA").value), parseInt(document.getElementById("numB").value))
console.log("Add:" + result)
}
document.getElementById("CalcButtonM").onclick = function(){
console.log("Mul--------")
const result = myAddDll.funMul(parseInt(document.getElementById("numA").value), parseInt(document.getElementById("numB").value))
console.log("Mul:" + result)
}
//...call ffi test
</script>
</html>
工程源码