简体   繁体   English

C struct到Java JNA Structure(指向struct的指针)

[英]C struct to Java JNA Structure (pointer to struct)

I have problem with JNA Structure based on C/C++ struct. 我对基于C / C ++结构的JNA结构有问题。 Fields nScreenIndex, uVendorID, uProductID, uVersionNumber looks OK, but after them I see odd bytes. 字段nScreenIndex,uVendorID,uProductID,uVersionNumber看起来不错,但是在它们之后,我看到了奇数字节。 My main and only goal is to "extract" pMonitor fields. 我的主要也是唯一的目标是“提取” pMonitor字段。 Are pMonitor declaration and MONITOR implementation correct? pMonitor声明和MONITOR实现正确吗?

C/C++ origin: 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;

and Java/JNA code: 和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 {
    };
}

You're so very close to right. 您非常靠近右侧。

in the SCREEN class in java, you need to define pMonitor as: 在Java的SCREEN类中,您需要将pMonitor定义为:

    public MONITOR.ByReference pMonitor;

This is per the FAQ . 这是根据FAQ进行的

When should I use Structure.ByReference? 什么时候应该使用Structure.ByReference? Structure.ByValue? 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 from StdCallLibrary and not Library - this may not be the case for you, I'm just mentioning this as it affected my testing. 当我使用mingw编译的dll进行存根时,我必须继承自StdCallLibrary而不是Library -对您而言,情况可能并非如此,我只是提到它,因为它影响了我的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM