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;
    }

}

 

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值