我有以下 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 站点上分配内存有关,但我无法进一步了解:/