Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OUT parameter with type of Struct Pointer cannot receive back values from DLL call #35

Open
feutech-pgsantos opened this issue Mar 5, 2019 · 2 comments

Comments

@feutech-pgsantos
Copy link

feutech-pgsantos commented Mar 5, 2019

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?

@feutech-pgsantos feutech-pgsantos changed the title Struct Pointer passed as IN and OUT argument OUT parameter with type of Struct Pointer cannot receive back values from DLL call Mar 7, 2019
@Olmectron
Copy link

Did you find a solution after these 4 years?

@stevenlafl
Copy link

Same question. In my case I have an array of pointers. When passed in, I only get the value of the first array element. I cannot get a reference to any other values. After using reinterpret, despite loading with identical buffer data, will not deref properly.. where as leaving it alone does, so I can't offset anything. I can manually offset values, with but the data appear to be in inconsistent locations.

const token_str = tokenPtr.readCString(0); // always works

const logprob_offset = ref.types.CString.size;
const logprob = tokenPtr.readFloatLE(logprob_offset); // complete garbage values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants