如何将指针映射到 JNA 中的结构数组
我正在尝试映射 Win32 函数 EnumJobs< /a> 在 JNA 中。该方法具有以下签名:
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
);
我弄清楚了其中的大部分内容,除了 LPBYTE pJob 之外,根据文档,它是一个指向接收 JOB_INFO 结构数组的缓冲区的指针。我似乎无法弄清楚如何正确地进行此映射。到目前为止,我已经:
boolean EnumJobs(HANDLE hPrinter, DWORD FirstJob, DWORD NoJobs, DWORD Level, JOB_INFO_2[] pJob, DWORD cbBuf, IntByReference pcbNeeded, IntByReference pcReturned );
但我得到了一个 IllegalArgumentException: Can't certain size of Nested Structure: can't instantiate class com.sun.jna.struct (java.lang.InstantiationException) 关于如何映射它的任何见解手柄将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1) 计算出您需要(或想要)多少个 JOB_INFO 结构
2) 在 JOB_INFO 的单个实例上使用 Structure.toArray() 来获取它们的连续(在内存中)数组
3) 传入第一个 JOB_INFO 结构或其内存(Structure.getPointer),取决于您的方法签名。
请注意,方法签名中的 Structure 参数将向 JNA 指示它需要自动将 Java 结构内存与本机内存(包括整个数组)同步;使用指针将同步留给您。
此外,作为方法参数的结构意味着“struct *”,而不是“struct”作为参数类型。
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.