当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Nodejs调用Dll模块的方法

Nodejs调用Dll模块的方法

2018年09月23日  | 移动技术网IT编程  | 我要评论

公司项目采用electron()开发pc应用,会涉及到与底层硬件设备的通信,而sdk封装 基本上都是通过 c++ 动态链接库dll实现的。

有两种方案可供选择:

  • 方案一: 使用node-ffi
  • 方案二: 使用c++编写一个node addon,通过loadlibrary调用dll

以上两种方案都可以解决dll调用问题,方案选型要个人对c++ 的掌握程度,如果熟悉c++开发,可以直接选择方案二最方便。如果完全不了解c++,那么只能采用方案一。

由于笔主不太懂c++,最终选择第一种方案。

二、什么是node-ffi?

( …

node-ffi是使用纯javascript加载和调用动态库的node addon,它可以用来在不写任何c++代码的情况下调用动态链接库的api 接口。

ffi究竟干了什么?其实它本质上还是一个编译后的node addon,node_modules/ffi/build/release/ffi_bindings.node, ffi_bindings.node就是一个addon ffi充当了nodejs和dll之间的桥梁。

下面是一个简单的加载dll的demo实例:

var ffi = require('ffi');
var libpath = path.join(_dirname, '/test.dll');
var testlib = ffi.library(libpath, {
 'start': ['bool', ['bool']]
});
testlib.start(true); // true

三、安装node-ffi

npm install ffi

如果本地没有安装编译node addon的环境会报错,如下图所示

无论是使用ffi,还是直接写node addon,都缺少不了编译node addon这个步骤,要编译node addon,有两种方法:

1、node-gyp(  … )。

npm install node-gyp

具体安装参考:github.com/nodejs/node…

总结来说需要以下四点:

python 2.7-3.0版本之间 (推荐装v2.7,v3.x.x是不支持的)

net framework 4.5.1

visual c++编译工具 (在windows中是不需要安装vs,如果自己安装例如vs2015,导致编译报错error msb4132: the tools version "2.0" is unrecognized. available tools versions are "4.0".这个问题,说明没有装好编译器,又或者编译器没有被正确地识别, node-gyp的文档建议使用npm config set msvs_version 2015, 但是有些机器即使这样设置了也无效,需要手动设置msvs_version, 应该这样写: node-gyp rebuild --msvs_version=2015。如果因为安装了vs2015导致无法正常编译,可直接恢复到安装vs之前的还原点)
环境变量配置。(注:python安装位置需要添加到环境变量)

2、electron-rebuild( … )

如果采用electron开发应用程序,electron同样也支持node原生模块,但由于和官方的node 相比使用了不同的 v8 引擎,如果你想编译原生模块,则需要手动设置electron的headers的位置。

electron-rebuild为多个版本的node和electron提供了一种简单发布预编译二进制原生模块的方法。 它可以重建electron模块,识别当前electron版本,帮你自动完成了下载 headers、编译原生模块等步骤。 一个下载 electron-rebuild 并重新编译的例子:

npm install --save-dev electron-rebuild
# 每次运行"npm install"时,也运行这条命令
./node_modules/.bin/electron-rebuild
# 在windows下如果上述命令遇到了问题,尝试这个:
.\node_modules\.bin\electron-rebuild.cmd

详情请看 electronjs.org/docs/tutori…

这里需要注意nodejs版本问题,nodejs平台必须跟dll保持一致,同样是32位或者64位,如果两者不一致,会导致调用dll失败。

成功安装ffi模块之后,就可以开始我们下面的ffi调用dll的实例应用。

四、应用举例

在开发需求中,需要调用基于c++编写的tcp数据转发服务的sdk。

首先我们来看一下dll头文件接口声明的代码如下:

#ifndef js_connection_sdk
#define js_connection_sdk
#ifdef js_sdk
#define c_export __declspec(dllexport)
#else
#define c_export __declspec(dllimport)
#endif
extern "c"
{
  typedef void(*receivecallback) (int cmd, int seq, const char *data);
  /*设置读取数据回调*/
  c_export void _cdecl setreceivecallback(receivecallback callback);
  /*
  *设置option
  */
  c_export void _cdecl setoption(
    const char* appkey, 
    const char* tk,
    int lc, 
    int rm
  );
  /*
  *创建连接
  */
  c_export bool _cdecl createconnection();
  /*发送数据*/
  c_export bool _cdecl senddata(int cmd, int seq, const char *data, unsigned int len);
  /*释放连接*/
  c_export void _cdecl releaseconnection();
}
#endif

ffi调用dll模块封装,代码如下:

try {
 const ffi = require('ffi');
 const path = require('path');
 const buffer = require('buffer').buffer;
 const libpath = path.join(app_path, '..', '..', '/testsdk.dll');
 
 const sdklib = ffi.library(libpath, {
 'createconnection': ['bool', []],
 'senddata': ['bool', ['int', 'int', 'string', 'int']],
 'releaseconnection': ['void', []],
 'setoption': ['void', ['string', 'string', 'int', 'int']],
 'setreceivecallback': ['void', ['pointer']]
 });
 
 module.exports = {
 createconnection: function(){
  sdklib.createconnection();
 },
 setreceivecallback(cb) {
  global.setreceivecallback = ffi.callback('void', ['int', 'int', 'string'], function(cmd, seq, data){
  cb && cb(cmd, seq, data && json.parse(data));
  });
  sdklib.setreceivecallback(global.setreceivecallback);
 },
 senddata: function(cmd, seq, data){
  data = json.stringify(data);
  sdklib.senddata(cmd, seq, data, data.replace(/[^\x00-\xff]/g, '000').length, 0);
 },
 releaseconnection: function(){
  sdklib.releaseconnection();
 },
 setoption: function (option) {
  sdklib.setoption(
  option.appkey,
  option.tk,
  option.lc,
  option.rm
  );
 }
 } 
} catch (error) {
 log.info(error);
}

第一步:通过ffi注册dll接口

const sdklib = ffi.library(libpath, {
 'createconnection': ['bool', []],
 'senddata': ['bool', ['int', 'int', 'string', 'int']],
 'releaseconnection': ['void', []],
 'setoption': ['void', ['string', 'string', 'int', 'int']],
 'setreceivecallback': ['void', ['pointer']]
 }); 

ffi.library方法,第一个参数传入dll路径,第二参数json对象配置相关接口。

key对应dll头文件中输出的接口,例如c_export bool _cdecl createconnection();

value array配置参数类型,array[0]注册接口函数返回值类型,array[1]注册接口函数传入形参类型。

1、基础参数类型bool, char, short, int, long等。

2、指针类型,需要引入ref模块,如下:

var ref = require('ref');
var intpointer = ref.reftype('char');
var doublepointer = ref.reftype('short');
var charpointer = ref.reftype('int');
var stringpointer = ref.reftype('long');
var boolpointer = ref.reftype('bool');

3、回调函数指针pointer,可以通过ffi.callback创建,如下:

global.setreceivecallback = ffi.callback('void', ['int', 'int', 'string'], function(cmd, seq, data){
 cb && cb(cmd, seq, data && json.parse(data));
 });
sdklib.setreceivecallback(global.setreceivecallback);

回调函数参数类型配置与dll接口参数类型配置相同,这里就不多说。

这里需要注意一点,回调函数可能会被javascript垃圾自动回收机制回收,所以我这里是把回调函数挂载到全局对象global上。

第二步:接口调用

通过ffi.library(libpath, {...})注册接口,可以直通过返回的sdklib对象调用对接的接口。例如:

var bool = sdklib.createconnection();
console.log(bool); // true or false;
var cmd = 0, seq = 0, data = {...};
var datastr = json.stringify(data);
// javascript中文字符长度在c++中长度计算要*3
sdklib.senddata(cmd, seq, data, data.replace(/[^\x00-\xff]/g, '000').length);
global.setreceivecallback = ffi.callback('void', ['int', 'int', 'string'], function(cmd, seq, data){
 cb(cmd, seq, data && json.parse(data));
});
sdklib.setreceivecallback(global.setreceivecallback);

补充:下面看下nodejs通过ffi调用dll

第一步建立一个dll, 提供方法如下

int winapi cam_open(char *pin, char* pout);

第二步安装ffi (前提已安装python2.x环境)

npm install --save ffi

第三步创建测试文件

var ffi = require("ffi")
var dll = ffi.library('facerecognition.dll', {
  'cam_open' : ['int', ['string', 'string']]
});
var result = dll.cam_open("", "");
console.log(result)

参考资料


总结

以上所述是小编给大家介绍的nodejs调用dll模块的方法,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网