Search code examples
javacjna

JNA - VM crashes when try to access to a C function with void* param


I have the following code :

public class TTM {

public interface WrapperDLL extends Library {
    WrapperDLL INSTANCE = (WrapperDLL) Native.loadLibrary("TransportRE", WrapperDLL.class);

    int TRE_send(int channel, Pointer data, int len);

}
public int Send (int channel, String data, int len) {
    WrapperDLL wdll = WrapperDLL.INSTANCE;

    Memory mem = new Memory(data.length()+1);
    mem.setString(0, data);
    int byteSent = wdll.TRE_send(channel_id, mem.getPointer(0), len);
    // at this (TRE_send) point the VM crashes !!!!

  return byteSent;
}

}

taking a look at the JNA api I tried :

public int Send (int channel, String data, int len) {
    WrapperDLL wdll = WrapperDLL.INSTANCE;

    Memory mem = new Memory(data.length()+1);
    mem.setString(0, data);
    int byteSent = wdll.TRE_send(channel_id, mem, len);

  return byteSent;
}

This time does not crashes but it not works properly (byteSent = 0!)

Some hint ?


Solution

  • I´ve fix it !

    int byteSent = wdll.TRE_send(channel_id, data.toCharArray(), len);
    

    since the 2nd parameter is a void* but in the dll code the first thing is to cast it to char * I used the same type and now it works.

    for sure this is not a "general solution" since the "TRE_send" want a void* type ... what do you think ?

    bye