我试图通过JNA在c ++ dll和我的java项目之间建立连接。 dll.h位于这个gitHub:dll.h。
EXTERN_C DLLEXPORT int STDCALL SolveBoard(
struct deal dl,
int target,
int solutions,
int mode,
struct futureTricks * futp,
int threadIndex);
struct deal
{
int trump;
int first;
int currentTrickSuit[3];
int currentTrickRank[3];
unsigned int remainCards[DDS_HANDS][DDS_SUITS];
};
struct futureTricks
{
int nodes;
int cards;
int suit[13];
int rank[13];
int equals[13];
int score[13];
};
我对JNA和c ++很新鲜,我尝试用Java编写代码:
public interface DllAl extends StdCallLibrary{
DllAl instanceDll = (DllAl)Native.loadLibrary("dds",DllAl.class);
public int SolveBoard(deal deal,int target,int solutions,int mode, futureTricks fut,int threadIndex);
public int PrintFut(char title[],futureTricks fut);
public static class deal extends Structure{
public int trump;
public int first;
public int currentTrickSuit[] = new int [3];
public int currentTrickRank[] = new int [3];
public int remainCards[][] = new int[4][4];
public static class ByReference extends deal implements Structure.ByReference {}
public static class ByValue extends deal implements Structure.ByValue {}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[]{"trump", "first", "currentTrickSuit", "currentTrickRank","remainCards"});
}
}
public static class futureTricks extends Structure{
public int nodes;
public int cards;
public int suit[] = new int [13];
public int rank[] = new int [13];
public int equals[] = new int[13];
public int score[] = new int[13];
public static class ByReference extends futureTricks implements Structure.ByReference {}
public static class ByValue extends futureTricks implements Structure.ByValue {}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[]{"nodes", "cards", "suit", "rank","equals","score"});
}
}
} 但是,它会返回错误:
Exception in thread "main" java.lang.NullPointerException
at java.lang.reflect.Array.getLength(Native Method)
at com.sun.jna.Native.getNativeSize(Native.java:1162)
at com.sun.jna.Structure.getNativeAlignment(Structure.java:1360)
at com.sun.jna.Structure.getNativeAlignment(Structure.java:1385)
at com.sun.jna.Structure.deriveLayout(Structure.java:1221)
at com.sun.jna.Structure.calculateSize(Structure.java:1053)
at com.sun.jna.Structure.allocateMemory(Structure.java:380)
at com.sun.jna.Structure.ensureAllocated(Structure.java:356)
at com.sun.jna.Structure.ensureAllocated(Structure.java:346)
at com.sun.jna.Structure.write(Structure.java:737)
at com.sun.jna.Structure.autoWrite(Structure.java:2047)
at com.sun.jna.Function.convertArgument(Function.java:512)
at com.sun.jna.Function.invoke(Function.java:305)
at com.sun.jna.Library$Handler.invoke(Library.java:236)
at com.sun.proxy.$Proxy0.SolveBoard(Unknown Source)
at DllAl.main(DllAl.java:98)
答案 0 :(得分:1)
JNA不会映射2D(或3D,4D等)数组。如果您需要映射2D数组you should replace it with a 1D array,然后编写自己的代码进行翻译。
您需要更改此行:
View view = new View(getContext());
GradientDrawable iconPlayShape = new GradientDrawable();
iconPlayShape.setShape(GradientDrawable.RECTANGLE);
iconPlayShape.setColor(Color.WHITE);
RotateDrawable iconPlayRotate = new RotateDrawable();
iconPlayRotate.setFromDegrees(45.0f);
iconPlayRotate.setToDegrees(45.0f);
iconPlayRotate.setPivotXRelative(true);
iconPlayRotate.setPivotYRelative(true);
iconPlayRotate.setPivotX(0.13f);
iconPlayRotate.setPivotY(-0.4f);
iconPlayRotate.setDrawable(iconPlayShape);
iconPlayRotate.setLevel(1);
view.setBackground(iconPlayRotate);
为:
public int remainCards[][] = new int[4][4];
然后添加一个方法,将2D坐标(例如,手,套装)转换为1D索引(例如,4 *指针+套装)。