Search code examples
javac++dlljna

Use C++ DLL from Java with JNA


I try to use a DLL from C++ with JNA to communicate with a Fanuc numeric control, from a Java program but always get this error: Exception in thread "main" java.lang.Error: Invalid memory access

The specific C++ method that I tried to use is this https://www.inventcom.net/fanuc-focas-library/handle/cnc_allclibhndl3

FWLIBAPI short WINAPI cnc_allclibhndl3(const char *ipaddr, unsigned short port, long timeout, unsigned short *FlibHndl);

And in the declaration in Java I use this:

short cnc_allclibhndl3(String ipaddr, short port, NativeLong timeout, short FlibHndl);

I tried with different type mappings but always get the same error.

Can you tell me if this declaration is correct?

This is my last program:

import com.sun.jna.Library;
import com.sun.jna.Native;

public class JnaFanuc {

    public interface Fwlib32 extends Library {
        short cnc_allclibhndl3(String ipaddr, short port, long timeout, short FlibHndl);
    }

    public static void main(String[] args) {
        short p = 0;
        int handle = 0;
        short ret;

        Fwlib32 fwl = (Fwlib32) Native.load("Fwlib32", Fwlib32.class);

        ret = fwl.cnc_allclibhndl3("192.168.1.100", (short)8193, 10, p);

        System.out.println("cnc_allclibhndl3 Ret: " + ret);
        System.out.println("hndl: " + handle);
    }
}

EDIT AFTER Daniel Widdis RESPONSE.

Hello, I tried your solution and for the first time I work. The application responds with "ret = -16" which means "EW_SOCKET (-16) Socket error". It is normal, for not having any CNC on the other side. The problem appears when I connect a real CNC with real IP, then appears the same error that the first time.

This is my actual code:

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.ShortByReference;

public class TestJNA {

    public interface Fwlib32 extends Library {
        Fwlib32 INSTANCE = (Fwlib32) Native.load("Fwlib32", Fwlib32.class);
        short cnc_allclibhndl3(String ipaddr, short port, long timeout, ShortByReference FlibHndl);
}

    public static void main(String[] args) {
        ShortByReference handle = new ShortByReference((short)0);

        short ret = 0;

        Fwlib32 fwl = Fwlib32.INSTANCE;
        ret = fwl.cnc_allclibhndl3("192.168.1.100", (short) 8193, 4, handle);

        System.out.println("cnc_allclibhndl3 Ret: " + ret);
        System.out.println("hndl: " + handle.getValue());
    }
}

And this is the error:

Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:422)
at com.sun.jna.Function.invoke(Function.java:361)
at com.sun.jna.Library$Handler.invoke(Library.java:265)
at com.sun.proxy.$Proxy0.cnc_allclibhndl3(Unknown Source)
at testjna.TestJNA.main(TestJNA.java:38)

Solution

  • The last argument in the cnc_allclibhndl3() function is a pointer to a short:

    unsigned short *FlibHndl
    

    So the proper mapping of that should be a ShortByReference. That will initialize a pointer to a short elsewhere in memory. Currently you're passing a null (0) pointer and asking the native method to access that memory!

    Also, you need the C long variable mapped to NativeLong in Java rather than long. Java long is always 64 bit, but C long varies based on OS and bitness. In Windows, that's actually always 32-bit so if your code is Windows-only you could even use int, but in general/cross-platform you should be using NativeLong for that mapping.

    Additionally the convention is for the Native.load() call to be in the interface as a static INSTANCE variable, rather than defining it inline as you've done.

    Try this:

    public class JnaFanuc {
    
        public interface Fwlib32 extends Library {
            Fwlib32 INSTANCE = (Fwlib32) Native.load("Fwlib32", Fwlib32.class);
    
            short cnc_allclibhndl3(String ipaddr, short port, NativeLong timeout, short FlibHndl);
        }
    
        public static void main(String[] args) {
            ShortByReference handle = new ShortByReference();
            short ret;
            String ip = "192.168.1.100";
            short port = (short) 8193;
            NativeLong timeout = new NativeLong(10);
            Fwlib32 fwl = Fwlib32.INSTANCE;
    
            ret = fwl.cnc_allclibhndl3(ip, port, timeout, handle);
    
            System.out.println("cnc_allclibhndl3 Ret: " + ret);
            System.out.println("hndl: " + handle.getValue());
        }
    }