JNA调用C++动态库,返回二维数组

1.下载eclipse创建一个项目,jdk选择1.8的版本,更高的需要配置下

package mydemo;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.Pointer;
import com.sun.jna.Memory;


public class Hello {
	
	public interface LgetLib extends Library {
		// 调用linux下面的so文件,注意,这里只要写test就可以了,不要写libtest,也不要加后缀
		LgetLib INSTANCE = (LgetLib) Native.loadLibrary("EasyLog", LgetLib.class);
	
		public int test(Pointer arr, int row, int col);

		public int get2Darr(Pointer pppFloatOut, IntByReference pLine, IntByReference pColumn);
		//public void free2Dppt(int line, int column, Pointer ppFloat);
		}
	

	
	public int test(){            
            
// 传入二维数组            
//            Pointer pointers = new Memory(24);
//            int pointer_size = 8;
//            int int_size = 4;
//            int row = 3;
//            int col = 5;
//            int n = 0;
//            for (int i = 0; i < row; i++) {
//                //分配20字节的内存空间,并且该指针指向这个内存空间的首地址
//                Pointer pointer = new Memory(20);
//                for (int j = 0; j < col; j++) {
//                    pointer.setInt(j * int_size, n);
//                    n += 1;
//                }
//                pointers.setPointer(i * pointer_size, pointer);
//                //这里不要释放内存
                Native.free(Pointer.nativeValue(pointer));
//            }
//            //调用Dll动态链接库内的getArrSum函数
//            System.out.println(LgetLib.INSTANCE.test(pointers, row, col));
//            System.out.println("释放内存之前,pointers地址:"+pointers);
//            Native.free(Pointer.nativeValue(pointers));
//            Pointer.nativeValue(pointers, 0);
		
//传出二维数组
		
		//调用
		PointerByReference ppFloat = new PointerByReference();
		Pointer pppFloat = ppFloat.getPointer();
		IntByReference linePt = new IntByReference();
		IntByReference columnPt = new IntByReference();
		//通过获取二维指针的指针,得到三重指针
		LgetLib.INSTANCE.get2Darr(pppFloat, linePt, columnPt);
		int line = linePt.getValue();
		int column = columnPt.getValue();
		System.out.println("line:"+line+",column:"+column);
		double[][] values = new double[line][column];
		//输出一个3行5列的二维数组,即3个指向一行的指针,因此可获取指针数组;
		// 注意getPointerArray方法必须指明指针数组的数量(此处是line = 3),否则返回的指针数组长度不定,有很多干扰值
		Pointer[] pointers = ppFloat.getValue().getPointerArray(0, line);
		for(int i=0;i

动态库编译后放到system32下,system下是64位的,SysWow64是32位的

你可能感兴趣的:(C++,c++,java,开发语言)