Параметр OUT node-ffi с типом Struct Pointer не может получить обратно значения из вызова DLL - PullRequest
1 голос
/ 07 марта 2019

C Структура данных:

typedef struct dpfpdd_dev_info {
        unsigned int         size;
        char                 name[MAX_DEVICE_NAME_LENGTH];
        DPFPDD_HW_DESCR      descr;
        DPFPDD_HW_ID         id;
        DPFPDD_HW_VERSION    ver;
        DPFPDD_HW_MODALITY   modality;
        DPFPDD_HW_TECHNOLOGY technology;
} DPFPDD_DEV_INFO;

typedef struct dpfpdd_hw_descr {
        char vendor_name[MAX_STR_LENGTH];
        char product_name[MAX_STR_LENGTH];
        char serial_num[MAX_STR_LENGTH];
} DPFPDD_HW_DESCR;

typedef struct dpfpdd_hw_id {
        unsigned short  vendor_id;
        unsigned short  product_id;
} DPFPDD_HW_ID;

typedef struct dpfpdd_hw_version {
        DPFPDD_VER_INFO hw_ver;
        DPFPDD_VER_INFO fw_ver;
        unsigned short bcd_rev;
} DPFPDD_HW_VERSION;

typedef struct dpfpdd_ver_info {
        int major;
        int minor;
        int maintenance;
} DPFPDD_VER_INFO;

typedef unsigned int DPFPDD_HW_MODALITY;
typedef unsigned int DPFPDD_HW_TECHNOLOGY;

Эмулированный JS-код вышеуказанного кода C:

const ref = require('ref');
const StructType = require('ref-struct');
const FixedBuffer = require('./FixedBuffer'); // from https://gist.github.com/TooTallNate/80ac2d94b950216a2705

const DPFPDD_DEV_INFO = StructType({
  size: ref.types.uint,
  name: FixedBuffer(MAX_DEVICE_NAME_LENGTH, 'utf-8'),
  descr: DPFPDD_HW_DESCR,
  id: DPFPDD_HW_ID,
  ver: DPFPDD_HW_VERSION,
  modality: DPFPDD_HW_MODALITY,
  technology: DPFPDD_HW_TECHNOLOGY
});

const DPFPDD_HW_DESCR = StructType({
  vendor_name: FixedBuffer(MAX_STR_LENGTH, 'utf-8'),
  product_name: FixedBuffer(MAX_STR_LENGTH, 'utf-8'),
  serial_num: FixedBuffer(MAX_STR_LENGTH, 'utf-8')
});

const DPFPDD_HW_ID = StructType({
  vendor_id: ref.types.ushort,
  product_id: ref.types.ushort
});

const DPFPDD_HW_VERSION = StructType({
  hw_ver: DPFPDD_VER_INFO,
  fw_ver: DPFPDD_VER_INFO,
  bcd_rev: ref.types.ushort
});

const DPFPDD_VER_INFO = StructType({
  major: ref.types.int,
  minor: ref.types.int,
  maintenance: ref.types.int
});

const DPFPDD_HW_MODALITY = ref.types.uint;
const DPFPDD_HW_TECHNOLOGY = ref.types.uint;

API DLL:

int DPAPICALL dpfpdd_query_devices (unsigned int *dev_cnt, DPFPDD_DEV_INFO *dev_infos)

код node-ffiAPI DLL выше:

const lib = ffi.Library('theDLL'), {
  'dpfpdd_init': [ 'int', [] ],
  'dpfpdd_query_devices': [ 'int', [ ref.refType('uint'), ref.refType(DPFPDD_DEV_INFO) ] ]
});

вызов функции ffi в узле:

lib.dpfpdd_init();
let deviceEntries = ref.alloc('uint');
let deviceInfo = ref.alloc(DPFPDD_DEV_INFO);
lib.dpfpdd_query_devices(deviceEntries, deviceInfo);
console.log(deviceInfo.deref());

Журнал консоли будет возвращать что-то вроде этого:

{ size: 0,
  name: <Buffer@0x019572AC 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... >,
  descr: { vendor_name: <Buffer@0x019576AC 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... >,
  product_name: <Buffer@0x0195772C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... >,
  serial_num: <Buffer@0x019577AC 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... >,
  'ref.buffer': <Buffer@0x019576AC 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... > },
  id: { vendor_id: 0,
  product_id: 0,
  'ref.buffer': <Buffer@0x0195782C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00> },
  ver: { hw_ver: { major: 0,
  minor: 0,
  maintenance: 0,
  'ref.buffer': <Buffer@0x01957830 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00> },
  fw_ver: { major: 0,
  minor: 0,
  maintenance: 0,
  'ref.buffer': <Buffer@0x0195783C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00> },
  bcd_rev: 0,
  'ref.buffer': <Buffer@0x01957830 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00> },
  modality: 0,
  technology: 0,
  'ref.buffer': <Buffer@0x019572A8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... > }

Из всехжурналы типа буфера, похоже, я вообще не получаю никаких значений из моего параметра OUT StructType.Я понятия не имею, почему это происходит.Я очень новичок в использовании node-ffi и node-ref.Мне удалось связаться с другим USB-устройством, но структуры данных намного проще, без вложенных структур и типов массивов.Но этот я не могу заставить работать.Кто-нибудь просветит меня, что с чем я делаю не так?

...