Java使用JNA調用C# dll方法


原文:https://www.cnblogs.com/wyongbo/p/jnaTest.html

原文鏈接:https://blog.csdn.net/qq_18219457/java/article/details/93905147

這兩篇文章已經講得非常清楚了。

但在實際操作的時候,由於C#使用了SQLite,而SQLite的相關庫是x86的,因此,java的jdk和c++以及C#環境都需要設置為x86運行環境。

x86  jdk下載地址:

https://www.cr173.com/soft/79926.html

補充:調用過程中,當存在中文輸入/輸出時,出現亂碼。

解決方案:兩邊的輸入輸出通過base64的方式進行交互

Java端:

     // 將 s 進行 BASE64 編碼 
     public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); } 
     // 將 BASE64 編碼的字符串 s 進行解碼 
     public static String getFromBASE64(String s) { if (s == null) return null; sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }
View Code

C#端:

public static string EncodeBase64(string codeType, string code) { string encode = ""; byte[] bytes = Encoding.GetEncoding(codeType).GetBytes(code); try { encode = Convert.ToBase64String(bytes); } catch { encode = code; } return encode; }
        public static string DecodeBase64(string codeType, string code) { string decode = ""; byte[] bytes = Convert.FromBase64String(code); try { decode = Encoding.GetEncoding(codeType).GetString(bytes); } catch { decode = code; } return decode; }
View Code

調用統一采用utf-8的方式:

vehicleInfo = DecodeBase64("utf-8", vehicleInfo);

EncodeBase64("utf-8", JsonConvert.SerializeObject(result));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM