I want to transfer an array of booleans with in a struct between java and a dll written in C. The struct in C looks like:
struct Parameters_VE3_RSG_v19b_Protect_ {
real_T Constant_Value;
boolean_T Memory_X0;
boolean_T Logic_table[16];
};
In java I have defined the following class to access it:
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 ;
}
}
}
This part of the main where I want to give a value to the boolean array:
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(?????????) ) ); //???
}
}
The problem is I do not know how to fill (and read) the array of booleans, I found examples on http://www.eshayne.com/jnaex/ for an array of strings and an array of doubles, but I do not know how to translate them, so they will work for an array of booleans.
Could somebody give a small example on how to access an array of booleans in a struct?
Thanks a lot, Frank
I solved it the following way:
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;
};
...
And here I fill the logical table:
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);
Thanks, Frank
The appropriate mapping for the native struct
struct Parameters_VE3_RSG_v19b_Protect_ {
real_T Constant_Value;
boolean_T Memory_X0;
boolean_T Logic_table[16];
};
would be
public class Parameters_VE3_RSG_v19b_Protect extends Structure {
public double Constant_Value;
public byte Memory_X0;
public byte[] Logic_table = new byte[16];
}
You are implying that instead your native struct is defined as the following:
struct Parameters_VE3_RSG_v19b_Protect_ {
real_T Constant_Value;
boolean_T Memory_X0;
boolean_T* Logic_table;
};
This represents a distinctly different memory layout than what you pose in your question, namely:
with array with pointer
+----------------+ +----------------+
Constant_Value | 0x0000-0x0007 | | 0x0000-0x0007 |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
+----------------+ +----------------+
Memory_X0 | 0x0008 | | 0x0008 |
+----------------+ +----------------+
Logic_table | 0x0009-0x0018 | | 3 or 7 bytes |
(array) | | | padding |
| | | |
| | +----------------+
| | | 0x000C-0x000F | logic_table (pointer)
| | | or |
| | | 0x0010-0x0018 |
| | | |
| | +----------------+
| |
| |
| |
| |
| |
| |
| |
| |
+----------------+
The difference in length on the right hand side is whether your pointers are 32- or 64-bit in size. The left side may or may not have padding preceding the byte array, depending on your compiler and settings.
What does sizeof(Parameters_VE3_RSG_v19b_Protect_)
return?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.