0

構造体内の構造体の配列にアクセスしようとしています。これは、問題に縮小された関連する 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;

これを 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 {

        };
    };
}

どんな助けでも大歓迎です。

4

1 に答える 1

0

構造体の配列は含まれている構造体のメモリをオーバーレイしないため、そのフィールドには Pointer または同等の型が必要です。その後、ベース ポインターから必要な構造を手動で派生させることができます。

ただし、使用例は有効ではないと思います。

"[0]" でインデックスを作成すると、fileinfo_t へのポインターが作成されるため、次を使用する必要があります (実際に C で例をコンパイルしましたか?)。

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

最終的に、実際の構造をどのように抽出するかは、それらがメモリ内でどのように配置されているかに依存しますが、これは現在の説明ではあいまいです。

于 2011-07-25T14:44:34.837 に答える