我想在 java 和用 C 编写的 dll 之间传输 boolean 数组。 C 中的结构如下所示:
struct Parameters_VE3_RSG_v19b_Protect_ {
real_T Constant_Value;
boolean_T Memory_X0;
boolean_T Logic_table[16];
};
在java中我定义了以下类来访问它:
public class VehicleModel {
public interface CLibrary extends Library {
public static class Parameters_VE3_RSG_v19b_Protect_ extends Structure {
public static class ByReference extends Parameters_VE3_RSG_v19b_Protect_ implements Structure.ByReference {}
public double Constant_Value ;
public boolean Memory_X0 ;
public Pointer Logic_table ;
}
}
}
这部分是我想给 boolean 数组赋值的地方:
public class SpecificVehicle {
public static void main(String[] args) {
Vehicle vh = new Vehicle();
vh. parameters .Constant_Value = -1.000000;
vh. parameters .Memory_X0 = false;
CLibrary.Parameters_VE3_RSG_v19b_Protect_.ByReference ltref = new CLibrary.Parameters_VE3_RSG_v19b_Protect_.ByReference();
ltref.Logic_table = new Memory(16*Native.getNativeSize(?????????) ) ); //???
}
}
问题是我不知道如何填充(和读取) boolean 数组,我在 http://www.eshayne.com/jnaex/ 上找到了示例对于字符串数组和 double 组,但我不知道如何翻译它们,所以它们适用于 boolean 数组。
有人可以举一个小例子来说明如何访问结构中的 boolean 数组吗?
非常感谢, 弗兰克
最佳答案
我通过以下方式解决了这个问题:
static Pointer setbooleanArray(boolean [] input) {
Pointer output = new Memory(input.length*Native.getNativeSize(Byte.TYPE));
for (int i=0;i<input.length;i++){
output.setByte(i, (byte) (input[i] ? 1:0));
};
return output;
};
...
这里我填写逻辑表:
boolean[] Logic_tableSet = new boolean[] { false, true, false, false, true, true, false, false, true, false, true, true, false, false, false, false };
vh.parameters.Logic_table = setbooleanArray( Logic_tableSet);
谢谢, 弗兰克
关于java - JNA 和结构体中的 boolean 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21095957/