First of all, apologies if the below question shows up twice. I posted this same question earlier today, but it doesn't seem to show up. I'm not sure if that's due to moderation, or if something went wrong on my end.
I'm having big trouble creating FFI bindings for the following C function:
typedef Handle void;
int getDeviceHandle(char *deviceName, Handle *handle);
which is called in C as follows:
Handle handle;
int res = getDeviceHandle("myDevice", &handle);
I'm trying in Dart with variations of:
typedef Handle = Void;
late int Function(
List<Utf8> deviceName,
Pointer<Void> handle
) getDeviceHandle;
typedef getDeviceHandle_native_t = Int32 Function(
List<Utf8> deviceName,
Pointer<Void> handle
);
// "library" is the opened library
getDeviceHandle = library
.lookup<NativeFunction<getDeviceHandle_native_t>>('getDeviceHandle')
.asFunction();
// Now to call getDeviceHandle()
Pointer<Handle> handlePtr = calloc(); // fails at runtime here
String deviceName = "myDevice";
int res = getDeviceHandle(
deviceName.toNativeUtf8(),
handlePtr
);
handle = handlePtr.value; // don't know how to extract the actual Handle object.
// There is no 'value' property (or 'ref')
Can anybody help me to get this to work? I've searched a lot through packages on
pub.dev that use dart:ffi in the hope of finding a similar situation, but haven't been able to find anything that worked for me.