java - 带有附加参数的 JNA 回调函数

标签 java c wrapper jna

我在解决 JNA 问题时遇到了问题。我正在尝试调用一个函数,该函数将指向函数和 const char* 的指针作为参数。我的 C 代码如下所示:

typedef void (*example_ptr)(const char*);

void exampleMethod(const char* value)
{
    printf("This is the string: %s\n", value);
}

void example_triggerCallback(const example_ptr func, const char* str) {
    printf("provided str: %s", str);
    func(str);
}

为了实现这一点,我用 Java 编写了这样的 JNA 包装器

public class Main {

    public interface TestLibrary extends Library {

        TestLibrary INSTANCE = (TestLibrary)
                Native.loadLibrary("libtest",
                        TestLibrary.class);

        interface ExampleCallback extends Callback {
            void invoke(String str);
        }

        class ExampleCallbackImpl implements ExampleCallback {
            public void invoke(String str) {
                System.out.println("example: " + str);
            }
        }

        void example_triggerCallback(ExampleCallback callback, String str);
    }

    public static void main(String[] args) {

        TestLibrary testLibrary = TestLibrary.INSTANCE;
        final TestLibrary.ExampleCallbackImpl callback = new TestLibrary.ExampleCallbackImpl();
        testLibrary.example_triggerCallback(callback, "testiddy test test");
    }
}

我面临的问题是,C 代码中的 example_triggerCallback 中的 printf 实际上正在被调用,我在 Java 控制台上得到输出,但我真正的情况是在这里尝试实现的是,我想从 Java 端传递来自 C 的 exampleMethod 指针,以便它将打印传递的 String。现在 func(str) 似乎被忽略了。我在这里缺少什么?

最佳答案

基于某事found in JNA documentation :

typedef void (*ExampleCallback)(const char*);

void exampleMethod(const char* value)
{
    printf("This is the string: %s\n", value);
}

void example_triggerCallback(const example_ptr func, const char* str) {
    printf("provided str: %s", str);
    func(str);
}
public interface CLibrary extends Library {
    // define an interface that wraps the callback code
    public interface ExampleCallbackInterface extends Callback {
        void invoke(String val);
    }

        // functions available in library (name must match)
    public void exampleMethod(String  value);
    public void example_triggerCallback(ExampleCallbackInterface callback);
}

// define an implementation of the callback interface
public static class CallbackExample implements Example22CallbackInterface {
    private CLibrary lib;

    public CallbackExample(CLibrary useLib) {
        lib = useLib;
    }

    @Override
    public void invoke(String val) {
        lib.exampleMethod(val);
    }
}

...
final CLibrary clib = (CLibrary)Native.loadLibrary("testlib", CLibrary.class);
...
// instantiate a callback wrapper instance
final CallbackExample callback = new CallbackExample(clib);

// pass the callback wrapper to the C library
clib.example_triggerCallback(callback);

由于我在其他互联网位置回答了这个问题,我知道它对提问者有用。

关于java - 带有附加参数的 JNA 回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52219921/

相关文章:

java - 创建目录以存储文件时出错

java - 为什么我不能导入静态 java.lang.System.out.println?

c - 十六进制编码数据的异或字符串结果字符串终止符

c++ - 相同的程序可以在 C 中运行,但不能在 C++ 中运行(使用 linux 系统调用)

terminology - "wrapper function"的反义词是什么?

java - TensorFlow 标签号与轴上的形状不匹配

java - 如果不满足启动要求,终止计划的良好做法是什么

c - STM32F4带RS485接口(interface)

C++ SWIG 生成的代码取决于 Tcl

c++ - 如何从类中包装的成员访问包装类的成员