java - JNA 直接调用不适用于参数结构[]

标签 java native jna

我有一个 C++ 函数:

struct Result {
    //...
};
bool doSomething(Result[]);

如果我使用以下 JNA 绑定(bind),函数调用工作正常:

public class Result extends Structure {
    //...
}

public interface CPPLibrary extends Library {
    public static final CPPLibrary INSTANCE = (CPPLibrary)Native.loadLibrary("dllname");
    boolean doSomething(Result[]);
}

但是通过直接调用,我遇到了一个IllegalArgumentExceptionclass [Lcom.usta.Result;不是受支持的参数类型(在类 com.usta.CPPLibrary 的方法 calcPV01 中)。我的直接调用映射的 JNA 代码:

public class CPPLibrary implements Library {
    Native.register("dllname");
    public static native boolean doSomething(Result[]);
}

我可以在 com.sun.jna.Function#convertArgument() 中看到显式处理 Structure[]com.sun.jna.Native#getConversion (),由直接调用映射使用,不处理 Structure[]

最佳答案

转换很简单,只需在结构数组的第一个元素上调用 Structure.getPointer()(假设您首先从 Structure.toArray 获得数组)。

使用直接映射时,您实际上会更好;当传递非原始、非指针类型时,JNI 层必须回调 VM 以派生适当的 native 数据。

请随意提交问题以支持直接映射中的 Structure[] 参数。这应该得到支持(JNA 文档指出支持 Pointer/String/WString/NativeMapped 数组)。

关于java - JNA 直接调用不适用于参数结构[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8439563/

相关文章:

java - 给定一个字符串, "xyz"是否出现在字符串的中间?

没有 .NET Framework 的 C#

http - 有没有可以让我使用 native 编辑器编辑 Web 文件的程序?

java - 共享库分配的 JNA 空闲内存

java - 如何移动(或使用)鼠标

java - Wicket:用自定义内容动态填充单元格

java - 处理 Spring Boot 资源服务器中的安全异常

java - 为什么我的代码出队无法正常工作?

java - "AWT widgets written in Java which delegated to peer classes that were written in C"是什么意思?

c - Security.h 中结构的 macOS 文档