Ctypes - how to cast a function pointer and then call it?

Disclaimer: I am a complete beginner in ctypes and I would be very interested if you could share your dune file showing how you use the ctypes stanza!

Could you show how void_function is defined in the type description module? I would suggest to use the following definitions:

let void_function = static_funptr (void @-> returning void)

let adder = static_funptr (int @-> int @-> returning int)

you can then coerce load's result between the two types:

let add = coerce Types.void_function Types.adder (load ())

and call add by coercing it to Foreign.funptr:

let () =
  Format.eprintf "%d@."
    (coerce Types.adder (Foreign.funptr (int @-> int @-> returning int)) add 1 2)
2 Likes