node调用动态链接库(.dll)

5 篇文章 0 订阅
3 篇文章 0 订阅

一、系统windows10
二、应用环境
1.安装node
2.安装node-gyp
3.安装windows-build-tools

npm install -g node-gyp //依赖python2.7,包含在windows-build-tools中
npm install --global --production windows-build-tools(在管理员权限打开的命令行中执行)

4.设置python环境变量,找到在安装windows-build-tools过程中安装的python2.7安装目录,并添加到环境变量
5.安装node-ffi

npm install ffi

6.安装ref

npm install ref

7.主要代码:

'use strict';
const path = require('path');
const refArray = require('ref-array');
let ref = require('ref');
let ffi = require('ffi');
let mydll = path.join(__dirname, '../dll/UhfReader_API_HID.dll');
let anyType = ref.types.void;
let pointer = ref.refType(anyType);

//设置path环境,解决DLL还有引用其他DLL文件,但是找不到引用的 DLL 文件。(Dynamic Linking Error: Win32 error 126)错误
process.env.PATH = `${process.env.PATH}${path.delimiter}C:\\work\\project\\node-dll\\dll`;
let scriptDLLObj = ffi.Library(mydll, {
    'UhfSearchHids': ['int', ['string']],
    'UhfReaderConnect': ['int', [pointer, 'string', 'int']],
    'UhfReaderDisconnect': ['int', [pointer, 'int']]
});
let serials = ref.alloc('string');//给字符串变量分配存储
let handle = ref.alloc(anyType, ref.NULL_POINTER);//给指针分配存储并赋值
console.log(scriptDLLObj.UhfSearchHids(serials)); //搜索HID的数量和序列号
console.log(scriptDLLObj.UhfReaderConnect(handle, '0065DD31', 0x05));//打开串口并与 RLM 建立链接
console.log(scriptDLLObj.UhfReaderDisconnect(handle, 1));//关闭 RLM 通信端口

8.github demo
参考:
node-gyp:https://github.com/nodejs/node-gyp#installation
node-ffi:https://www.npmjs.com/package/ffi
ref: https://github.com/TooTallNate/ref
https://www.jianshu.com/p/79bbb96e0065
https://www.jianshu.com/p/5af3ad2b0856
https://blog.csdn.net/zhulin2609/article/details/51474676
https://www.v2ex.com/amp/t/474611

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wl_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值