开发者

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)
  1. DeviceNum — Index of the device for which the product description string, serial number, or full path is desired.
  2. DeviceString — Variable of type CP210x_DEVICE_STRING returning the NULL-terminated serial number, device description or full path string.
  3. Options — Flag that determines if DeviceString 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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜