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));

原文地址:https://www.cnblogs.com/tylertang/p/12929040.html