I am trying to get the return value from c++ function in dart.
My c++ code is something like this
static bool is_alive() {
return true;
}
From dart, I loaded the shared lib with this native code and I am trying to call that is_alive()
function.
typedef BooleanFunction = Pointer<Int8> Function();
Pointer<NativeFunction<BooleanFunction>> isAlive = (functionAddress).cast<NativeFunction<BooleanFunction>>();
Pointer<Int8> Function() isAliveFunc = isAlive.asFunction();
I called then the isAliveFunc
from dart and I want the result of that functions. I tried all these ways. None of these works.
Pointer<Int8> casted = isAliveFunc()!.cast();
Pointer<Int8>? fromPointer = isAliveFunc()!;
developer.log('value : $casted');
I get the result like this Pointer<Int8>: address=0x1
How can I fetch the value from this pointer in dart ? I expect to get a 1 or 0 as the result.
For some reason you never actually call your isAliveFunc
function.
Just call the function as you would any other:
Pointer<Int8> casted = isAliveFunc();
or
Pointer<Int8> casted = isAliveFunc.call();
You should also be paying attention to your Dart static analysis warnings and errors.
Your function definitions are also incorrect. is_alive
returns a bool
/Int8
, not a pointer.
Your BooleanFunction
typedef should be:
typedef BooleanFunction = Int8 Function();
int Function() isAliveFunc = isAlive.asFunction();
And the function call return should be assigned to an int
:
int casted = isAliveFunc();