Closed simon-p-r closed 7 years ago
Is it possible to show me a more complete sample code to demonstrate your problem? What C lib are you trying to invoke?
I have just done a stripped down version of api, the C api is like this
void create(struct Obj *obj) {
// value should be true
if (obj->traceMode) {
// do something
}
}
Struct for c api is defined like this
struct Obj {
bool traceMode;
}
It is defined and used in javascript like this
const Obj = Struct({
traceMode: ref.types.bool
});
const newObj = new Obj({
traceMode: ref.alloc('bool', 1) // have also tried ref.alloc(ref.types.bool, 1) as well
});
const structPtr = ref.refType(Obj);
const lib = ffi.Library(dllPath, {
'create': ['void', [structPtr]]
});
lib.create(newObj.ref());
Oh I see. You don't need to do the ref.alloc()
part. Just pass JS true
or false
in and ref.types.bool
handles the coercion to the C value:
const newObj = new Obj({
traceMode: true
});
What you were doing was passing in a pointer to a bool, which was an address in memory which would basically always be "true".
Ok I will try that, thanks for your swift answer!
I am testing value of bool inside c like this but it still doesn't recognise the ref past from javascript.
if (obj->traceMode) {
printf("Trace mode is on\n");
}
else {
prinf("Trace mode is off\n");
}
@simon-p-r I added a test case in https://github.com/node-ffi/node-ffi/commit/7a928f38b1f7520035cfb6607391b9d7c8acb739 which seems to be working as expected. Let me know if I'm missing anything.
Thanks @TooTallNate forgot about this, I think my problem was a boundary problem with the struct I was working with. I am a total c novice, thanks for this excellent module!
Hi
I am trying to use a bool type however the value in c code is not true or false, I have tried these 2 different ways
and
Any pointers would be appreciated!
Thanks