简体   繁体   中英

Function not found JNA + SiUSBXp

I'm trying to use JNA with USBXPRESS library from SiLabs (siusbxp.dll), and while basic functions work fine, there is a problem with SI_GetDeviceProductString function.

public class usbxpress 
{

public interface SiUSBXp extends StdCallLibrary 
{
SiUSBXp INSTANCE = (SiUSBXp) Native.loadLibrary("SiUSBXp", SiUSBXp.class);

byte SI_GetNumDevices (IntByReference lpdwNumDevices);
byte SI_GetProductString( int dwDeviceNum, byte[] lpvDeviceString, int dwFlags);
byte SI_Open (int dwDevice, HANDLEByReference cyHandle);
byte SI_GetPartNumber (HANDLE cyHandle, ByteByReference lpbPartNum);        
byte SI_GetDeviceProductString (HANDLE cyHandle, PointerByReference lpProduct, ByteByReference lpbLength, int bConvertToASCII);
//byte SI_GetDeviceProductString (HANDLE cyHandle, LPVOID lpProduct, LPBYTE lpbLength, BOOL bConvertToASCII = TRUE); //original c function
}

public static void main(String[] args) 
{
//checking number of connected devices  
IntByReference lpdwNumDevices = new IntByReference();
SiUSBXp.INSTANCE.SI_GetNumDevices (lpdwNumDevices);        
System.out.println(lpdwNumDevices.getValue());

//opening the device
HANDLEByReference dev_handle_ref = new HANDLEByReference();
byte status = SiUSBXp.INSTANCE.SI_Open(0, dev_handle_ref);
System.out.printf("Status %d\n", status);

HANDLE device_handle = dev_handle_ref.getValue();

//checking part number
ByteByReference lpbPartNum = new ByteByReference();
SiUSBXp.INSTANCE.SI_GetPartNumber(device_handle, lpbPartNum);
System.out.printf("Part number is CP210%d\n", lpbPartNum.getValue());      

//checking product string - does not work
PointerByReference lpProduct = new PointerByReference();
ByteByReference lpbLength = new ByteByReference();
SiUSBXp.INSTANCE.SI_GetDeviceProductString(device_handle, lpProduct, lpbLength, 1);
}}

when I try to run it I get the following error:

> Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'SI_GetDeviceProductString': at com.sun.jna.Function.<init>(Function.java:179) 
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:391) 
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:371) 
at com.sun.jna.Library$Handler.invoke(Library.java:205) 
at $Proxy0.SI_GetDeviceProductString(Unknown Source)

It feels like it's the problem with the default argument of the C-function. I tried to use int as the argument and tried omitting it, but none of these helped. I didn't even reach the SI_Write (HANDLE cyHandle, LPVOID lpBuffer, DWORD dwBytesToWrite, LPDWORD lpdwBytesWritten, OVERLAPPED* o = NULL); function, which is promising to cause even more problems:)

Could please anyone suggest how can I deal with the default function arguments in JNA?

Update: SI_Write function works fine this way:

byte SI_Write (HANDLE cyHandle, PointerByReference lpBuffer, int dwBytesToWrite, IntByReference lpdwBytesWritten, Pointer o);
...
SiUSBXp.INSTANCE.SI_Write (device_handle, lpBuffer, message.length, lpdwBytesWritten, null);

So the problem is caused by something else, but still it exists.

I've managed to solve the problem by using an older version of the SiUSBXp library, which was found on the internet. Newer version downloaded from SiLabs website is acting weird - sometimes SI_GetDeviceProductString function is visible in Dependency Walker and sometimes it's not, while older one is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM