JNA Profile

JNA

JNA (Java Native Access) provides a set of Java tools for dynamic access to the system run the local library (native library: Window as the dll) without the need to write any Native / JNI code. Developers to describe a native library functions and structure of the target in a Java interface, JNA automatically implement Java interfaces to native function mapping.

  • Advantages: JNA allows you to call a general java like the same way as direct calls to native methods. And direct execution on the local method is almost, but also call the local method without additional processing or any other configuration and does not require extra coding or references, very easy to use.
  • Drawback: JNA JNI is based on existing ones, so the efficiency will be lower than JNI.

Key Code

import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
public class LYTest {
    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary)Native.loadLibrary("ly_icparse",CLibrary.class);
        int Parse(String databuf,IntByReference ickh,IntByReference quantity,IntByReference fc,Pointer cid);
        int Build(int ickh, int quantity, int fc, String cid, Pointer databuf);
    }
    public static void main(String[] args) throws Exception {
        //用于接收输出的char*
        Pointer databuf = new Memory(512);  
        CLibrary.INSTANCE.Build(20133058, 11, 3, "201013000285", databuf);
        byte[] byteArray = databuf.getByteArray(0, 512);
        String data = new String(byteArray,"UTF-8");
        System.out.println("data:"+data);
        //构建读卡数据
        String databufstr = "A2131091FFFF8115FFFF201013000285FFFFFFFFFFD27600000400FFFFFFFFFF"+data.substring(64,512);

        IntByReference ickh = new IntByReference();
        IntByReference quantity = new IntByReference();
        IntByReference fc = new IntByReference();
        Pointer cid = new Memory(12);
        int result = CLibrary.INSTANCE.Parse(databufstr, ickh, quantity, fc, cid);
        String cidstr =  new String(cid.getByteArray(0, 12),"UTF-8");
        System.out.println("ickh:"+ickh.getValue());
        System.out.println("quantity:"+quantity.getValue());
        System.out.println("fc:"+fc.getValue());
        System.out.println("cid:"+cidstr);
        System.out.println("result:"+result);
    }
}

Explanation

C common to the correspondence relationship parameter java

c parameters java parameters Explanation
int* IntByReference A reference, directly into the reference int
char* Pointer/Memory A reference, directly into the reference String

char * string corresponding to a need to know the length of time used in obtaining the reference content.





Accessories list

 

Reproduced in: https: //my.oschina.net/MeiJianMing/blog/1812014

Guess you like

Origin blog.csdn.net/weixin_33748818/article/details/92546507