[英]C struct to Java JNA Structure (pointer to 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;
這是根據FAQ進行的 。
什么時候應該使用Structure.ByReference? Structure.ByValue? 結構體[]?
typedef struct _outerstruct2 {
simplestruct *byref; // use Structure.ByReference
} outerstruct2;
作為附錄:
當我使用mingw編譯的
dll
進行存根時,我必須繼承自StdCallLibrary
而不是Library
-對您而言,情況可能並非如此,我只是提到它,因為它影響了我的測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.