简体   繁体   English

通过JNA检索数组

[英]Retrieving an array via JNA

I've got an problem with getting information back from a memory block allocated by the called function. 我从被调用函数分配的内存块中获取信息时遇到问题。 Specifically, there's function XGetWindowProperties in the X11 lib included with JNA. 具体来说,JNA随附的X11库中有函数XGetWindowProperties It returns the results via the last parameter - PointerByReference prop_return . 它通过最后一个参数PointerByReference prop_return返回结果。

I know the actual memory area contains an array of Window . 我知道实际的内存区域包含一个Window数组。 But how do I create those objects? 但是,如何创建这些对象? I don't see any way to get it from the pointer itself and I can't get a Buffer from it, since I don't know the size of Window . 我不知道从指针本身获取任何方法,也无法从其获取Buffer ,因为我不知道Window的大小。

The real function is: int XGetWindowProperty(...., unsigned char **prop_return) and with the parameters I'm calling it with, it's actually int XGetWindowProperty(...., Window *prop_return[]) . 真正的功能是: int XGetWindowProperty(...., unsigned char **prop_return)并使用我调用它的参数,它实际上是int XGetWindowProperty(...., Window *prop_return[])

The size of the elements in the returned data is defined by the returned "format" from XGetWindowProperty(), with one annoying exception. 返回数据中元素的大小由XGetWindowProperty()返回的“格式”定义,但有一个令人讨厌的异常。 Xlib always returns format 32 as an array of "long" even if "long" is 64-bit. Xlib始终将格式32作为“长”数组返回,即使“长”是64位。 Window would have format 32. 窗口的格式为32。

In a quick google search, you can use NativeLong.SIZE to determine this in JNA. 在快速的Google搜索中,您可以使用NativeLong.SIZE在JNA中进行确定。 So if (NativeLong.SIZE == 8) you have a buffer of 64-bit window IDs, otherwise of 32-bit window IDs. 因此,如果(NativeLong.SIZE == 8),则您具有64位窗口ID的缓冲区,否则具有32位窗口ID的缓冲区。

The byte order of each value should match the CPU you're on. 每个值的字节顺序应与您使用的CPU匹配。

You'll need to extract the data first as an array of primitive type (int or long as appropriate). 您需要首先将数据提取为原始类型的数组(适当时为int或long)。 you can then construct X11.Window objects as needed using the XIDs from the array. 然后,您可以根据需要使用数组中的XID构造X11.Window对象。

It is possible to set up converters to handle some of this automatically, but ultimately it's simpler for a one-off to do the conversion explicitly. 可以设置转换器来自动处理一些这方面,但最终它是一次性进行转换明确简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM