JNA DLL Function Call implementation

不问归期 提交于 2019-12-11 04:43:06

问题


I have tried and failed! first ill show you the c++ code im trying to imitate then the JNA java code i came up with (half of it works).

C++ Code

typedef struct 
{
    int DeviceType;
    int Handle;
    int NumberOfClients;
    int SerialNumber;
    int MaxAllowedClients;
} NeoDevice;

typedef int  (__stdcall *FINDNEODEVICES)(unsigned long DeviceTypes, NeoDevice *pNeoDevice, int *pNumDevices);

extern FINDNEODEVICES icsneoFindNeoDevices;

bool LoadDLLAPI(HINSTANCE &hAPIDLL){
     icsneoFindNeoDevices =    (FINDNEODEVICES) GetProcAddress(hAPIDLL, "icsneoFindNeoDevices");
}

Java Code

struct implementation...

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 {

};
}

main class code...

System.setProperty("jna.library.path", "C:\\WINDOWS\\system32");
icsneo40 n40 = icsneo40.INSTANCE;
NeoDevice.ByReference myDeviceByRef = new NeoDevice.ByReference();
int icsneoGetDLLVersion = n40.icsneoGetDLLVersion();
int iResult = n40.icsneoFindNeoDevices(65535, myDeviceByRef.getPointer(), NumDevices);
myDeviceByRef.read();

the function without params works fine and returns the integer it is supposed to. the other one returns '1' which its supposed to but the problem is its also supposed to write to the fields of the struct i pass it but when i look at its fields, all are still zero.

来源:https://stackoverflow.com/questions/17353245/jna-dll-function-call-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!