0

次の C++ 構造体と関数があります。

typedef struct _Phase_Information
{
  char infoMessage[MAX];
} INFORMATION;

typedef struct _Informations
{
  int infoCount;
  INFORMATION *infoArray;
} INFORMATIONS ;

int GetInformations(INFORMATIONS *pInfos);

私はそれらを次のように使用します:

INFORMATIONS informations;
INFORMATION * informationArray = new INFORMATION[MAX_INFOS];
informations.info = informationArray;
int error = GetInformations(&informations);

JNAを使用してJavaでC++ライブラリを使用したいので、次のことを行いました。

public class Information extends Structure {
  public char[] infoMessage = new char[MAX];
  public Information () { super(); }
  protected List<? > getFieldOrder() {
    return Arrays.asList("infoMessage ");
  }
  public Information (char infoMessage []) {
    super();
    if ((infoMessage .length != this.infoMessage .length)) 
      throw new IllegalArgumentException("Wrong array size !");
    this.infoMessage  = infoMessage ;
  }

  public static class ByReference extends Information implements Structure.ByReference {};
  public static class ByValue extends Information implements Structure.ByValue {};
}

public class Informations extends Structure {
  public int infoCount;
  public Information.ByReference infoArray;
  public Informations () { super(); }
  protected List<? > getFieldOrder() {
    return Arrays.asList("infoCount", "infoArray");
  }
  public Informations(int infoCount, Information.ByReference infoArray) {
    super();
    this.infoCount= infoCount;
    this.infoArray= infoArray;
  }
  public static class ByReference extends Informations implements Structure.ByReference {};
  public static class ByValue extends Informations implements Structure.ByValue {};
}

私はこのようにライブラリを呼び出そうとしました:

Informations.ByReference informations = new Informations.ByReference();
informations.infoArray= new Information.ByReference();
int error = CLib.GetInformations(Informations);

Information[] test =(Information[])informations.infoArray.toArray(Informations.infoCount);

配列の最初の要素のみを取得することもありますが、残りの時間は Java がクラッシュします...したがって、Java サイトにメモリを割り当てていないことに関連していると思いますが、それ以上取得できません:/

4

1 に答える 1

2

Nativecharは Java に対応していますbyte

あなたの例では、サイズ 1 の配列を に渡していることに注意してくださいGetInformations

クラッシュの原因である可能性が非常に高いその誤ったマッピングは別として、マッピングは問題ないように見えます。

編集

渡す配列のサイズに初期化する必要がありinfoCountます(例では「1」)。より大きな配列を渡したい場合は、 を呼び出す.toArray()onを呼び出す必要があります。を呼び出すと、追加の配列要素用のメモリが割り当てられます。それまでは、単一の要素に割り当てられたメモリしかありません。 informations.infoArray GetInformations()Structure.toArray()

于 2013-10-18T20:12:02.767 に答える