在JNA中将回调变成指针 [英] Turn a Callback into a Pointer in JNA
问题描述
我一定很想念东西.给定实现com.sun.jna.Callback
的类的实例,如何获取函数指针地址作为Pointer
,以便可以将其传递给带有void*
参数的函数(我正在使用Pointer
).
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
).
具体来说,我想使用NSNumber*
) .html#//apple_ref/c/func/CFNumberCreate"rel =" nofollow> CFNumberCreate(Pointer, int, Pointer)
,带有函数的地址,这样我就可以调用 SpeechDoneProcPtr
.
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
.
推荐答案
您没有.您传递回调实例,并让JNA处理指针转换.确保对回调对象保持强烈的引用,这样它就不会被垃圾回收.
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.
如果需要,进行另一个使用您的回调类型而不是Pointer
的函数映射.
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.
这篇关于在JNA中将回调变成指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!