0
傳遞結構我想調用此函數:如何通過參考與JNA
extern "C" xxx_SERVICE_API int initSocket(socketStruct* skStruct);
在C定義
++ dll.socketStruct是如下所定義的結構:
typedef struct
{
int a;
int b;
} mystruct;
在Java等效代碼是:
public interface MyDllClass extends Library {
public static class MyStructure extends Structure {
public static class ByReference extends MyStructureRef
implements Structure.ByReference{
}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[]{"a","b"});
}
public int a;
public int b;
}
//Native function
int initSocket (MyStructure.ByReference sks);
MyDllClass INSTANCE = (MyDllClass)Native.loadLibrary("Mydll",
MyDllClass.class);
}
我怎樣才能調用這個本機fucntion與參考結構作爲參數? 謝謝
謝謝你的回答。你根據我一個構造器添加到我的結構:
public interface MyDllClass extends Library {
public static class MyStructure extends Structure {
public MyStructure() {
idModule = 0;
nbModules = 0;
nbQueueInModule = 3;
portList = new int[128];
serverList = new char[128][20];
}
@Override
protected List<String> getFieldOrder() {
return Arrays.asList(new String[]{"a","b","portList","serverList"});
}
public int a;
public int b;
public int[] portList;
public char[][] serverList;
}
//Native function
int initSocket (MyStructure.ByReference sks);
MyDllClass INSTANCE = (MyDllClass)Native.loadLibrary("Mydll",
MyDllClass.class);
}
在主我寫了這個代碼:
MyStructure mySocket = new MyStructure();
MyDllClass.INSTANCE.initSocket(mySocket);
此代碼調用本機功能時,因爲空指針異常的失敗。 你有想法嗎?