前端(node.js)调用dll动态链接库
Ⅰ- 壹 - 需求
使用 js node 调用dll 动态链接库.
(图片来源网络,侵删)
github地址如下,包含dll,里面就一个Add方法暴露出来
github
Ⅱ - 贰 - 两种方式调用dll
当前开发环境
Windows 11 22H2
node v16.20.0
Python 3.11.2
一 node直接调用
需要安装这俩库
(图片来源网络,侵删)
ffi-napi
npm i ffi-napi npm i -g node-gyp
app.js
var ffi = require('ffi-napi'); var libm = ffi.Library('./add.dll', { 'Add': [ 'int', // 对应 C函数返回类型 ['int', 'int'] // C函数参数列表 ], }); console.log(libm.Add(6, 5)); // 2
二 node调python调dll
第一种不好使,曲线救国的方式这个是。
python-shell
npm install python-shell
app.js
var { PythonShell } = require('python-shell'); // mode 类型: json text binary(默认) var pyshell = new PythonShell('index.py', { mode: 'text' }); //js传送给python的数据 pyshell.send(JSON.stringify({ a: 9, b: 4 })); //在实施python代码之后,将数据从python传递给js。 //传递给python的数据存储在“data”中。 pyshell.on('message', function (data) { console.log(data); });
python
(图片来源网络,侵删)
import json import sys from ctypes import cdll # 导入 dll 该文件 有一个 Add方法 累加的 DLLFUN = cdll.LoadLibrary("./add.dll") data = sys.stdin.readline() # 从js 传过来 获取数据 jsonData = json.loads(data) # 转换为字典 js中的对象 dlladdRes=DLLFUN.Add(jsonData['a'],jsonData['b']) print(dlladdRes) # num=int(data) # 数据转换为 int # def sum(a): # return a+3 # print(type(data)) # print(getattr(data,'a')) # print(DLLFUN.Add(1,2)) # print(sum(num)) #print()的内容交付给js # 读取 json文件中 的数据 发送给js 接收 # import sys # import json # # (3-1)打开JSON文件test.json # f = open("./pyjson.json", "r") # # (3-2)读取test.json的数据 # json_dict = json.load(f) # # (3-3)JSON数据json_dict的key:'stoch_访问num’。 # dat1 = json_dict["stock_num"] # print(dat1) # 将打印内容返回给python-shell
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...