C结构到Java JNA结构(指向struct的指针)

我有基于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 { }; } 

你非常接近正确。

在java的SCREEN类中,您需要将pMonitor定义为:

  public MONITOR.ByReference pMonitor; 

这是常见问题解答 。

我什么时候应该使用Structure.ByReference? Structure.ByValue? 结构体[]?

 typedef struct _outerstruct2 { simplestruct *byref; // use Structure.ByReference } outerstruct2; 

作为附录:

当我使用mingw编译的dll对此进行StdCallLibrary ,我不得不inheritanceStdCallLibrary而不是Library – 这可能不是你的情况,我只是提到它,因为它影响了我的测试。