如何初始化struct node-ffi结构

时间:2017-10-22 13:10:52

标签: node.js struct node-ffi

我在linux上有库。 那个库有结构。 该库的手册说必须初始化struct,如下所示:

LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
lUserID = NET_DVR_Login_V30("192.168.1.64", 8000, "admin", "12", &struDeviceInfo);

struDeviceInfo - 从设备输出数据,我必须阅读和使用。
我怎样才能在Nodejs中编写代码。 PS:我使用node-ffi模块,库初始化并连接到设备完成ok。

    var net = require('net');
    var events = require('events');
    var util = require('util');
    var request = require('request');
    var xml2js = require('xml2js');

    var ffi = require("ffi");
    var ref = require("ref");
    var ref_struct = require("ref-struct");    

var libHikSDK = ffi.Library('./libhcnetsdk.so', {
    'NET_DVR_Init': ['bool', []],
    'NET_DVR_SetConnectTime': ['bool', ['char', 'char']],
    'NET_DVR_SetReconnect': ['bool', ['char', 'bool']],
    'NET_DVR_Login_V30': ['char', ['string', 'int', 'string', 'string']],
    'NET_DVR_Cleanup': ['void', []],
    'NET_DVR_GetLastError': ['char', []],
    'NET_DVR_GetSDKVersion': ['char',[]],
    'NET_DVR_SetLogToFile': ['bool',['int', 'string']]
    });

libHikSDK.NET_DVR_Cleanup();
var init = libHikSDK.NET_DVR_Init();
var v = libHikSDK.NET_DVR_GetSDKVersion();
var s = libHikSDK.NET_DVR_SetConnectTime('200000', '1');
libHikSDK.NET_DVR_SetLogToFile(3, "/home/user");
var i = libHikSDK.NET_DVR_Login_V30("192.168.0.10", 8000, "admin", "12345");
var g = libHikSDK.NET_DVR_GetLastError();

1 个答案:

答案 0 :(得分:0)

您可以使用ref-struct定义NET_DVR_DEVICEINFO_V30结构,然后像这样调整函数定义:

const NetDvrDeviceInfoV30 = StructType({...});

const libHikSDK = ffi.Library('./libhcnetsdk.so', {
  ...
  'NET_DVR_Login_V30': ['char', ['string', 'int', 'string', 'string', NetDvrDeviceInfoV30]],
  ...
});

const netDvrDeviceInfoV30 = new NetDvrDeviceInfoV30();
const i = libHikSDK.NET_DVR_Login_V30("192.168.0.10", 8000, "admin", "12345", netDvrDeviceInfoV30);