我有一个 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[]);
}
但是通过直接调用,我遇到了一个IllegalArgumentException 说class [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/