.h
文件中有一个结构:
struct usb_relay_device_info
{
char *serial_number;
char *device_path;
intptr_t /*enum usb_relay_device_type*/ type;
struct usb_relay_device_info* next;
};
和typedef
:
typedef struct usb_relay_device_info *pusb_relay_device_info_t;
和我要调用的函数标题:
pusb_relay_device_info_t USBRL_API usb_relay_device_enumerate(void);
我试图用node-ffi
调用此功能,所以我做了:
var ref = require('ref');
var ffi = require('ffi');
var Struct = require('ref-struct');
var charPtr = ref.refType(ref.types.char);
var intPtr = ref.refType(ref.types.int);
var deviceInfo = Struct()
var deviceInfoPtr = ref.refType(deviceInfo);
deviceInfo.defineProperty('serial_number', charPtr);
deviceInfo.defineProperty('device_path', charPtr);
deviceInfo.defineProperty('type', intPtr);
deviceInfo.defineProperty('next', deviceInfoPtr);
var pusb_relay_device_info_t = ref.refType(deviceInfo);
var usbLib = ffi.Library('USB_RELAY_DEVICE_64.dll', {
'usb_relay_device_enumerate': [pusb_relay_device_info_t, []]
});
调用函数usbLib.usb_relay_device_enumerate()
会返回缓冲区<Buffer@0x000002AB786E2100 28 21 6e 78 ab 02 00 00 40 24 bd 99 fb 7f 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00>
,但我不知道如何将此结果转换为javascript对象,并阅读serial_number
或device_path
属性