admin管理员组

文章数量:1794759

Java用JNA调用dll : Invalid memory access

Java用JNA调用dll : Invalid memory access

问题描述 Java通过JNA调用C/C++ dll时,报Invalid memory access 问题原因 经过分析原因是数据类型不匹配问题 int &a 和 a 的区别 

C语言中的a是一个变量,储存着值。&a是常量,是变量a的内存地址。一般的&a是用来赋值给指针的(int *p=&a ;),或者是作为函数的参数传递(地址传递)

在java中对应&a 指针地址的引用变量为 com.sun.jna.ptr.IntByReference 或者 byte[]

代码实现

将dll 放到resources 下面

引入的 JNA 的 Maven配置

<dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>5.8.0</version> </dependency>

JAVA代码实现 

package com.example.demo; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.ptr.IntByReference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * jvm 64位程序执行64位dll * jvm 32位程序执行32位dll */ @RestController @RequestMapping("/test") public class AddController { public interface AddLib extends Library { AddLib instance = Native.load("lib64/test", AddLib.class); //int Add1(byte[] a,byte[] b); // 对应C 语言程序 int Add1(int &a,int &b) //int Add1(Object a, Object b);// 对应C 语言程序 int Add1(int &a,int &b) int Add1(IntByReference a, IntByReference b);// 对应C 语言程序 int Add1(int &a,int &b) int Add2(int a, int b); // 对应C 语言程序 int Add1(int a,int b) } @GetMapping("/add1") public static int Add1() { Integer a = new Integer(1258); Integer b = new Integer(2); return AddLib.instance.Add1(new IntByReference(a), new IntByReference(b)); // return AddLib.instance.Add1(toLH(a),toLH(b)); } @GetMapping("/add2") public int Add2() { return AddLib.instance.Add2(2, 3); } @GetMapping() public String st() { return "hello test add"; } public static void main(String[] args) { int d = AddLib.instance.Add2(112, 22); System.out.println("=============" + d); d = Add1(); System.out.println(d); } public static byte[] toLH(int n) { byte[] b = new byte[4]; b[0] = (byte) (n & 0xff); b[1] = (byte) (n >> 8 & 0xff); b[2] = (byte) (n >> 16 & 0xff); b[3] = (byte) (n >> 24 & 0xff); return b; } public static byte[] toHH(int n) { byte[] b = new byte[4]; b[3] = (byte) (n & 0xff); b[2] = (byte) (n >> 8 & 0xff); b[1] = (byte) (n >> 16 & 0xff); b[0] = (byte) (n >> 24 & 0xff); return b; } }

 

本文标签: DLLJNAjavaAccessmemory