I want to create binding to this C function in dart, this C function accepts a function pointer called progress_monitor
:
FFI_PLUGIN_EXPORT void* magickSetProgressMonitor(..., const void* progress_monitor,...);
where progress_monitor
is a function pointer:
typedef bool(*MagickProgressMonitor)(const char *,const long long,
const long long,void *);
In dart, I tried creating a function pointer like this:
typedef MagickProgressMonitor = bool Function(String text, int offset, int size, List<int>? clientData);
extension _MagickProgressMonitorExtension on MagickProgressMonitor {
/// Creates a native function pointer from this method.
Pointer<Void> toNativeFunctionPointer() {
return Pointer.fromFunction<Bool Function(Pointer<Char>, LongLong, LongLong, Pointer<Void>)>(this, false).cast();
}
}
But I get this error:
The type 'bool Function(String, int, int, List<int>?)' must be a subtype of 'Bool Function(Pointer<Char>, LongLong, LongLong, Pointer<Void>)' for 'fromFunction'.
I have 2 questions:
- what types should I use with
fromFunction<???>
to remove the error? - the last parameter to the C function is a void* pointer which can be anything, right now I am trying to represent it as
List<int>
in dart andunsigned char
array in C, but this doesn't seem correct, is there a way to handle this more correctly?
Update:
I intend to use the above code by exposing an api method by something like this:
void magickSetProgressMonitor(MagickProgressMonitor progressMonitor, [List<int>? clientData]) {
final Pointer<UnsignedChar> clientDataPtr = clientData?.toUnsignedCharArray() ?? nullptr;
final Pointer<Void> progressMonitorPtr = progressMonitor.toNativeFunctionPointer();
Pointer<Void> oldMonitorPtr =
_bindings.magickSetProgressMonitor(...,progressMonitorPtr,...);
...
}