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

时间:2016-04-20 14:37:48

标签: java c struct structure jna

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

1 个答案:

答案 0 :(得分:2)

你非常接近正确。

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

    public MONITOR.ByReference pMonitor;

这是per the FAQ

  

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

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

作为附录:

  

当我使用mingw编译dll来解决这个问题时,我必须继承StdCallLibrary而不是Library - 这可能不是你的情况,我是只是提到这个因为它影响了我的测试。