JNA将数组的引用传递给本机DLL。

huangapple go评论76阅读模式
英文:

JNA passing reference of array into native DLL

问题

以下是翻译好的部分:

这个 DLL 中的函数是:

int getInfo (
  unsigned int Index,
  unsigned int* Mask,
  unsigned int* Serial,
  unsigned __int64* licInfo);

licInfo 是一个包含 4 个元素的数组非常重要。

在 Java 中,这个方法声明如下:

int getInfo(int Index, IntByReference Mask, IntByReference Serial, Memory licInfo);

方法调用部分:

int Index = 0;
IntByReference Mask = null;
IntByReference Serial = null;
Memory LicInfo = new Memory(256);    
int status = dll.INSTANCE.getInfo(Index, Mask, Serial, licInfo);

这个 DLL 返回一个错误代码,表示参数有误。我非常确定错误在最后一个参数上。我还尝试过直接传递一个长整型数组或者传递一个 Pointer,但都没有成功。

英文:

The function in the dll is:

int getInfo (
  unsigned int Index,
  unsigned int* Mask,
  unsigned int* Serial,
  unsigned __int64* licInfo);

It is important that licInfo is an array with 4 elements.

In Java the method is declared like this:

int getInfo(int Index, IntByReference Mask, IntByReference Serial, Memory licInfo);

the method call:

int Index = 0;
IntByReference Mask = null;
IntByReference Serial = null;
Memory LicInfo = new Memory(256);    
int status = dll.INSTANCE.getInfo(Index, Mask, Serial, licInfo);

The dll returns an error code that says the parameters are wrong. I am pretty sure the mistake is the last parameter. I also tried passing an long array directly or passing an Pointer without success.

答案1

得分: 1

函数映射本身看起来是合理的,尽管我会将映射中的 Memory 替换为 Pointer。由于 Memory 扩展了 Pointer,您仍然可以像现在一样初始化并传递该值。

在没有API文档指定参数期望值的情况下,我必须根据问题做一些有根据的猜测。本机函数期望一个 int 和三个指向信息的指针,您已经说明最后一个是一个 long 数组。那么前两个指针的期望值是什么?

问题很可能是传递给 MaskSerial 指针的 null 值。除非API明确允许使用 null,否则这些指针很可能应该使用 new IntByReference() 进行初始化,以便在本机端为一个可以在本机端填充的 int 分配本机内存。

虽然为 long[4] 进行过多的内存分配不应该特别引起错误,但您已经为256 bit 数组分配了 256 bytes 的内存。分配 new Memory(32) 应该就足够了。

英文:

The function mapping itself looks sane, although I would replace Memory with Pointer in the mapping. As Memory extends Pointer you can still initialize and pass the value as you do now.

Without the API documentation specifying what it expects for arguments, I must make some educated guesses as to the problem. The native function expects an int and 3 pointers to information, the last of which you've stated is an array of long. What is expected in the first two?

The problem is most likely the null values being passed for the Mask and Serial pointers. Unless the API specifically says null is allowed there, these should most likely be initialized with new IntByReference() to allocate native-side memory for an int that can be populated on the native side.

While over-allocating memory for the long[4] shouldn't specifically cause the error, you've allocated 256 bytes for a 256 bit array. Allocating new Memory(32) should suffice.

huangapple
  • 本文由 发表于 2020年9月4日 20:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63741328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定