I'm working on implementing a callback using node-ffi
with the following signature:
const callback = ffi.Callback('void', ['int', 'void *'], (eMsg, pData) => {
console.log(pData);
console.log(`Buffer deref: ${ref.get(pData)}`);
});
The underlying C library calls this with a void *
value for pData
. About half of the callbacks actually return an int *
value and, in the other half, pData
is more complex data (i.e. Struct
). The ref.get(pData)
ends up being null
. If I change the Callback
definition to use int *
instead of void *
, ref.get(pData)
returns the correct value. What I can't figure out is why the void *
ends up being a Buffer with a size of 0 that does not dereference properly (even when pData
is an int *
).
I feel like I'm missing something but can't, for the life of me, see what that would be. Can anyone help?
I had a similar problem, and what I did was set the length
property of the returned Buffer to the size of the thing that will be returned.
Because it is void *
type, node-ffi basically does not know how long of Buffer will be returned to it.
so in this case, pData.length = <size of structure>
should fix it.