1

我试过了,失败了!首先向您展示我试图模仿的 C++ 代码,然后是我想出的 JNA Java 代码(其中一半有效)。

C++ 代码

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 代码

结构实现...

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 {

};
}

主类代码...

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();

没有参数的函数工作正常并返回它应该的整数。另一个返回它应该返回的“1”,但问题是它也应该写入我传递它的结构的字段,但是当我查看它的字段时,所有字段仍然为零。

4

0 回答 0