typedef struct 指针进入 JNA

typedef struct pointer into JNA

本文关键字:JNA 指针 struct typedef      更新时间:2023-10-16

我正在为我的应用程序开发JNA,我对在java中使用typedef结构指针感到困惑。请检查我的代码并指导我。提前谢谢你!

下面是我的 c++ 代码

typedef struct tagHWDeviceInfo
{
USHORT          VendorID;      // vendor id
USHORT          ProductID;     // product id
DWORD           nXExt;         // firmware width
DWORD           nYExt;         // firmware height
DWORD           preesure;      // pressure level
DWORD           penState;      // pen state
}HWDeviceInfo, *PHWDeviceInfo;

和 Java 代码

public class HWDeviceInfo extends Structure{
    short VendorID;
    short ProductID;
    int   nXExt;        // firmware width
    int   nYExt;        // firmware height
    int   preesure;     // pressure level
    int   penState;
}

现在我的问题是:在 c++ 代码中*PHWDeviceInfo是什么意思,如何在我的 java 代码中使用此指针?

JNA 会自动将Structure实例转换为函数调用中的struct *,并在结构字段定义中自动转换为structStructure.getPointer()为您提供 JNA 将使用的指针。

您可以通过使用 Structure.ByReferenceStructure.ByValue 接口标记类和/或参数类型来修改此默认行为。

JNA清楚地记录了这一点,正如@areeba-irfan-ullah-khan所指出的那样。