forked from wzdxy/electron-ffi-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
76 lines (67 loc) · 1.98 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<style>
#hello,#str-length{
color: #ff0000;
}
#plus>input{
width: 150px;
}
</style>
</head>
<body>
<h3>C++ Dll hello:
<span id="hello"></span>
</h3>
<div id='plus'>
<h3 style="display:inline-block">C++ Function Plus:</h3>
<input type="text" value="1">+
<input type="text" value="2">=
<input type="text">
<button type="button" onclick="plus()">计算</button>
</div>
<h3>C++ Function strlen() :
<span id="str-length">0</span>
<br>
<textarea id="str-input" cols="30" rows="10" onchange="strlen(this)" oninput="strlen(this)" placeholder="Input something"></textarea>
</h3>
We are using node
<script>document.write(process.versions.node)</script>, Chrome
<script>document.write(process.versions.chrome)</script>, and Electron
<script>document.write(process.versions.electron)</script>.
<script>
window.onload = function () {
try {
// Call *.dll with ffi
let ffi = require('ffi');
// Create funtions
window.Dll = ffi.Library('dll/MyDLL.dll', {
'Add': ['float', ['float', 'float']],
'Hello': ['string', []],
'StrLength': ['int', ['string']]
})
console.log('fii.Library result:',Dll);
// Call C++ function Hello
document.getElementById('hello').innerHTML = Dll.Hello()
} catch (error) {
console.error('ffi.Library', error);
}
}
function plus() {
let inputs = document.getElementById('plus').getElementsByTagName('input');
let a = Number(inputs[1].value), b = Number(inputs[0].value);
// Call C++ function Add
let sum = Dll.Add(a, b);
console.log(`${a}+${b}=${sum}`);
inputs[2].value = sum;
}
function strlen(el) {
// Call C++ function StrLength
document.getElementById('str-length').innerHTML = Dll.StrLength(el.value);
}
</script>
</body>
</html>