简体   繁体   中英

why am I not getting any output when a method with a callback is called?

I am trying to call a method which is in a C DLL through Java. But I am not getting any output nor any exceptions. Below is my C code.

typedef void (*rfid_data)(
    uint32_t data_id,
    uint32_t data_length,
    char *data  );


RFIDLIB_API uint32_t init(
    char *csvParams                 /*[in]*/,
    uint32_t length                 /*[in]*/,
    rfid_data callbackFunction);

Shown below is my JAva Interface related to the callback.

public interface rfid_data extends Callback{

public void callback(int dataId,int dataLength,Pointer data);

}

Below is my library interface.

public interface rfidlib extends Library{

public int init(Pointer csvParams,int length,rfid_data callbackFunction);

}

Shown below is my main method from which I am calling the C code.

public class MainClass {

public rfidlib rlib;

public static void main(String[] args) {
    MainClass mainClass = new MainClass();

    mainClass.rlib = (rfidlib) Native.loadLibrary("rfidlib", rfidlib.class);
    rfid_data rfidData = new rfid_data() {
        @Override
        public void callback(int dataId, int dataLength, Pointer data) {
            byte[] buffer = data.getByteArray(0,dataLength);
            System.out.println("Data Id is : " + dataId + "Data Length is : " + dataLength + " data is : " + new String(buffer));

        }
    };
    String comPort = "COM4";
    Pointer m = new Memory(comPort.length()+1);
    m.setString(); 
    int result1 = mainClass.rlib.init(m, 100, rfidData);
    System.out.println("result is : "+result1);


}

When I run the code nothing gets printed. Not even the string "result is". What am I doing wrong here? Please advice.

char* implies Pointer , not String . Your callback is receiving pointer to data, presumably an array of bytes. Unless your documentation says otherwise, you shouldn't assume that the incoming data is a NUL-terminated C string (since the length is being passed in, I highly doubt it's a printable string). If you are certain that it is a C string, then use the function Pointer.getString(0) to convert it to a Java String.

public interface rfid_data extends Callback{
    public void callback(int dataId, int dataLength, Pointer data);
}

rfid_data cb = new rfid_data() {
    public void callback(int dataId, int dataLength, Pointer data) {
        byte[] buffer = data.getByteArray(0, dataLength);
        // Alternatively, if the incoming data is a NUL-terminated C string
        String s = data.getString(0);
        // ...
    }
};

EDIT

If the RFIDLIB_API macro contains stdcall (which is likely), then your library should also implement StdCallLibrary and your callback StdCallCallback .

The most up-to-date documentation for JNA may be found at the github project .

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