`
美丽的小岛
  • 浏览: 297650 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Delphi开发DLL,Java调用DLL

 
阅读更多

用一个小例子来示范一下,不写文件字。

第一步

第二步,

Dynamic-link Library就得,生成如下:

编写自己的Dll

 

 

library Add;

 

uses

  SysUtils,

  Classes;

 

{$R *.res}

 

function addFunABC(a, b: integer): integer; stdcall;

begin

  result := a + b;

end;

 

exports

  addFunABC;

 

begin

end.

 

Dlephi调用DLL

 

创建一个VCL,动态加载DLL

 

procedure TForm1.Button1Click(Sender: TObject);

type

  TAddFunc = function (a, b: integer): integer; stdcall;

var

  Th:Thandle;

  Tf:TAddFunc;

  Tp:TFarProc;

begin

  Th:=LoadLibrary('MyAdd.dll'); {装载DLL}

  if Th>0 then

    try

      Tp:=GetProcAddress(Th,PChar('addFunABC'));

      if Tp<>nil then

      begin

        Tf:=TAddFunc(Tp);

        Edit1.Text:=IntToStr(Tf(2, 3)); {调用函数}

      end

      else

        ShowMessage('addFunABC函数没有找到');

    finally

      FreeLibrary(Th); {释放DLL}

    end

  else

   ShowMessage('MyAdd.dll没有找到');

end;

 

 

 

JAVA调用(JNative.jar)

 

import org.xvolks.jnative.JNative;

import org.xvolks.jnative.Type;

import org.xvolks.jnative.exceptions.NativeException;

import org.xvolks.jnative.misc.basicStructures.LONG;

publicclass Apllication {

    /**

     * java调用Dll,采用JNative;

     * JNative里面有三个主要的包:

     *     For Windows:

     *     For Linux:

     *     JNativeJar包:   JNativeCpp.dlllibJNativeCpp.soJNative.jar     

           JNativeCpp.dll Windows下用的,拷贝到windows / system32目录下;

           libJNativeCpp.so Linux下的,拷贝到系统目录下;

           JNative.jar 这是一个扩展包,导入工程LIB中或将其拷贝到jdk\jre\lib\ext 下,系统会自动加载

     */

    publicstaticvoid main(String[] args) throws NativeException,

           IllegalAccessException {

       JNative n = new JNative(            "C:\\Users\\Administrator\\Desktop\\MyFirstDll\\MyAdd.dll", "addFunABC"); //

       LONG a = new LONG(2);

       n.setParameter(0, Type.INT, a.toString());

       LONG b = new LONG(3);

       n.setParameter(1, Type.INT, b.toString());

       n.setRetVal(Type.INT);

       n.invoke();

       System.out.println(n.getRetVal());

    }

}

 

 

 

  • 大小: 123 KB
  • 大小: 16.4 KB
  • 大小: 44 KB
1
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics