I'm using node-ffi to call a function that takes an out-param as a pointer-to-a-pointer-to-an-array-of-structs. Is there a way to use ref-struct and ref-array for me to access the array that I get out?
struct = require("ref-struct");
var rect_type = struct({
'x': 'int',
'y': 'int',
'width': 'int',
'height': 'int',
});
var rotation_type = struct({
'yaw': 'short',
'pitch': 'short',
'roll': 'short'
});
var face_type = struct({
'rect' : rect_type,
'rotation' : rotation_type,
'confidence' : 'double'
});
I'm able to get the first struct, out from the pointer after the function call but I'm unable to get the rest of the array:
var mylib = ffi.Library('lib/libN', {
'GetFaces' : [ 'int', [ 'pointer' ] ]
});
var pface_type = ref.refType(face_type);
var ppface = ref.alloc(pface_type);
result = mylib.GetFaces(ppface);
face = ppface.deref().deref();
console.log("X:" + face.rect.x + " Y:" + face.rect.y);
Is there a way to declare the parameter as an array of structs? I've tried this but it doesn't work:
var array = require("ref-array");
var face_array = array(face_type)
var p_face_array = ref.refType(face_array);
var ppface = ref.alloc(p_face_array);
result = mylib.GetFaces(ppface);
For reference I did eventually solve this without using ref-array.
The trick/hack is to know that in C, an 'array' is pretty much the same as a pointer to the first element. So in ffi, we simply pass a pointer to the first element, and be very careful to not overstep our bounds.