通过参考作为JNA中的参数传递结构

发布于 2025-02-03 09:06:51 字数 728 浏览 4 评论 0 原文

我有C ++写的动态链接库(DLL)。在我的dll中,我具有这样的函数:

void anpr_set_params(byte instance, SLPRParams* params);

其中 slprams 是包含字节,int,float和它们的数组的结构。

现在,我想在Java中使用DLL。我正在使用JNA在我的Java项目中调用本机代码。我尝试从

void anpr_set_params(byte instance, long slpr_params);

但是我在C ++中获得的地址与我在Java中发送的地址不同。

然后我寻找另一种解决方案,然后阅读this JNA FAQ条目

我测试了所有类型,但是每次收到运行时错误时。

I have a dynamic link library (DLL) written in c++. In my DLL I have a function like this:

void anpr_set_params(byte instance, SLPRParams* params);

Where SLPRParams is a Structure containing byte, int, float and array of them.

Now, I want to use my DLL in Java. I'm using JNA to call native code in my java project. I tried get help from this site to get the address of my structure in java and pass it to my dll, and I defined my signature function in Java like this:

void anpr_set_params(byte instance, long slpr_params);

But the address i get in c++ is different from the address i sent in Java.

Then I looked for another solution and read the this JNA FAQ entry.

I tested all types but every time I get a runtime error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

墨洒年华 2025-02-10 09:06:51

您可以直接传递结构作为参数。

jna结构和工会

当功能需要指向结构的指针时,应使用Java结构。

另外,结构 说:

用作函数参数或返回值时,此类对应于 struct*。当用作另一个结构中的字段时,它对应于 struct 。标记接口 struction.byReference struction.byvalue 可用于更改默认行为。

将指针传递到诸如 slprams* params 之类的结构是DLL期望结构的最常见方法,而 byReference 类型是默认行为。只有在内联的完整结构中,您才需要在其上使用 byvalue 标签。

另一个结构内的嵌套结构以相反的方式工作。默认情况下,它是 byvalue ,如果将其称为指针,则需要 byReference 标签。

中的示例(您也链接)可能会更有意义。相关示例是:

void myfunc(simplestruct* data); // use Structure

在您的情况下,正确的映射是

void anpr_set_params(byte instance, SLPRParams slpr_params);

使用 long 的映射,如果您提取了结构的指针的对等值(> ),则可能在64位非Windows操作系统上使用。 pointer.nativevalue(params.getpointer())。

You can just pass the Structure directly as an argument.

As explained in JNA Structures and Unions,

When a function requires a pointer to a struct, a Java Structure should be used.

Also, the javadoc for Structure says:

When used as a function parameter or return value, this class corresponds to struct*. When used as a field within another Structure, it corresponds to struct. The tagging interfaces Structure.ByReference and Structure.ByValue may be used to alter the default behavior.

Passing a pointer to a structure like SLPRParams* params is the most common way that DLLs expect structures, and that ByReference type is the default behavior. Only if the full structure is passed inline would you need to use the ByValue tag on it.

A nested Structure inside another Structure works the opposite way; it is ByValue by default and would need the ByReference tag if it was declared as a pointer.

The examples in the JNA FAQ (which you also linked) may make more sense once you understand this distinction. The relevant example is this one:

void myfunc(simplestruct* data); // use Structure

In your case the correct mapping is

void anpr_set_params(byte instance, SLPRParams slpr_params);

The mapping with long might appear to work on 64-bit non-Windows operating systems if you extracted the structure's pointer's peer value (Pointer.nativeValue(params.getPointer()). But it wouldn't be portable. Just pass the Structure and JNA handles the rest.

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