[JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PAnsiChar
Delphi代码
1 unit UnitDll; 2 3 interface 4 5 uses 6 StrUtils, SysUtils, Dialogs; 7 8 function DoBusinessAnsi(pvData: PAnsiChar): PAnsiChar; stdcall; 9 10 implementation 11 12 function DoBusinessAnsi(pvData: PAnsiChar): PAnsiChar; stdcall; 13 var 14 lvData: AnsiString; 15 begin 16 ShowMessage(pvData); 17 lvData := pvData; 18 ShowMessage(lvData); 19 lvData := lvData + FormatDateTime('YYYYMMDDHHMMSSZZZ',Now); 20 Result := PAnsiChar(lvData); 21 ShowMessage(Result); 22 end; 23 24 end.
Java调用代码
1 package com.jna.demo; 2 3 import com.sun.jna.Library; 4 import com.sun.jna.Native; 5 6 public class JnaDemo { 7 8 public interface RtcPubLibrary extends Library{ 9 RtcPubLibrary instance = (RtcPubLibrary) Native.loadLibrary("JavaDll", RtcPubLibrary.class); 10 public String DoBusinessAnsi(String pvData); 11 } 12 13 public static void main(String[] args) { 14 System.setProperty("jna.encoding", "GBK"); 15 StringBuilder sBuilder = new StringBuilder(); 16 sBuilder.append("123测试"); 17 String pvData2 = sBuilder.toString(); 18 System.out.println("String-->" + RtcPubLibrary.instance.DoBusinessAnsi(pvData2)); 19 } 20 21 }
正常返回结果:String-->123测试20160825160443394