0

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.

jgm
  • 1,230
  • 1
  • 19
  • 39

1 Answers1

1

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();
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
  • it was a typo that the function is not getting called. – jgm Jul 09 '21 at 22:37
  • @jgm There are other problems with your code that you have not addressed and are using bang operators and other null-safe oddities left and right for some reason. You really need to be careful about typos because they make your code unreproducible. – Christopher Moore Jul 09 '21 at 22:44
  • your solution to the function definition solved the issue. Can you also tell me what should i declare in dart, if i want to return a string. my cpp function is in that case like this static const char* getStringText() { return std::string("Hello world").c_str();; } functions[1] = (char **) getStringText; – jgm Jul 10 '21 at 01:20
  • @jgm You'll want a `Pointer`. The links in [this](https://stackoverflow.com/a/60273020/13250142) stack overflow answer will be of some help. – Christopher Moore Jul 10 '21 at 01:27
  • both your solutions worked. One final query, how would i return from a native function in c++ which has a return type of `std::vector ` and get the result in dart ? Is it better to convert to to string ? – jgm Jul 10 '21 at 21:51
  • @jgm I only know C, not c++. Is that return type just an array of some kind? If so, you're going to have to figure out a way to return a pointer and handle everything in a c-like manner. – Christopher Moore Jul 10 '21 at 21:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/234730/discussion-between-jgm-and-christopher-moore). – jgm Jul 10 '21 at 22:07