JNA communication to Native Code
I have this native function and I get the null value in JNA when I attach device to my system I think I have problem in LPVOID maping with JNA any Idea will be appreciated.
CP210x_GetProductString( DWORD DeviceNum,LPVOID DeviceString,DWORD Options)
DeviceNum
— Index of the device for which the product description string, serial number, or full path is desired.DeviceString
— Variable of typeCP210x_DEVICE_STRING
returning the NULL-terminated serial number, device description or full path string.Options
— Flag that determines ifDeviceString
contains the product description, serial number,开发者_如何转开发 or full-path string
JNA code:
public class Helloworld {
public interface CLibrary extends Library{
CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
(Platform.isWindows() ? "CP210xManufacturing.dll" : "c"),
CLibrary.class);
int CP210x_GetProductString(int dn,String [] ds,int op);
}
public static void main(String[] args) {
int dn=0;
String dsc = new String[100];
if(CLibrary.INSTANCE.CP210x_GetProductString(dn, dsc,
CP210x.CP210x_RETURN_SERIAL_NUMBER) == CP210x.CP210x_SUCCESS){
{
for(int i=0;i<dsc.length;i++)
System.out.print(dsc[i]);
}
}
}
}
Don't know if this will work, but from this description of CP210x_GetProductString I think you have to pass a byte[] with length 256 to the function (the buffer to fill with the String).
int CP210x_GetProductString(int dn,byte[] ds,int op);
//use it like this
byte[] rawString = new byte[256];
int dn = ...;
int op = ...;
CP210x_GetProductString(dn,rawString,op);
//You might have to choose a different Charset here
//since I don't know what encoding the string uses I used the default
String product = new String(rawString,Charset.defaultCharset());
精彩评论