指针在Java中的功能
我有C ++写的动态链接库(DLL)。在我的dll中,我具有这样的函数:
void set_event_callback(EVENT_CALLBACK callback_fcn);
其中event_callback
是这样的函数指针:
void (*EVENT_CALLBACK)(int event_type, byte param1, int param2);
我在其他语言中使用我的dll。例如,在C#i中调用set_event_callback
这样:
public delegate void EVENT_CALLBACK(int event_type, byte param1, int param2);
EVENT_CALLBACK HandleEventsDelegate = null;
HandleEventsDelegate = new EVENT_CALLBACK(HandleEvents);
set_event_callback(HandleEventsDelegate);
其中handlevents
是这样的函数:
protected void HandleANPREvents(int event_type, byte param1, int param2){ ... }
现在,我想在Java中使用我的DLL。我正在使用JNA在我的Java项目中调用本机代码。我找不到Java的功能指针。
如何在Java中调用set_event_callback
并将功能传递给我的DLL?
I have a dynamic link library (DLL) written in c++. In my DLL I have a function like this:
void set_event_callback(EVENT_CALLBACK callback_fcn);
where EVENT_CALLBACK
is a function pointer like this:
void (*EVENT_CALLBACK)(int event_type, byte param1, int param2);
I use my DLL in other languages. For example, in C# I call set_event_callback
like this:
public delegate void EVENT_CALLBACK(int event_type, byte param1, int param2);
EVENT_CALLBACK HandleEventsDelegate = null;
HandleEventsDelegate = new EVENT_CALLBACK(HandleEvents);
set_event_callback(HandleEventsDelegate);
where HandleEvents
is a function like this:
protected void HandleANPREvents(int event_type, byte param1, int param2){ ... }
Now, I want to use my DLL in Java. I'm using JNA to call native code in my java project. I couldn't find function pointers in Java.
How can I call set_event_callback
in Java and pass a function to my DLL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如评论使用单个方法匹配回调的功能接口。它可以看起来像这样:
然后您可以在代码中参考
set_event_callback()
。As the comment by @Mark-Rotteveel states, you should define a functional interface with a single method matching the callback. It could look something like this:
You can then refer to
set_event_callback()
in your code.