B4J Question JNA : access C++ DLL methods & variables type conversion

Discussion in 'B4J Questions' started by GabrielM, Oct 9, 2019 at 2:51 PM.

  1. GabrielM

    GabrielM Member Licensed User


    Happened to need to call some methods from C++ generated Windows DLL (I do not have access to its source, but methods prototype are available). Looking around I have tried to use JNA examples.
    I have compiled with SLC the library though when I try to use the method in B4J I get :

    java.lang.Error: Invalid memory access
    The C++ method prototype is:

    C++ method :
    unsigned short ASTDiscoverDevice(int &DeviceNum, char deviceName[][
    unsigned char* deviceType);
    and I though to have the JNA side method prototype like:

    public int ASTDiscoverDevice(int DeviceNum, byte[] deviceName, byte[] deviceType);
    The SDK I can not post as it is licensed, but I can attach the attempted JNA code as I have it so far. I do not have experience with JNA or Java and will appreciate any help here or private.


    Attached Files:

  2. rboeck

    rboeck Well-Known Member Licensed User

    You got two mistakes in your paramters. The vers first parameter of ASTDiscoverDevice is not of type int but a reference to type (int&, notice the & before the paramtere name DeviceNum). I have little experience with JNA but it seems like you need to use IntByReference instead of int. Second parameter you got wrong is deviceName. The C++ API itsself is not very nice admittedly as this is actually an array of unkown size of an array of fixed size of 17 of type char. This is either mappable using an array of strings, so String[], or char[][]. Hope this helps!
    Erel, GabrielM and DonManfred like this.
  3. GabrielM

    GabrielM Member Licensed User

    ... and here is the B4J code I have used to test the above JNA library.

    Attached Files:

  4. GabrielM

    GabrielM Member Licensed User

    Thanks rboek

    Have added/replaced as below and it seems I got no more Memory errors like before.
    Probably I should use the same ByRef for the other two array parameters.

    import com.sun.jna.ptr.IntByReference;

    IntByReference DeviceNum = new IntByReference();

    public int ASTDiscoverDevice(IntByReference DeviceNum, char[] deviceName, char[] deviceType);
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice