JNA 直接调用不适用于参数 Structure[]
我有一个 C++ 函数:
struct Result {
//...
};
bool doSomething(Result[]);
如果我使用以下 JNA 绑定,则函数调用工作正常:
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.结果;不是受支持的参数类型(在 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[]。
I have a C++ function:
struct Result {
//...
};
bool doSomething(Result[]);
If I use the following JNA binding, the function call works fine:
public class Result extends Structure {
//...
}
public interface CPPLibrary extends Library {
public static final CPPLibrary INSTANCE = (CPPLibrary)Native.loadLibrary("dllname");
boolean doSomething(Result[]);
}
But with direct call, I hit an IllegalArgumentException saying class [Lcom.usta.Result; is not a supported argument type (in method calcPV01 in class com.usta.CPPLibrary). My JNA code for the direct call-mapping:
public class CPPLibrary implements Library {
Native.register("dllname");
public static native boolean doSomething(Result[]);
}
I can see in com.sun.jna.Function#convertArgument() explicitly handles Structure[] but com.sun.jna.Native#getConversion(), which is used by direct call-mapping, does not handle Structure[].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
转换很简单,只需对结构体数组的第一个元素调用 Structure.getPointer() 即可(假设您首先从 Structure.toArray 获取了该数组)。
当使用直接映射时,你实际上会更好;当传递非基元、非指针类型时,JNI 层必须回调到 VM 以派生适当的本机数据。
请随意提出问题以支持直接映射中的 Structure[] 参数。应该支持这一点(JNA 文档指出,不支持 Pointer/String/WString/NativeMapped 数组)。
The conversion is trivial, just call Structure.getPointer() on the first element of your structure array (assuming that you got the array from Structure.toArray in the first place).
You're actually better off with that when using direct mapping; when passing non-primitive, non-pointer types the JNI layer has to call back into the VM to derive the appropriate native data.
Feel free to file an issue for support of Structure[] arguments in direct mappings. That should be supported (JNA documentation notes that arrays of Pointer/String/WString/NativeMapped are not supported).
如果我使用不同的方法签名:
它确实有效。但随后我必须自己将 Result[] 转换为指针。
If I use a different method signature:
it does work. But then I have to convert from Result[] to a Pointer my self.