简体   繁体   English

指针的JNA Java结构

[英]JNA Java struct from pointer

I have a struct, which is not fully described like the original C one. 我有一个结构,没有像原始C语言那样完整地描述。

    public class DISPLAY_DEVICE extends Structure {
        public char DeviceName[] = new char[32];
        public int StateFlags;
    }

Whereas it actually needs, way, more variables. 实际上,它实际上需要更多的变量。 However it will take me a long time to port them all over. 但是,将它们移植到所有位置都需要花费我很长时间。 Now I create the struct and pass the pointer to a dll function, and try to use device.read(); 现在,我创建该结构并将指针传递给dll函数,并尝试使用device.read();。 to regain the variables. 重新获得变量。 However, the variables return empty. 但是,变量返回空。 So my question is, do I need to fill out the whole struct? 所以我的问题是,我需要填写整个结构吗? Or is there something else wrong? 还是还有其他问题?

    DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE();
    int i = 0;
    while((CLibrary.INSTANCE.EnumDisplayDevicesA(Pointer.NULL, i, displayDevice.getPointer(), 0))) {
        System.out.println("screen" + i);
        displayDevice.read();
        System.out.println(displayDevice.StateFlags);
        System.out.println(displayDevice.DeviceName);

At a minimum, you must define the structure to be the same size as its native counterpart (you can pad it with a byte[] field for stuff you don't care about). 至少,您必须将结构定义为与其本机对应的结构相同的大小(您可以在byte []字段中填充不需要的内容)。

For example: 例如:

public class MyStruct extends Structure {
   public char[] DeviceName = new char[32];
   public char StateFlags;
   public byte[] dontcare = new char[128];
}

You can also use JNAerator to auto-generate mappings from a C header if the definition is not available in JNA's platform.jar. 如果该定义在JNA的platform.jar中不可用,您还可以使用JNAerator从C标头自动生成映射。

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

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