功能未找到JNA + SiUSBXp

时间:2013-02-04 12:52:23

标签: java jna

我正在尝试使用来自SiLabs(siusbxp.dll)的USBXPRESS库的JNA,虽然基本功能正常,但SI_GetDeviceProductString函数存在问题。

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

当我尝试运行它时,我收到以下错误:

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

感觉这是C函数的默认参数的问题。我尝试使用int作为参数并尝试省略它,但这些都没有帮助。 我甚至没有到达SI_Write(HANDLE cyHandle,LPVOID lpBuffer,DWORD dwBytesToWrite,LPDWORD lpdwBytesWritten,OVERLAPPED * o = NULL);功能,这有望导致更多问题:)

可以请任何人建议我如何处理JNA中的默认函数参数?

更新:SI_Write函数以这种方式工作正常:

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

所以这个问题是由别的东西引起的,但它仍然存在。

1 个答案:

答案 0 :(得分:0)

我已经设法通过使用旧版本的SiUSBXp库来解决问题,该库可以在互联网上找到。 从SiLabs网站下载的较新版本表现得很奇怪 - 有时在Dependency Walker中可以看到SI_GetDeviceProductString函数,有时它不是,而较旧版本则可以。

相关问题