DLL 的静态调用实例代码

 自己写了一MinMax.dll文件 里面定义了2个函数Min、Max

 

在测试中使用了静态调用的方法

 

完整代码如下:

----------------------------------------

unit unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Buttons;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation


{$R *.dfm}
uses unit3;
function min(x,y:integer):integer;stdcall; external 'Minmax.dll' name 'Min';
function max(x,y:integer):integer;stdcall; external 'Minmax.dll' name 'Max';

procedure TForm1.Button1Click(Sender: TObject);
var
  x,y,mi,ma:integer;
begin
  if (edit1.Text ='') or (edit2.Text ='') then
    showmessage('X或Y值为空!')
  else
  begin
    x:=strtoint(trim(edit1.Text) );
    y:=strtoint(trim(edit2.Text) );
    mi:=min(x,y);
    ma:=max(x,y);
    Memo1.Lines.Clear ;
    Memo1.Lines.Add ('最小值:'+inttostr(mi));
    Memo1.Lines.Add ('最大值:'+inttostr(ma));
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  Form3:=TForm3.Create(Form1);
  Form3.ShowModal ;
end;

end.

-----------------------------------

MinMax.dll文件源码如下:

 

library Minmax;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes;

{$R *.res}
function Min(x,y:integer):integer;stdcall ;
begin
  if (x<y) then
    Min:=x
  else
    Min:=y;
end;

function Max(x,y:integer):integer;stdcall ;
begin
  if (x>y) then
    Max:=x
  else
    Max:=y;
end;
exports Min,Max;
begin
end.
------------------------------

 

2009-11-04

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JNA 是 Java Native Access 的缩写,是 Java 的一个开源库,可以方便地访问本地的 C/C++ 库。如果你想要使用 JNA 调用 dll 库,你需要按照以下步骤进行操作: 1. 定义接口 首先你需要定义一个接口,用来描述你要调用dll 的函数。接口的定义需要继承自 com.sun.jna.Library 接口,这个接口定义了 JNA 库的核心功能,你需要在接口中定义调用的函数,如下所示: ```java import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Platform; public interface MyDll extends Library { MyDll INSTANCE = (MyDll) Native.loadLibrary((Platform.isWindows() ? "mydll.dll" : "mydll"), MyDll.class); void myFunction(); } ``` 2. 加载库 在上面的代码中,我们使用 Native.loadLibrary() 方法加载了一个名为 mydll.dll 的库,这个方法的第一个参数是库的名称,第二个参数是接口的类型。在这个例子中,我们通过 INSTANCE 变量获取了一个 MyDll 接口的实例。 3. 调用函数 在我们定义的接口中,我们已经定义了一个名为 myFunction() 的函数,我们可以直接调用这个函数来执行我们想要的操作,例如: ```java MyDll.INSTANCE.myFunction(); ``` 注意,这里的 INSTANCE 是我们在定义接口时创建的一个静态变量,它是一个 MyDll 接口的实例,我们可以通过它来调用我们定义的函数。 以上就是使用 JNA 调用 dll 的基本步骤,当然在实际使用中还需要根据具体的情况进行一些参数的设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值