Arrays JNA:如何在struct中访问struct的数组?
我试图访问结构中的结构数组。这是简化为问题的相关C代码:Arrays JNA:如何在struct中访问struct的数组?,arrays,struct,jna,jnaerator,Arrays,Struct,Jna,Jnaerator,我试图访问结构中的结构数组。这是简化为问题的相关C代码: typedef struct { int a; int b; } fileinfo_t; typedef struct { fileinfo_t **file; int max_files; } project_t; 在C语言中,访问阵列非常简单: int var_a_of_file_0 = project.file[0].a; int var_b_of_file_1 = project.file[1].b; 如何在
typedef struct {
int a;
int b;
} fileinfo_t;
typedef struct {
fileinfo_t **file;
int max_files;
} project_t;
在C语言中,访问阵列非常简单:
int var_a_of_file_0 = project.file[0].a;
int var_b_of_file_1 = project.file[1].b;
如何在Java中实现这一点?我问这个问题是因为我是JNA的新手。到目前为止,我阅读了JNA文档,并尝试了每一个与我的问题有关的例子,但没有运气
我使用jnavior转换头文件。我不知道舒尔的结果是否正确:
package test;
import com.ochafik.lang.jnaerator.runtime.LibraryExtractor;
import com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper;
import com.ochafik.lang.jnaerator.runtime.Structure;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.jna.ptr.PointerByReference;
/**
* JNA Wrapper for library <b>test</b><br>
* This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
* a tool written by <a href="http://ochafik.free.fr/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
* For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> , <a href="http://rococoa.dev.java.net/">Rococoa</a>, or <a href="http://jna.dev.java.net/">JNA</a>.
*/
public interface TestLibrary extends Library {
public static final java.lang.String JNA_LIBRARY_NAME = LibraryExtractor.getLibraryPath("test", true, test.TestLibrary.class);
public static final NativeLibrary JNA_NATIVE_LIB = NativeLibrary.getInstance(test.TestLibrary.JNA_LIBRARY_NAME, com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper.DEFAULT_OPTIONS);
public static final TestLibrary INSTANCE = (TestLibrary)Native.loadLibrary(test.TestLibrary.JNA_LIBRARY_NAME, test.TestLibrary.class, com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper.DEFAULT_OPTIONS);
public static class fileinfo_t extends Structure<fileinfo_t, fileinfo_t.ByValue, fileinfo_t.ByReference > {
public int a;
public int b;
public fileinfo_t() {
super();
}
public fileinfo_t(int a, int b) {
super();
this.a = a;
this.b = b;
}
protected ByReference newByReference() { return new ByReference(); }
protected ByValue newByValue() { return new ByValue(); }
protected fileinfo_t newInstance() { return new fileinfo_t(); }
public static fileinfo_t[] newArray(int arrayLength) {
return Structure.newArray(fileinfo_t.class, arrayLength);
}
public static class ByReference extends fileinfo_t implements Structure.ByReference {
};
public static class ByValue extends fileinfo_t implements Structure.ByValue {
};
};
public static class project_t extends Structure<project_t, project_t.ByValue, project_t.ByReference > {
/// C type : fileinfo_t**
public PointerByReference file;
public int max_files;
public project_t() {
super();
}
/// @param file C type : fileinfo_t**
public project_t(PointerByReference file, int max_files) {
super();
this.file = file;
this.max_files = max_files;
}
protected ByReference newByReference() { return new ByReference(); }
protected ByValue newByValue() { return new ByValue(); }
protected project_t newInstance() { return new project_t(); }
public static project_t[] newArray(int arrayLength) {
return Structure.newArray(project_t.class, arrayLength);
}
public static class ByReference extends project_t implements Structure.ByReference {
};
public static class ByValue extends project_t implements Structure.ByValue {
};
};
}
封装测试;
导入com.ochafik.lang.jnavior.runtime.LibraryExtractor;
导入com.ochafik.lang.jnavior.runtime.MangledFunctionMapper;
导入com.ochafik.lang.jnavior.runtime.Structure;
导入com.sun.jna.Library;
导入com.sun.jna.Native;
导入com.sun.jna.nativellibrary;
导入com.sun.jna.ptr.PointerByReference;
/**
*用于库测试的JNA包装器
*此文件是由,
*一个由它编写的工具。
*如需帮助,请访问或。
*/
公共接口测试库扩展了测试库{
public static final java.lang.String JNA_LIBRARY_NAME=LibraryExtractor.getLibraryPath(“test”,true,test.TestLibrary.class);
public static final nativellibrary JNA_NATIVE_LIB=nativellibrary.getInstance(test.TestLibrary.JNA_LIBRARY_NAME,com.ochafik.lang.jnabavior.runtime.MangledFunctionMapper.DEFAULT_OPTIONS);
公共静态最终TestLibrary实例=(TestLibrary)Native.loadLibrary(test.TestLibrary.JNA_LIBRARY_名称,test.TestLibrary.class,com.ochafik.lang.jnavior.runtime.MangledFunctionMapper.DEFAULT_选项);
公共静态类fileinfo\t扩展结构{
公共INTA;
公共int b;
公共文件信息(){
超级();
}
公共文件信息(内部a、内部b){
超级();
这个a=a;
这个.b=b;
}
受保护的ByReference newByReference(){返回新的ByReference();}
受保护的ByValue newByValue(){返回新的ByValue();}
受保护的fileinfo_t newInstance(){返回新的fileinfo_t();}
公共静态文件信息[]新数组(整数数组长度){
返回结构.newArray(fileinfo\u t.class,arrayLength);
}
公共静态类ByReference扩展fileinfo\t实现结构。ByReference{
};
公共静态类ByValue扩展fileinfo\u t实现Structure.ByValue{
};
};
公共静态类项目\u t扩展结构{
///C类型:文件信息\u t**
公共指针引用文件;
公共int max_文件;
公共工程(){
超级();
}
///@param文件C类型:fileinfo\u t**
公共项目(指针引用文件、int max文件){
超级();
this.file=文件;
this.max\u files=max\u files;
}
受保护的ByReference newByReference(){返回新的ByReference();}
受保护的ByValue newByValue(){返回新的ByValue();}
受保护的项目\u t newInstance(){返回新项目\u t();}
公共静态项目\u t[]新数组(整数数组长度){
返回结构.newArray(项目类,arrayLength);
}
公共静态类ByReference扩展项目\u t实现结构。ByReference{
};
公共静态类ByValue扩展项目\u t实现Structure.ByValue{
};
};
}
任何帮助都将不胜感激。由于结构数组不会覆盖包含结构的内存,因此该字段需要指针或等效类型。然后可以从基指针手动派生所需的结构 然而,我认为您的使用示例无效 一旦使用“[0]”进行索引,就会有一个指向fileinfo\u t的指针,因此必须使用以下内容(您是否实际用C语言编译了示例?) 最终,如何提取实际结构取决于它们在内存中的布局,这在您当前的解释中是不明确的
int var_a_of_file_0 = project.file[0]->a;
int var_b_of_file_1 = project.file[1]->b;