使用Delphi实现JNI - 实例

版权声明:本文为xiaobin原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaobin_HLJ80/article/details/46299877

    JNI是Java Native Interface的缩写。从Java 1.1开始,Java Native Interface (JNI)标准成为java平台的一部分,它允许Java代码和其他语言写的代码进行交互。JNI一开始是为了本地已编译语言,尤其是C和C++而设计的,但是它并不妨碍你使用其他语言,只要调用约定受支持就可以了。 

    使用java与本地已编译的代码交互,通常会丧失平台可移植性。但是,有些情况下这样做是可以接受的,甚至是必须的,比如,使用一些旧的库,与硬件、操作系统进行交互,或者为了提高程序的性能。JNI标准至少保证本地代码能工作在任何Java 虚拟机实现下。

    下面我们使用Delphi创建一个DLL,提供JAVA所需要的函数。

    1. 创建DLL工程

        New->Other,选"DLL Wizard"

    2. 增加pas到工程

        Project->Add to Project...

    

    把JNI.pas和其他工程需要的源文件加入到工程里。

   3. 保存工程文件

       注意:工程名即是动态库文件名。

附:

    Umethod.pas

unit Umethod;

interface

uses
  SysUtils,
  Classes;

// procedure CreateFile(TFileName: String); // original source 
function CreateFile(TFileName: String): Boolean;

implementation
uses
  Udefine;



function CreateFile(TFileName: string): Boolean;
Begin
  (*
  If Not FileExists(TFileName) Then
  Begin
    genCRCtable;
    FileStream := TFileStream.Create(TFileName, fmCreate);
    FileHead.Version := '4.2.0.2';
    FileHead.author:='xiaobinORA';
    FileHead.CRC32:=calCRCvalue;
    Filehead.UpdateDate := Now;
    FileStream.Write(FileHead, SizeOf(FileHead));
  End
  Else
  Begin
  *)
  if Not FileExists(TFileName) then
  begin
    Result := False;
    Exit;
  end;
  FileStream := TFileStream.Create(TFileName, fmOpenReadWrite);
  Result := True;
  //End;
End;

end.

Udefine.pas参见[1]

xbfLibR.dpr参见[2]

JNI.pas参见[3]

下载地址:http://www.pudn.com/downloads689/sourcecode/java/detail2777247.html

参考文档:

    1. 辛亥百年纪念 - 产品组件系列02

    2. 辛亥百年纪念 - 产品组件系列04

    3. Using the Java Native Interface with Delphi - Matthew Mead

       JNI_pas.zip

猜你喜欢

转载自blog.csdn.net/xiaobin_HLJ80/article/details/46299877
今日推荐