简体   繁体   中英

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

I'm trying to map the Win32 function EnumJobs in JNA. 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. 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.

1) figure out how many JOB_INFO structures you need (or want)

2) Use Structure.toArray() on a single instance of JOB_INFO to get a contiguous (in memory) array of them

3) Pass in the first JOB_INFO structure or its memory (Structure.getPointer), depending on your method signature.

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); 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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