[JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PWideChar

Delphi代码

 1 unit UnitDll;
 2 
 3 interface
 4 
 5 uses
 6   StrUtils, SysUtils, Dialogs;
 7 
 8 function DoBusinessWide(pvData: PWideChar): PWideChar; stdcall;
 9 
10 implementation
11 
12 function DoBusinessWide(pvData: PWideChar): PWideChar; stdcall;
13 var
14   lvData: WideString;
15 begin
16   ShowMessage(pvData);
17   lvData := pvData;
18   ShowMessage(lvData);
19   lvData := lvData + FormatDateTime('YYYYMMDDHHMMSSZZZ',Now);
20   Result := PWideChar(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 import com.sun.jna.WString;
 6 
 7 public class JnaDemo {
 8     
 9     public interface RtcPubLibrary extends Library{
10         RtcPubLibrary instance = (RtcPubLibrary) Native.loadLibrary("JavaDll", RtcPubLibrary.class);
11         public WString DoBusinessWide(char[] pvData);
12     }
13 
14     public static void main(String[] args) {
15         System.setProperty("jna.encoding", "GBK");
16         StringBuilder sBuilder = new StringBuilder();
17         sBuilder.append("123测试");
18         char[] pvData = sBuilder.toString().toCharArray();
19         WString lvData = RtcPubLibrary.instance.DoBusinessWide(pvData);
20         System.out.println("Wstring-->" + lvData);
21     }
22 
23 }

 

正常返回结果:Wstring-->123测试20160825160441882

posted @ 2016-08-25 16:10  今心  阅读(4463)  评论(0编辑  收藏  举报