我在基于C/C++结构体的JNA结构中遇到了问题。nScreenIndex、uVendorID、uProductID和uVersionNumber字段看起来没问题,但在它们后面是奇怪的字节。我的主要目标是“提取”pMonitor字段。 pMonitor声明和MONITOR实现是否正确?
C/C++原始代码:
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;
以及Java/JNA代码:
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 {
};
}
pCWndBeamHandler
应该是Pointer
,而不是PointerByReference
。你还应该为每个Structure
添加一个基于Pointer
的构造函数,在super()
之后调用Structure.read()
(这不是必要的,但可以避免不必要的额外内存分配)。 - technomage