這是我第一次使用JNA。我想要做的是調用一個DLL中的函數,它需要(C代碼) - >(無符號長DeviceTypes,NeoDevice * pNeoDevice,int * pNumDevices)...(JNA形式) - > (int,NeoDevice.getPointer (),int [] myInt) as params。函數應寫入結構的字段,我想查看更新的字段。JNA updateStructureByReference()麻煩
這裏是我的JNA結構「NeoDevice」
import java.util.Arrays;
import java.util.List;
import com.sun.jna.*;
public class NeoDevice extends Structure {
public volatile int DeviceType;
public volatile int Handle;
public volatile int NumberOfClients;
public volatile int SerialNumber;
public volatile int MaxAllowedClients;
public NeoDevice() {
super();
}
protected List<? > getFieldOrder() {
return Arrays.asList("DeviceType", "Handle", "NumberOfClients", "SerialNumber", "MaxAllowedClients");
}
public NeoDevice(int DeviceType, int Handle, int NumberOfClients, int SerialNumber, int MaxAllowedClients) {
super();
this.DeviceType = DeviceType;
this.Handle = Handle;
this.NumberOfClients = NumberOfClients;
this.SerialNumber = SerialNumber;
this.MaxAllowedClients = MaxAllowedClients;
}
protected ByReference newByReference() { return new ByReference(); }
protected ByValue newByValue() { return new ByValue(); }
protected NeoDevice newInstance() { return new NeoDevice(); }
public static class ByReference extends NeoDevice implements Structure.ByReference {
};
public static class ByValue extends NeoDevice implements Structure.ByValue {
};
}
我試圖用「updateStrucureByReference(類類型,對象,指針對象)」更新的字段。我不相信我的'類類型'參數是正確的還是它?我在做別的事情嗎?任何投入將不勝感激。
當我試圖println他們看起來都是零仍然是領域。
在我的主類,我有
NeoDevice.ByReference myDeviceByRef = new NeoDevice.ByReference();
NeoDevice.ByValue myDeviceByVal = new NeoDevice.ByValue();
NeoDevice myDevice = new NeoDevice();
int [] NumDevices;
NumDevices = new int [1];
NumDevices[0] = 1;
int iResult = n40.icsneoFindNeoDevices(65535, myDeviceByRef.getPointer(), NumDevices);
int icsneoGetDLLVersion = n40.icsneoGetDLLVersion();
Object serialN = myDeviceByRef.readField("SerialNumber");
NeoDevice.ByReference myDeviceBy = Structure.updateStructureByReference(NeoDevice.ByReference, myDeviceByRef, myDeviceByRef.getPointer());
感謝您的意見!我只對讀取BACK值有興趣。 沒有Structure.updateStructureByReference,在我調用該函數並嘗試查看字段值後,它們都是零。 我目前與結構.read()和結構.readField()爲了查看字段,但仍然有問題鬼混 –
我沒有訪問的源代碼來修改參數,所以我被迫通過NeoDevice作爲指針 –
只需在函數調用後刪除'volatile'並調用'Structure.read()'。 – technomage