指针在Java中的功能

发布于 2025-02-01 08:14:18 字数 1003 浏览 3 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2025-02-08 08:14:18

评论使用单个方法匹配回调的功能接口。它可以看起来像这样:

// Extend StdCallLibrary if library is __stdcall
public interface YourDLL extends Library {
    YourDLL INSTANCE = // insert library loading code here

    // Define the callback
    // Extend StdCallCallback if library is __stdcall
    interface HandleANPREvents extends Callback {
        // the function name doesn't matter
        void invoke(int event_type, byte param1, int param2);
    }

    // Define the function using the callback
    void set_event_callback(HandleANPREvents callback_fcn);
}

然后您可以在代码中参考set_event_callback()

public static void main(String[] args) {
    HandleANPREvents fn = new HandleANPREvents() {
        @Override
        void invoke(int event_type, byte param1, int param2) {
            // implementation here
        }
    };
    YourDLL.INSTANCE.set_event_callback(fn);
}

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:

// Extend StdCallLibrary if library is __stdcall
public interface YourDLL extends Library {
    YourDLL INSTANCE = // insert library loading code here

    // Define the callback
    // Extend StdCallCallback if library is __stdcall
    interface HandleANPREvents extends Callback {
        // the function name doesn't matter
        void invoke(int event_type, byte param1, int param2);
    }

    // Define the function using the callback
    void set_event_callback(HandleANPREvents callback_fcn);
}

You can then refer to set_event_callback() in your code.

public static void main(String[] args) {
    HandleANPREvents fn = new HandleANPREvents() {
        @Override
        void invoke(int event_type, byte param1, int param2) {
            // implementation here
        }
    };
    YourDLL.INSTANCE.set_event_callback(fn);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文