c native structure in flutter dart

103 views Asked by At

some help would be grateful here.

I am trying to import C-library to flutter app and I can create the dynamic library (.so) for android. I am new to flutter and I have coded with C 10years ago. I have used ffigen to create dart bindings but my calls to native C is not working as expected.
Code structure is like this: android/app/src/main/jni/include/header1.h android/app/src/main/jni/include/fold2/header2.h

"header1.h" : header file in C has function definitions

#include header2.h
testStruct_t *init_new(void);
typedef struct testStruct testStruct_t;
(and some bunch of get/set functions for each item in structure such as below)
int set_host(testStruct_t *self, const char *host);
const char *get_host(testStruct_t *self);

"header2.h" : header file in C has struct definition

struct testStruct {
    int port;
    char *host;
    char *username;
    char *password;
}

'lib.config.yaml': dart yaml to generate ffibidings from header1.h only

    output: 'lib/test_generated_bindings.dart'
    name: 'libtest'
    description: 'test libtest can be used for ffi gen'
    headers:
      entry-points:
        - "android/app/src/main/jni/include/header1.h"
    comments:
      style: any
      length: full

"lib_generate_bindings.h" :

class libtest {
  /// Holds the symbol lookup function.
  final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
      _lookup;

  /// The symbols are looked up in [dynamicLibrary].
  libtest(ffi.DynamicLibrary dynamicLibrary) : _lookup = dynamicLibrary.lookup;

  /// The symbols are looked up with [lookup].
  libtest.fromLookup(
      ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
          lookup)
      : _lookup = lookup;

  ffi.Pointer<testStruct_t> init_new() {
    return _init_new();
  }

  late final _init_newPtr =
      _lookup<ffi.NativeFunction<ffi.Pointer<testStruct_t> Function()>>('init_new');
  late final _init_new =
      _init_newPtr.asFunction<ffi.Pointer<testStruct_t> Function()>();
}

final class testStruct extends ffi.Opaque {}
  1. When I call the native functions in dart, I don't see the C library is operating correctly. One reason may be that I am not creating structure in dart side and link that memory to C? Do I really have to do this?
  2. When I use the get field method from structure of type Pointer I don't see correct values.
0

There are 0 answers