I'd like to bind to a C function abc
whose signature is:
int abc(void (*)(int))
I.e. it accepts a pointer to a function. This callback accepts an int
and has void
return type.
What's the right incantation in Racket?
Here is an example for a similar binding: let's say that we want to bind a function callTwice
with the signature:
int callTwice(int (*)(int));
and with the silly C implementation:
int callTwice(int (*f)(int)) {
return f(f(42));
}
Here's what the Racket FFI binding looks like:
(define call-twice (get-ffi-obj "callTwice" the-lib
(_fun (_fun _int -> _int) -> _int)))
where the-lib
is the FFI-bound library we get from ffi-lib
. Here, we see that the first argument to call-twice
is itself a (_fun _int -> _int)
, which is the function pointer type we want for my example.
That means that technically we could have written this:
(define _mycallback (_fun _int -> _int))
(define call-twice (get-ffi-obj "callTwice" the-lib
(_fun _mycallback -> _int)))
to make it easier to see that call-twice
takes in a callback as its only argument. For your case, you'll probably want (_fun _int -> _void)
for the value of your callback type.
The fully worked-out example can be found here: https://github.com/dyoo/ffi-tutorial/tree/master/ffi/tutorial/examples/call-twice. Run the pre-installer.rkt
in that directory to build the extension, and then test it out by running test-call-twice.rkt
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With