node-ffi OUT parameter with type of Struct Pointer cannot receive back values from DLL call

908 Views Asked by At

C Data Structure:

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;

Emulated JS code of the C code above:

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;

DLL API:

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

node-ffi code of the DLL API above:

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

ffi function invocation in node:

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());

The console log would return something like this:

{ 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 ... > }

From all the Buffer type logs it seems like I'm receiving back no values at all from my OUT StructType parameter. I have no Idea why this is happening. I'm very new to using node-ffi and node-ref. I managed to get to communicate to another USB device but data structures are much simpler, no nested struct and array types. But this one I cannot get to work. Anyone enlighten me to what with what I'm doing wrong?

0

There are 0 best solutions below