天天看點

java驅動dll檔案

由于項目需要,要驅動dll檔案從平台中取值,所有做了一個java類讀取dll檔案,執行函數

package com.stm.dll;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;

/**
 * 執行此函數需要把JNative.jar中的lib-bin\JNativeCpp.dll檔案拷貝到C:\Windows\System32下;
 * 并且把zjnet.dll檔案也拷貝到C:\Windows\System32下
 * @author LOVCC
 * @date 2011/12/28
 *
 */
public class JavaCallsDLL {

	//系統調用的dll檔案名
	private static final String dllName = "zjnet.dll" ;
	
	//dll檔案中定義的初始化函數
	private static final String initFunName = "InitSocketEnvironment" ;
	
	//dll檔案中定義的交易函數
	private static final String tradeFunName = "SendDateToTXZFAndReceiveFromTXZF" ;
	
	//dll檔案中定義的釋放資源函數
	private static final String closeFunName = "FinalSocketEnvironment" ;
	
	//為傳回消息而設定的記憶體大小
	private static final int memorySize = 65535 ;
	
	//JNative對象用于操作dll檔案
	private static JNative jn = null ;
	
	/**
	 * 初始化資源
	 * @return 傳回初始化的狀态碼
	 * 			>0 代表成功
	 * @throws Exception
	 */
	public static int open() throws Exception {
		jn = new JNative(dllName, initFunName) ;
		jn.setRetVal(Type.INT);
		jn.invoke() ;
		return jn.getRetValAsInt() ;
	}
	
	/**
	 * 交易函數
	 * @param send 
	 * @param receive 
	 * @return 傳回執行函數的資訊
	 * 		   失敗傳回null
	 * @throws Exception
	 */
	public static String trade(String send, String receive) throws Exception{
		jn = new JNative(dllName, tradeFunName) ;
		jn.setRetVal(Type.INT) ;
		Pointer point = new Pointer(MemoryBlockFactory.createMemoryBlock(memorySize));
		point.setStringAt(0, receive) ;
		jn.setParameter(0, Type.STRING, send) ;
		jn.setParameter(1, point) ;
		jn.invoke() ;
		int i = jn.getRetValAsInt() ;
		if(i>=0){
			String str = point.getAsString() ;
			point.dispose() ;
			return str ;
		}
		else 
			return null;
	}
	
	/**
	 * 釋放系統資源函數
	 * @return
	 * @throws Exception
	 */
	public static String close() throws Exception{
		int i ;
		String message ;
		jn = new JNative(dllName, closeFunName) ;
		jn.setRetVal(Type.INT);
		jn.invoke() ;
		i = jn.getRetValAsInt() ;
		if(i>0)
			message = "資源釋放成功!" ;
		else
			message = "資源釋放失敗!" ;
		return message ;
	}
	
	/**
	 * 把以上三個函數封裝
	 * 調用函數直接執行
	 * 初始化資訊,進行交易,釋放資源
	 * 三步
	 * @param send
	 * @param receive
	 * @return 傳回交易資訊
	 */
	public static String getInfo(String send, String receive){
		String info = null ;
		try{
			int i = open() ;
			if(i>0)
				info = trade(send, receive) ;
			else
				throw new Exception("建立連接配接失敗") ;
			System.out.println(close()) ;
		}catch(Exception e){
			e.printStackTrace() ;
		}
		return info ;
	}

}
           

 類用到了JNative插件來操作dll檔案的

JNative中有個類Pointer實作了類似于c語言中的指針的作用!當是以為實作指針是驅動了自己jar包中dll檔案實作的,但是我把C:\Windows\System32下的JNativeCpp.dll和它自身jar包中的JNativeCpp.dll都删了,但是它依然可以工作,于是檢視源代碼,結果更迷惑了!!!

Pointer是用setStringAt方法往裡面指派的,而setStringAt方法調用了JNative.setMemory()方法,

JNative.setMemory()方法調用了自身的nSetMemory()方法,但是 nSetMemory方法是這樣定義的:

private static native void nSetMemory(int pointer, byte[] buff, int offset,
            int len) throws NativeException;
           

到這裡就結束了.........

到底也不知道它是怎麼實作Pointer的???