進出口試圖建立一個可以從Java經由JNA可以使用C++庫。我正在做一些測試來返回一個Structs數組。我創建了以下結構:返回陣列的第一個結構是空的
C++:
namespace structures
{
typedef struct _Point {
int x, y;
} Point;
}
的Java:
public class Point extends Structure {
public Point() {
// required for toArray()
}
public Point(Pointer pointer) {
super(pointer);
}
public int x, y;
}
所以,我有一個C++方法返回點的數組:
extern "C" __declspec(dllexport) structures::Point* getPoints(){
structures::Point *p;
p = (structures::Point *) malloc(4 * sizeof(structures::Point));
p[0].x = 10;
p[0].y = 20;
p[1].x = 30;
p[1].y = 40;
p[2].x = 50;
p[2].y = 60;
p[3].x = 70;
p[3].y = 80;
return p ;
而且在Java中,這是聲明庫訪問接口:
public interface Multiplyt extends Library{
Pointer getPoints();
}
這是測試庫中的代碼:
Pointer pointer = test.getPoints();
Point point, points[];
point = new Point(pointer);
points = (Point[]) point.toArray(4);
System.out.println("0x:"+points[0].x + " 0y:" + points[0].y + " 1x:"+ points[1].x + " 1y:" + points[1].y);
System.out.println("2x:"+points[2].x + " 2y:" + points[2].y + " 3x:"+points[3].x + " 3y:" + points[3].y);
這些命令的輸出是:
0x:0 0y:0 1x:30 1y:40
2x:50 2y:60 3x:70 3y:80
因此,大家可以看到,在位置點結構0有沒有x或y的值(它有0和0,它應該是10和20)。這是爲什麼發生?我該如何糾正它?
謝謝。在調用數組之後調用「.read()」點也使得avobe代碼有效。我還嘗試在Java中聲明接口以返回一個Struct Point,使C++代碼保持不變,並且它也可以在不需要調用point.read()的情況下工作。 – 2012-03-14 08:15:16
在這種情況下,在調用.toArray()之前,應該在Structure(指針)構造函數中調用Structure.read()。一旦JNA知道結構是數組的第一個元素,只要Structure.read()/。write()作爲結構自動讀/寫功能的一部分被調用,它就會自動讀取和寫入其餘元素。因此,您應該在構造函數中或在調用toArray()之前執行讀取以避免多餘的讀取。 – technomage 2012-03-14 11:14:43