简体   繁体   English

如何将指针映射到JNA中的结构数组

[英]How do I map a pointer to an array of structures in JNA

I'm trying to map the Win32 function EnumJobs in JNA. 我正在尝试在JNA中映射Win32函数EnumJobs The method has the following signature: 该方法具有以下特征:

BOOL EnumJobs(
  __in   HANDLE hPrinter,
  __in   DWORD FirstJob,
  __in   DWORD NoJobs,
  __in   DWORD Level,
  __out  LPBYTE pJob,
  __in   DWORD cbBuf,
  __out  LPDWORD pcbNeeded,
  __out  LPDWORD pcReturned
);

I figured out most of it except LPBYTE pJob which according to the documentation is a pointer to a buffer that receives an array of JOB_INFO structures. 除了LPBYTE pJob之外,我想出了大部分内容,根据文档,它是指向接收JOB_INFO结构数组的缓冲区的指针。 I can't seem to figure out how to do this mapping correctly. 我似乎无法弄清楚如何正确地进行这种映射。 So far I have: 到目前为止,我有:

   boolean EnumJobs(HANDLE hPrinter, DWORD FirstJob, DWORD NoJobs, DWORD Level, JOB_INFO_2[] pJob, DWORD cbBuf, IntByReference pcbNeeded, IntByReference pcReturned );

but I'm getting an IllegalArgumentException: Can't determine size of nested structure: can't instantiate class com.sun.jna.structure (java.lang.InstantiationException) Any insight into how this should be mapped and handle would be greatly appreciated. 但我得到一个IllegalArgumentException:无法确定嵌套结构的大小:无法实例化类com.sun.jna.structure(java.lang.InstantiationException)任何有关如何映射和处理的深入见解将不胜感激。

1) figure out how many JOB_INFO structures you need (or want) 1)弄清楚你需要(或想要)多少个JOB_INFO结构

2) Use Structure.toArray() on a single instance of JOB_INFO to get a contiguous (in memory) array of them 2)在JOB_INFO的单个实例上使用Structure.toArray()来获取它们的连续(在内存中)数组

3) Pass in the first JOB_INFO structure or its memory (Structure.getPointer), depending on your method signature. 3)传入第一个JOB_INFO结构或其内存(Structure.getPointer),具体取决于您的方法签名。

Note that a Structure argument in a method signature will indicate to JNA that it needs to automatically sync your Java structure memory with native memory (including the entire array); 请注意,方法签名中的Structure参数将向JNA指示它需要自动将Java结构内存与本机内存(包括整个数组)同步; using a Pointer leaves the synching up to you. 使用指针离开同步到你。

In addition, a Structure as a method parameter implies "struct *", not "struct" as an argument type. 另外,作为方法参数的Structure表示“struct *”,而不是“struct”作为参数类型。

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

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