JNative中给DLL传入数组


// 调用DLL,取出数组中的最大值
public voic fnGetMax(int[] arrInt) {

	try
	...
	...

	// 创建JNative对象
	JNative jnative = JNative("test.dll","fnGetMax");

	// 为数组创建空间
	Pointer aArrIntInput = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * arrInt.length));

	// 初始化数组
	for (int i = 0; i < arrInt.length; i++) {
		aArrIntInput.setIntAt(4 * i, arrInt[i]);
	}

	// 设定传入参数
	jnative.setParameter(0, pArrIntInput); // 数组指针
	jnative.setParameter(1, arrInt.length); // 数组大小

	// 设定返回类型
	jnative.setRetVal(Type.INT);

	// 调用DLL
	jnative.invoke();

	// 打印返回值
	int iRet = jnative.getRetValAsInt();
	System.out.println(iRet);

	catch
	...
	...
}


double的场合
// 传入和返回类型是double
double dParam = 10.23;
...
...
jnative.setParameter(0, Type.DOUBLE, Stirng.valueOf(dParam));
...
jnative.setRetVal(Type.DOUBLE);
...
String dRet = jnative.getRetVal();
System.out.println(dRet);

你可能感兴趣的:(参数,数组,dll,jnative)