java jna调用c dll,结合使用Java和JNA中的C ++ DLL

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

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)

解决方案

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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值