簡體   English   中英

JNA和struct中的布爾數組

[英]JNA and array of booleans in struct

我想在Java和用C編寫的dll之間的結構中傳遞布爾數組。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 ;           
                       }
          }
}

我想為布爾數組賦值的主要部分:

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(?????????)   ) ); //???
       }
}

問題是我不知道如何填充(和讀取)布爾數組,我在http://www.eshayne.com/jnaex/找到了有關字符串數組和雙精度數組的示例,但是我沒有知道如何翻譯它們,因此它們將適用於布爾數組。

有人可以舉一個小例子來說明如何在結構中訪問布爾數組嗎?

非常感謝,弗蘭克

我通過以下方式解決了它:

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);

謝謝,弗蘭克

本地結構的適當映射

struct Parameters_VE3_RSG_v19b_Protect_ {
    real_T Constant_Value;              

    boolean_T Memory_X0;               
    boolean_T Logic_table[16];         
};

將會

public class Parameters_VE3_RSG_v19b_Protect extends Structure {
    public double Constant_Value;
    public byte Memory_X0;
    public byte[] Logic_table = new byte[16];
}

您是在暗示,您的本機結構定義如下:

struct Parameters_VE3_RSG_v19b_Protect_ {
    real_T Constant_Value;              

    boolean_T Memory_X0;               
    boolean_T* Logic_table;
};

這表示的內存布局與您在問題中提出的布局截然不同,即:

                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  |
               |                | |                |
               |                | +----------------+
               |                |
               |                | 
               |                | 
               |                | 
               |                |
               |                |
               |                | 
               |                | 
               +----------------+

右側長度的不同是指針的大小是32位還是64位。 根據您的編譯器和設置,左側可能在字節數組之前有填充,也可能沒有。

sizeof(Parameters_VE3_RSG_v19b_Protect_)返回什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM