我有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.
发布评论
评论(1)
您可以直接传递
结构
作为参数。如 jna结构和工会,
另外,结构 说:
将指针传递到诸如
slprams* params
之类的结构是DLL期望结构的最常见方法,而byReference
类型是默认行为。只有在内联的完整结构中,您才需要在其上使用byvalue
标签。另一个结构内的嵌套结构以相反的方式工作。默认情况下,它是
byvalue
,如果将其称为指针,则需要byReference
标签。中的示例(您也链接)可能会更有意义。相关示例是:
在您的情况下,正确的映射是
使用
long
的映射,如果您提取了结构的指针的对等值(>
),则可能在64位非Windows操作系统上使用。 pointer.nativevalue(params.getpointer()
)。You can just pass the
Structure
directly as an argument.As explained in JNA Structures and Unions,
Also, the javadoc for
Structure
says:Passing a pointer to a structure like
SLPRParams* params
is the most common way that DLLs expect structures, and thatByReference
type is the default behavior. Only if the full structure is passed inline would you need to use theByValue
tag on it.A nested Structure inside another Structure works the opposite way; it is
ByValue
by default and would need theByReference
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:
In your case the correct mapping is
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.