2010-11-14 74 views
0

我想访问一个结构内的结构数组。这是相关的C代码减少的问题:JNA:如何在struct中访问struct数组?

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文档并尝试了每个与我的问题有关的例子,但没有运气......

我使用JNAerator来转换头文件。我不知道,如果结果是正确的:

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 { 

     }; 
    }; 
} 

任何帮助,将不胜感激。

回答

0

由于结构数组不覆盖包含结构的内存,所以您需要一个指针或该字段的等效类型。然后您可以从基指针手动派生出您需要的结构。

但是,我不认为您的使用示例是有效的。

一旦你与“[0]”,你有一个指针fileinfo_t的,所以你就必须使用以下指数(你实际上是用C编写的例子吗?):

int var_a_of_file_0 = project.file[0]->a; 
int var_b_of_file_1 = project.file[1]->b; 

最终如何你提取实际结构取决于它们如何在内存中布局,这在你当前的解释中是不明确的。