JAVA調用動態鏈接庫DLL的方法有:JNI(Java Native Interface),JInvoke,JNative(Java to native interface),JNI是JAVA自身提供的方法;JInvoke從何而來不得而知,好像還需要注冊付費的,64位的JDK、JIR環境下運行不了,在VMware下安裝的WindowsXP下同樣有問題;JNative是開源的,但相關資料文檔也特別的少,好像已經停止更新了。JNative源碼地址:http://jnative.cvs.sourceforge.net/viewvc/jnative/,jar包下載地址:http://sourceforge.net/projects/jnative/files/jnative/。
開發環境:myeclipse-10.7-offline-installer-windows
文件》新建》JAVA項目:
輸入項目名稱,單擊“Finish”:
單擊鼠標右鍵》新建》類:
輸入包名,類名,勾選“public static void main(String[] args)”,單擊“Finish”:
打開項目文件目錄:
將要調用的DLL文件“EhfscliaxDll.dll”復制到此目錄下,該DLL的代碼請參考:VS2010編寫動態鏈接庫DLL及單元測試用例,調用DLL測試正確性:http://blog.csdn.net/testcs_dn/article/details/27237509
新建“lib”文件夾,將JNative.jar復制到此目錄下:
配置編譯目錄:
單擊“Add External JARs”添加jar包,下圖為已經添加過的:
Helper類代碼:
package com.ehfscliax; import org.xvolks.jnative.JNative; import org.xvolks.jnative.Type; import org.xvolks.jnative.exceptions.NativeException; import org.xvolks.jnative.pointers.Pointer; public class Helper { /** * @param args * @throws NativeException */ public static void main(String[] args) throws NativeException { JNative.setLoggingEnabled(true); try { JNative getUrl = new JNative("EhfscliaxDll.dll", "getUrl"); //創建 getUrl 方法的<span style="font-family: Arial, Helvetica, sans-serif;">JNative對象</span> getUrl.setRetVal(Type.STRING); //設置返回值類型為:String getUrl.setParameter(0, "127.0.0.1"); //按順序設置方法需要的參數值 getUrl.setParameter(1, 10087); getUrl.setParameter(2, 123); getUrl.invoke(); //調用方法 System.out.println(getUrl.getRetVal()); //輸出返回值 } catch (IllegalAccessException e) { e.printStackTrace(); } try { JNative getPlayUrl = new JNative("EhfscliaxDll.dll", "getPlayUrl"); getPlayUrl.setRetVal(Type.INT); //此方法在DLL中定義的返回值類型為:const wchar_t*,如果設置返回值類型為:String,則只能獲取到返回值的第一個字符 String mgrIp = "127.0.0.1"; Pointer ptr = Pointer.createPointerFromString(mgrIp); getPlayUrl.setParameter(0, ptr); getPlayUrl.setParameter(1, 10087); getPlayUrl.setParameter(2, 123); getPlayUrl.invoke(); //個人理解getRetValAsInt是獲取返回值的指針地址,<span style="font-family: Arial, Helvetica, sans-serif;">getUnicodeMemoryAsString方法從指針地址的內存中讀取字符串</span> String url = JNative.getUnicodeMemoryAsString(getPlayUrl.getRetValAsInt()); System.out.println(url); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
執行結果:
2014-5-25 22:48:46, [DEBUG] [org.xvolks.jnative.JNative] [loadLibrary]: Successfully loaded library 'EhfscliaxDll.dll', functionName = getUrl: hModule = 268435456 http://中文127.0.0.1:10087 2014-5-25 22:48:46, [DEBUG] [org.xvolks.jnative.pointers.memory.MemoryBlockFactory] [setPreferredMemoryType]: Using org.xvolks.jnative.pointers.memory.HeapMemoryBlock memory reservation strategy http://-17001:10087 2014-5-25 22:48:46, [DEBUG] [org.xvolks.jnative.JNative] [unLoad]: Unloading native library 'EhfscliaxDll.dll'
getUrl方法的返回值類型為:const char*,結果是正常的,getPlayUrl的返回值類型為:const wchar_t*,雖然通過getUnicodeMemoryAsString讀取到了返回值,不是第一個字符,但中文亂碼的問題一直沒能解決。
本學習項目源碼下載:http://download.csdn.net/detail/testcs_dn/7400203
作者:csdn博客 微wx笑