[英]JNA Structure inside a structure
我对JNA编程很陌生。 我有如下的本机代码
int Data(int Number, aaa *Data, int* error);
typedef struct {
uint8 Storage;
LStr path;
int32 chart;
int32 strt;
LStr val;
int32 timedata;
} aaa;
typedef struct {
int32 cnt; /*num of bytes that follow*/
uChar str[1];
} LStrval, *LStrPtr, **LStr;
如何使用JNA从Java调用此本机函数。 我尝试了几种选择,但没有得到结果。 我尝试的选项之一如下。
接口功能
public int SetStorage_Data(int num,Storage_Data.ByReference data, Pointer error);
public class Storage_Data extends Structure{
public static class ByReference extends Storage_Data implements Structure.ByReference {}
public byte storage;
public Stringtype.ByReference savepath;
public int chart;
public int strt;
public Stringtype.ByReference val;
public int timedata;
@Override
protected List getFieldOrder() {
return Arrays.asList("storage", "savepath","chart",
"strt","val","timedata");
}
}
public class Stringtype extends Structure{
public static class ByReference extends Stringtype implements Structure.ByReference {
public ByReference(int buffersize) {
super(buffersize);
// TODO Auto-generated constructor stub
}}
public int count;
public byte[] str;
public Stringtype(int buffersize) {
str = new byte[buffersize];
count = str.length;
allocateMemory();
}
@Override
protected List getFieldOrder() {
return Arrays.asList("count", "str");
}
}
和Java调用
InjectionAnalyzerInterfaces.Storage_Data.ByReference storagedata = new InjectionAnalyzerInterfaces.Storage_Data.ByReference();
int len= string.length;
InjectionAnalyzerInterfaces.Stringtype.ByReference savepath = new InjectionAnalyzerInterfaces.Stringtype.ByReference(len);
byte[] stringbyte = string.getBytes();
System.arraycopy(stringbyte, 0, savepath.str, 0, bytes);
storagedata.savepath = savepath;
storagedata.chart = 1;
storagedata.strt =1;
String temp= "all";
byte[] strtbyte = temp.getBytes();
int dd = strtbyte.length;
InjectionAnalyzerInterfaces.Stringtype.ByReference stra = new InjectionAnalyzerInterfaces.Stringtype.ByReference(dd);
System.arraycopy(strtbyte, 0, stra.str,0, dd);
storagedata.strt = stra;
storagedata.tdata = 0;
Pointer error = new Memory(5);
int status1 = lib.Set_Config_Storage_Data(deviceNumber, storagedata, error);
请帮帮我。 先感谢您
在定义LStrVal
,您需要初始化原始数组字段,以便JNA知道要分配多少内存(至少LStrVal
开始):
public byte str = new byte[1];
您需要重写Structure.read()
来做正确的事情,并提供一个基于Pointer
的构造函数,以便从本机内存初始化后进行读取:
public void read() {
count = (int)readField("count");
str = new byte[count];
super.read();
}
public LStr(Pointer p) {
super(p);
read();
}
最后,您的包含结构具有struct**
字段(不知道为什么),您必须将其映射到Pointer
,并提供便捷功能以实际转换为所需的结构:
public Pointer savepath;
public LStr getSavepath() {
return savepath != null ? new LStr(savepath.getPointer(0)) : null;
}
您走在正确的轨道上。
您需要将内部结构( LStrVal
)声明为其自己的结构类。 看起来您已经用Stringtype
结构完成了此操作,但是您需要在其中进行一些更改:
byte[]
数组的大小指定为1,只需使用该常数即可。 super()
的无参数构造函数 super(p)
ByReference
部分 然后,外部结构应该为这些结构指针使用PointerByReference
类型,然后在主代码中使用new Stringtype(pbr.getValue())
将指针转换为必要的类。
或者,正如@technomage所建议的,您可以使用Pointer类型,然后使用getPointer(0)
代替getValue()
getPointer(0)
。 最重要的是,您需要一个指针(指向一个指针)作为实例变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.