我尝试使用带有JNA的C ++中的DLL与Java程序中的Fanuc数字控件进行通信,但始终会收到此错误:线程“ main” java.lang.Error中的异常:无效的内存访问
我尝试使用的特定C ++方法是这个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);
在Java的声明中,我使用以下代码:
short cnc_allclibhndl3(String ipaddr, short port, NativeLong timeout, short FlibHndl);
我尝试使用不同的类型映射,但始终会遇到相同的错误。
你能告诉我这个声明是否正确吗?
这是我的最后一个程序:
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);
}
}
丹尼尔·维迪斯回应后编辑。
您好,我尝试了您的解决方案,这是我第一次工作。应用程序以“ ret = -16”响应,这意味着“ EW_SOCKET(-16)套接字错误”。这是正常的,因为另一侧没有任何CNC。当我用真实IP连接真实CNC时出现问题,然后出现与第一次相同的错误。
这是我的实际代码:
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());
}
}
这是错误:
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)
函数中的最后一个参数cnc_allclibhndl3()
是指向的指针short
:
unsigned short *FlibHndl
因此,正确的映射应该是ShortByReference
。这将初始化一个指向short
内存中其他位置的指针。当前,您正在传递一个空(0)指针,并要求本机方法访问该内存!
另外,您需要使用Java而不是long
映射到C 变量。Java long始终为64位,但是C 随操作系统和位的不同而不同。在Windows中,实际上始终是32位的,因此,如果您的代码仅是Windows,则甚至可以使用,但是在一般/跨平台中,应使用该映射。NativeLong
long
long
int
NativeLong
另外,约定是Native.load()
在接口中将调用作为静态INSTANCE
变量进行,而不是像您已经完成的那样内联定义。
试试这个:
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());
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句