JNA 直接调用不适用于参数 Structure[]

发布于 2024-12-20 13:39:50 字数 907 浏览 2 评论 0原文

我有一个 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[]);
}

但是使用直接调用,我遇到 IllegalArgumentExceptionclass [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 技术交流群。

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

发布评论

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

评论(2

偏闹i 2024-12-27 13:39:50

转换很简单,只需对结构体数组的第一个元素调用 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).

笑梦风尘 2024-12-27 13:39:50

如果我使用不同的方法签名:

boolean doSomething(Pointer results);

它确实有效。但随后我必须自己将 Result[] 转换为指针。

If I use a different method signature:

boolean doSomething(Pointer results);

it does work. But then I have to convert from Result[] to a Pointer my self.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文