Search code examples
jna

Turn a Callback into a Pointer in JNA


I must be missing something. Given an instance of a class that implements com.sun.jna.Callback, how do I get a function pointer address as a Pointer, so that I can pass it to a function that takes a void* parameter (I’m using Pointer).

Specifically, I want to create a CFNumber (aka NSNumber*) using CFNumberCreate(Pointer, int, Pointer) with the address of the function, so that I can call SetSpeechProperty to register a SpeechDoneProcPtr.


Solution

  • You don't. You pass the callback instance and let JNA handle the pointer conversions. Make sure you keep a strong reference to the callback object so it doesn't get GC'd.

    If you need to, make another function mapping that uses your callback type instead of Pointer.

    You can get the native pointer value for a callback instance, but there are very few reasons why you'd ever need to.