简体   繁体   中英

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

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