[英]JNA: Java equivalent of C Structures, Structure containing another structure variable
I need help in defining JAVA JNA equivalent of C Structures where each structure contains another structure variable 我需要帮助来定义等效于C结构的JAVA JNA,其中每个结构都包含另一个结构变量
Code 码
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;
I am not able to properly define JAVA JNA equivalent of these structure as what I have implemented gives me Invalid Memory access error. 我无法正确定义与这些结构等效的JAVA JNA,因为我实现的内容给了我无效的内存访问错误。
None of these has a struct
field. 这些都没有一个
struct
字段。 Keeping in mind that []
binds more tightly (higher precedence) than *
, you have, respectively, an array of pointer to short, a pointer to struct
(or a pointer to a contiguous array of struct
, more likely), and an array of pointer to struct
. 注意,保持
[]
结合更紧密(高优先级),比*
,则分别具有指针的短的阵列,一个指向struct
(或一个指向的一个连续数组struct
,更可能的),和阵列指向struct
的指针。
The easiest mapping for a pointer type is Pointer
. 指针类型最简单的映射是
Pointer
。 Once you get that working, you can refine it to a more specific type. 一旦工作成功,就可以将其优化为更具体的类型。
struct*
should use Structure.ByReference
as the field type, and an array of those would be Structure.ByReference[]
. struct*
应该使用Structure.ByReference
作为字段类型,而这些数组将是Structure.ByReference[]
。
As described in the JNA FAQ (omitting getFieldOrder()
and constructors for brevity): 如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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.