我想要將一個指向C結構的指針轉換爲jna結構。我使用jna從dll接收回調函數,該函數有一個參數,該參數是指向C結構的指針,當我嘗試將指針轉換爲jna結構時,我得到錯誤的結構值。如何將一個指向c結構的指針轉換爲jna結構
這是C結構:
typedef struct
{
int x;
int y;
}Point;
Point *gpt;
typedef struct
{
int x;
int y;
Point pt1;
}Point2;
Point2 *gpt2;
也就是說在C回調函數的指針(void *的PARAMS),以點2 sctruct:
void __stdcall PointCallback(void *params, int param_size)
所以,我做了這個java中的代碼接收回調並獲得原始結構:
// Point.java
package Callback.UsePointLib;
import com.sun.jna.Structure;
public class Point extends Structure
{
public static class ByValue extends Point implements Structure.ByValue {}
public int x;
public int y;
}
//Point2.java
package Callback.UsePointLib;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
public class Point2 extends Structure {
public int x;
public int y;
Point pt1;
public Point2(Pointer p){
super(p);
}
}
回調執行:
//UsePointLib.java
public interface IFuncCallback extends StdCallCallback{
void callback(Pointer Params, int ParamSize);
}
public class FuncCallback implements IFuncCallback{
@Override
public void callback(Pointer Params, int ParamSize) {
Point2 pt2; // = new Point2();
pt2 = new Point2(Params);
System.out.println("pt2.x = "+pt2.x); **<- I get zero here instead of four**
System.out.println("pt2.y = "+pt2.y); **<- I get zero here instead of five**
System.out.println("pt2.pt1.x = "+pt2.pt1.x);**<- pt1 is null, throwing exception**
System.out.println("pt2.pt1.y = "+pt2.pt1.y);**<- same as pt1.**
}
}
我做了一個C程序來訪問DLL並接收回調,它工作正常,它收到正確的值。所以,問題是我的java代碼。我嘗試了許多替代品,但都沒有成功。
請,我會很感激任何幫助。
謝謝,
費爾南多。
編輯
我已經修改了代碼,它的工作原理部分。
//UsePointLib.java
public interface IFuncCallback extends StdCallCallback{
void callback(Pointer Params, int ParamSize);
}
public class FuncCallback implements IFuncCallback{
@Override
public void callback(Pointer Params, int ParamSize) {
Point2 pt2; // = new Point2();
pt2 = new Point2(Params);
*pt2.read();* **<--Modification**
System.out.println("pt2.x = "+pt2.x); **<- I get the correct value (four)**
System.out.println("pt2.y = "+pt2.y); **<- I get the correct value (five)**
System.out.println("pt2.pt1.x = "+pt2.pt1.x);**<- pt1 is still null, throwing exception**
System.out.println("pt2.pt1.y = "+pt2.pt1.y);**<- same as pt1.**
}
}
嗨阿布吉斯,謝謝你的幫助。我閱讀jna文件,我知道,但是,我要做的是將數據從dll中的結構複製到java內存中。 – Fernando 2011-06-16 14:19:39
這是一個CTI應用程序,dll由cti卡供應商提供,回調是從卡上接收事件(響鈴,呼叫應答等)。所以,我把回調放在一個線程中,我使用消費者/生產者將事件數據通過隊列傳遞給另一個線程。這是因爲我需要將收到的指針所指向的內容複製到本地內存。我將編輯我的帖子以提供一個可能的解決方案。 – Fernando 2011-06-16 14:35:51