Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JNA中结构中的结构数组_Java_Jna - Fatal编程技术网

Java JNA中结构中的结构数组

Java JNA中结构中的结构数组,java,jna,Java,Jna,我的本地代码是 typedef struct driver_config { unsigned int dllVersion; unsigned int channelCount; unsigned int reserved[10]; ChannelConfig channel[64]; } DriverConfig; 在Java中,我的类如下所示 public class DriverConfig extends Structure { pu

我的本地代码是

typedef struct driver_config {
    unsigned int dllVersion;
    unsigned int channelCount;
    unsigned int reserved[10];
    ChannelConfig channel[64];
} DriverConfig;
在Java中,我的类如下所示

public class DriverConfig extends Structure {
    
    public int dllVersion;
    public int channelCount;
    public int[] reserved= new int[10];
    ChannelConfig[] channel = new ChannelConfig[64];
    
    public DriverConfig() {
        super();
        init();     
    }
    
    private void init() {
        for (int i = 0; i < channel.length; i++) {
            channel[i]= new ChannelConfig();
        }
    }

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { "dllVersion", "channelCount", "reserved" });
    }

    //toString()...
}
DriverConfig driverConfig = new DriverConfig();
status = dll.INSTANCE.getDriverConfig(driverConfig);
System.out.println("DriverConfig Status: " + status);
System.out.println(driverConfig.toString());
我试图像这样访问该方法

public class DriverConfig extends Structure {
    
    public int dllVersion;
    public int channelCount;
    public int[] reserved= new int[10];
    ChannelConfig[] channel = new ChannelConfig[64];
    
    public DriverConfig() {
        super();
        init();     
    }
    
    private void init() {
        for (int i = 0; i < channel.length; i++) {
            channel[i]= new ChannelConfig();
        }
    }

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { "dllVersion", "channelCount", "reserved" });
    }

    //toString()...
}
DriverConfig driverConfig = new DriverConfig();
status = dll.INSTANCE.getDriverConfig(driverConfig);
System.out.println("DriverConfig Status: " + status);
System.out.println(driverConfig.toString());
如果将
channel.length
替换为小于50,则数组已正确初始化,但使用
channel.length
则数组不起作用。它甚至没有显示任何错误,只是没有显示任何内容。

您的
getFieldOrder()
数组不包括您的结构的最后一个元素(
channel
)。我在您的评论中看到,您试图这样做,但收到了一个错误,因为您没有将其声明为公共的。结构的所有元素都必须在
字段顺序中列出,并声明为
公共
,以便通过反射找到它们

另外,对于JNA 5.x(您应该使用它),首选
@FieldOrder
注释

您尚未识别
ChannelConfig
的映射,但您的问题标题并指出它是嵌套结构数组。必须使用连续内存分配结构数组,可以直接分配需要知道结构大小的本机内存(
newmemory()
),也可以使用
Structure.toArray()
)。在循环中进行分配,最终将在本机内存中可能/可能不连续的位置为每个新结构分配内存。鉴于您声明它似乎对某些值有效,您可能会幸运地获得连续分配,但您的行为肯定是未定义的

因此,您的结构映射应该是:

@FieldOrder ({"dllVersion", "channelCount", "reserved", "channel"})
public class DriverConfig extends Structure {
    public int dllVersion;
    public int channelCount;
    public int[] reserved= new int[10];
    public ChannelConfig[] channel = (ChannelConfig[]) new ChannelConfig().toArray(64);
}

channel
是否应该按字段顺序排列?我不确定getFieldOrder()中的内容到底是什么意思。首先我尝试使用通道,但出现了一个错误,错误是什么?类DriverConfig上的Structure.getFieldOrder()提供了太多的名称[4]([channel,channelCount,dllVersion,reserved]),无法匹配声明的字段[3]([channelCount,dllVersion,reserved])