Converting a dart String to a pointer

1,757 views
Skip to first unread message

Chris Norman

unread,
Apr 6, 2021, 10:37:16 AM4/6/21
to mi...@dartlang.org
Hi all,
I'm currently writing bindings for a library. The header file contains the following definition:

SYZ_CAPI syz_ErrorCode syz_createBufferFromStream(syz_Handle *outconst char *protocolconst char *pathconst char *options);

I've run the header file through ffigen, and is working well so far.

The resulting dart has the signature:

  int syz_createBufferFromStream(
    ffi.Pointer<ffi.Uint64out,
    ffi.Pointer<ffi.Int8protocol,
    ffi.Pointer<ffi.Int8path,
    ffi.Pointer<ffi.Int8options,
  ) {
    return _syz_createBufferFromStream(
      out,
      protocol,
      path,
      options,
    );
  }

First of all, is this right? I don't know enough C or C++ to know the difference, but the C++ method (the first code example) wants protocol, path and options given as const char * but the dart version is expecting Pointer<Int8>. Assuming that dart and ffigen know more about C++ than I do, how do I convert a normal string to this pointer type?
I thought the StringUtf8Pointer extension would help, but that returns a Pointer<Utf8>.

Any help would be greatly appreciated. Thanks in advance.

Take care,

Chris Norman

Daco Harkes

unread,
Apr 6, 2021, 10:42:44 AM4/6/21
to mi...@dartlang.org
Hi Chris,

package:ffi Pointer<Utf8> is the answer indeed. You can use .cast<Utf8>() and .cast<Int8>() on Pointers to cast between different types of pointers. An example of these casts can be seen here [1].

Also see the ffigen documentation [2], and feel free to ask questions there or make suggestions. Prerak is very helpful!


Kind regards,

 •  Daco Harkes
 •  Software Engineer
 •  dacoh...@google.com 
 •  🔵 Blue Dot Ally


--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CANjhqb86GcDiVn0XjUQbQ44XcRtcatAD-Kw7Gb4mVh%3De9nviAA%40mail.gmail.com.

Chris Norman

unread,
Apr 6, 2021, 11:46:42 AM4/6/21
to mi...@dartlang.org
That's great, thank you.

What threw me was the availability of dart:ffi, and the package of the same name.

Absolutely blew my tiny mind!

Take care,

Chris Norman



Reply all
Reply to author
Forward
0 new messages