2016-09-21 79 views
0

我需要在确定的JAVA JNA相当于C结构,其中每个结构包含另一个结构体变量JNA:相当于Java C结构的,含有另一种结构变量

代码

typedef struct algorithm_list { 

    unsigned char num_of_alg; 
    unsigned short *algorithm_guid[]; 

} algorithm_list_t; 

typedef struct key_data { 

    unsigned char *key; 

    unsigned short key_length; 

    algorithm_list_t *algorithms; 

} key_data_t; 


    typedef struct key_array { 

    unsigned char read_byte; 

    unsigned char number_of_keys; 

    key_data_t *keys[]; 

} key_array_t; 

帮助结构我不能正确定义这些结构的JAVA JNA等价物,因为我实现了这个结构,导致无效的内存访问错误。

回答

0

这些没有一个struct字段。请记住,[]*绑定更紧密(更高的优先级),您分别有一个指向short的指针数组,指向struct的指针(或更可能指向连续数组struct的指针),以及一组数组指向struct

指针类型的最简单映射是Pointer。一旦你得到这个工作,你可以将其改进为更具体的类型。

struct*应该使用Structure.ByReference作为字段类型,并且这些的数组应该是Structure.ByReference[]

如在描述的JNA FAQ(省略getFieldOrder()并且为了简洁的构造函数):

public class algorithm_list extends Structure { 
    public static class ByReference extends algorithm_list implements Structure.ByReference { } 
    public byte num_of_alg; 
    public Pointer[] algorithm_guid = new Pointer[1]; 
    public algorithm_list(Pointer p) { 
     super(p); 
     int count = (int)readField("num_of_alg") & 0xFF; 
     algorithm_guid = new Pointer[count]; 
     super.read(); 
} 

public class key_data extends Structure { 
    public static class ByReference extends key_data implements Structure.ByReference { } 
    public Pointer key; 
    public short key_length; 
    public algorithm_list.ByReference algorithms; 
    public key_data(Pointer p) { 
     super(p); 
     super.read(); 
     // NOTE: if algorithms points to a contiguous block of struct, 
     // you can use "algorithms.toArray(count)" to get that array 
    } 
} 

public class key_array { 
    public byte read_byte; 
    public byte number_of_keys; 
    public key_data.ByReference[] keys = new key_data.ByReference[1]; 
    public key_array(Pointer p) { 
     super(p); 
     int count = (int)readField("number_of_keys") & 0xFF; 
     keys = new key_data.ByReference[count]; 
     super.read(); 
}