How to pass a list of strings to C In dart ffi

894

I was able to do it by making a pointer to pointer to char

like this

/// Define the type like this
typedef SaveFileDialogD = Pointer<Utf8> Function(
 
  Pointer<Pointer<Utf8>> listOfStrings,

);

Look up the function in the dynamic library


SaveFileDialogD saveFileDialog = dylib
    .lookupFunction<SaveFileDialogC, SaveFileDialogD>('dylib');

make a function to convert Dart list of string to something more C friendly

/// Don't forget to run malloc.free with result!
Pointer<Pointer<Utf8>> strListToPointer(List<String> strings) {
  List<Pointer<Utf8>> utf8PointerList =
      strings.map((str) => str.toNativeUtf8()).toList();

  final Pointer<Pointer<Utf8>> pointerPointer =
      malloc.allocate(utf8PointerList.length);

  strings.asMap().forEach((index, utf) {
    pointerPointer[index] = utf8PointerList[index];
  });

  return pointerPointer;
}

Finally call the C function like this

Pointer<Utf8> result;
    final strListPointer = strListToPointer(["one", "two", "three"]);

    result = saveFileDialog(
       strListPointer;
    );
Share:
894
xerotolerant
Author by

xerotolerant

I'm the Technical Director at Raiora. I learn because I must.

Updated on December 31, 2022

Comments

  • xerotolerant
    xerotolerant over 1 year

    I'm learning how to use dart ffi.

    I don't have much C experience or knowledge.

    I'm trying to call a function from a C library(tiny file dialogs).

    I can call other functions however I can't call this one

    char * tinyfd_saveFileDialog(
            char const * aTitle , /* NULL or "" */
            char const * aDefaultPathAndFile , /* NULL or "" */
            int aNumOfFilterPatterns , /* 0 */
            char const * const * aFilterPatterns , /* NULL or {"*.jpg","*.png"} */
            char const * aSingleFilterDescription ) /* NULL or "image files" */
    {
    

    the main issue is that I don't understand what

    char const* const * a FilterProperties, / *Null or {"*.jpg", "*.png"} */

    means.

    As far as I was able to get from this issue the C function is asking for an array of strings. The arg aNumberOfFilterPatterns defines the length of the array.

    If that's the case, how can I prepare the array in dart to pass it to C?

  • roman
    roman almost 2 years
    When using utf8PointerList.length in malloc.allocate() my app is crashing with malloc(): invalid size (unsorted). The docs are saying that allocate() expects byteCount of bytes as first argument. Is utf8PointerList.length the right size to allocate?
  • roman
    roman almost 2 years
    There's also a sizeOf() function in dart::ffi. I changed the allocate() call to the following - malloc.allocate(sizeOf<Pointer<Utf8>>() * utf8PointerList.length);. Now the issue seems to be resolved.
  • xerotolerant
    xerotolerant almost 2 years
    Hey, I'm not sure sorry. I never ended up using malloc as part of my program.
  • roman
    roman almost 2 years
    And what did you use instead of malloc?
  • xerotolerant
    xerotolerant almost 2 years
    I never had reason to use malloc. My understanding of malloc is that is for allocating and deallocating memory. Dart is a memory managed language so you'll never really need to use malloc normally. FFI is sort of a bridge that you use to access C libraries. I never had to use any C specific techniques. Just use native dart objects. Use FFI specific objects just to communicate with C libraries. if you need more help we could chat so I can better understand your problem.
  • roman
    roman almost 2 years
    I'm also calling a C function from Dart(Flutter). The function in C is expecting an array of char* as parameter (similar to the standard int main(int argc, char** argv). I think your answer is already showing most of the steps on how to do it. The only thing that's missing is allocating correct size using sizeOf and cleaning the allocated pointers once you're done with them. Thank you for your help.
  • xerotolerant
    xerotolerant almost 2 years
    try this. The Dart FFI util handles that sizeof bit I think.