C结构到Java JNA结构(指向struct) [英] C struct to Java JNA Structure (pointer to struct)
问题描述
我有一个基于C / C ++结构JNA结构问题。场nScreenIndex,uVendorID,uProductID,uVersionNumber看起来不错,但在他们之后我看到奇怪的字节。我的主要和唯一的目标就是提取P监视器领域。
是的 P监视器的声明和 MONITOR 实施正确的?
C / C ++产地:
SCREEN * EloGetScreenByIndex(INT nScreenIndex);typedef结构SCREEN_TAG
{
INT nScreenIndex;
USHORT uVendorID;
USHORT uProductID;
USHORT uVersionNumber;
wchar_t的szDevicePath [MAX_PATH];
HANDLE hCalTouchThread;
MONITOR * P监视器;
LPVOID pCWndBeamHandler;
BOOL bIrBeams;
}屏;typedef结构MONITORS_TAG
{
INT elo_mon_num;
INT X;
诠释Ÿ;
INT宽度;
INT高度;
DWORD方向;
布尔is_primary;
} MONITOR;
和Java / JNA code:
屏幕EloGetScreenByIndex(INT nScreenIndex);公共类SCREEN扩展结构{
公众诠释nScreenIndex;
公共短uVendorID;
公共短uProductID;
公共短uVersionNumber;
公众的char [] = szDevicePath新的char [WinDef.MAX_PATH]
公共WinNT.HANDLE hCalTouchThread;
公共MONITOR P监视器;
公共PointerByReference pCWndBeamHandler;
公共布尔bIrBeams;
...
}公共班长扩展结构{
公众诠释elo_mon_num;
公众诠释X;
公众诠释Ÿ;
公众诠释宽度;
公众诠释高度;
公众诠释取向;
公共字节is_primary; 公共MONITOR(){
超();
} @覆盖
保护名单,LT ;? > getFieldOrder(){
返回Arrays.asList(elo_mon_num,X,Y,宽度,高度,方向,is_primary);
} 公共MONITOR(指针等){
超(同行);
} 公共静态类ByReference扩展MONITOR实现Structure.ByReference {
}; 公共静态类扩展根据值实现MONITOR {Structure.ByValue
};
}
您是如此非常接近正确的。
在Java中的Screen类,您需要定义 P监视器
为:
公共MONITOR.ByReference P监视器;
这是<一个href=\"https://github.com/java-native-access/jna/blob/master/www/FrequentlyAskedQuestions.md#when-should-i-use-structurebyreference-structurebyvalue-structure\"相对=nofollow>每页的FAQ 。
当我应该使用Structure.ByReference? Structure.ByValue?结构体[]?
块引用>typedef结构_outerstruct2 {
simplestruct *按地址; //使用Structure.ByReference
} outerstruct2;作为附录:
当我掐灭这件事用编了MinGW的
DLL
,我不得不从StdCallLibrary
,而不是<$继承C $ C>图书馆 - 这可能不适合你的话,我只是提这个,因为它影响了我的测试
块引用>I have problem with JNA Structure based on C/C++ struct. Fields nScreenIndex, uVendorID, uProductID, uVersionNumber looks OK, but after them I see odd bytes. My main and only goal is to "extract" pMonitor fields. Are pMonitor declaration and MONITOR implementation correct?
C/C++ origin:
SCREEN* EloGetScreenByIndex (int nScreenIndex); typedef struct SCREEN_TAG { int nScreenIndex; USHORT uVendorID; USHORT uProductID; USHORT uVersionNumber; wchar_t szDevicePath [MAX_PATH]; HANDLE hCalTouchThread; MONITOR* pMonitor; LPVOID pCWndBeamHandler; BOOL bIrBeams; } SCREEN; typedef struct MONITORS_TAG { int elo_mon_num; int x; int y; int width; int height; DWORD orientation; bool is_primary; } MONITOR;
and Java/JNA code:
SCREEN EloGetScreenByIndex(int nScreenIndex); public class SCREEN extends Structure { public int nScreenIndex; public short uVendorID; public short uProductID; public short uVersionNumber; public char[] szDevicePath = new char[WinDef.MAX_PATH]; public WinNT.HANDLE hCalTouchThread; public MONITOR pMonitor; public PointerByReference pCWndBeamHandler; public boolean bIrBeams; ... } public class MONITOR extends Structure { public int elo_mon_num; public int x; public int y; public int width; public int height; public int orientation; public byte is_primary; public MONITOR() { super(); } @Override protected List<? > getFieldOrder() { return Arrays.asList("elo_mon_num", "x", "y", "width", "height", "orientation", "is_primary"); } public MONITOR(Pointer peer) { super(peer); } public static class ByReference extends MONITOR implements Structure.ByReference { }; public static class ByValue extends MONITOR implements Structure.ByValue { }; }
解决方案You're so very close to right.
in the SCREEN class in java, you need to define
pMonitor
as:public MONITOR.ByReference pMonitor;
This is per the FAQ.
When should I use Structure.ByReference? Structure.ByValue? Structure[]?
typedef struct _outerstruct2 { simplestruct *byref; // use Structure.ByReference } outerstruct2;
As an addendum:
When I stubbed this up using a mingw compiled
dll
, I had to inherit fromStdCallLibrary
and notLibrary
- this may not be the case for you, I'm just mentioning this as it affected my testing.
这篇关于C结构到Java JNA结构(指向struct)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!