2016-09-21 78 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(); 
}