delphi下创建和调用dll

DELPHI中开发DLL的例子
[ 2007-3-17 21:03:00 | By: roki ]
 

1。开发DLL 程序

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

1)FILE—>NEW—>OTHER

选择NEW 标签中的 DLL Wizard 图标

自动生成一个产生 dll文件的模板程序(这个程序以 library project1 ; 开始的)

************************************************************************

library project1;

{………一堆注释………}

uses

   sysutils,classes;

begin

end.

************************************************************************

2)FILE—>NEW—>OTHER

选择NEW 标签中的 unit 图标,添加一个unit 文件进dll 的工程文件中。

在这个unit 文件中定义编写功能函数,例如

***************************注意蓝色粗体部分*************************************************

unit Unit1;

interface
function countnum(i:integer):integer;export;
implementation
    function countnum(i:integer):integer;
    var
    n:integer;
    begin
    n:=i;
    n:=n+100;
    result:=n;
    end;
end.
 

*********************************************************************************

 

开发以后必须在 前面dll的工程文件中添加一句

exports

countnum;

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

添加以后 就成了这样的代码

××××××××××××××××××××××××××××××××××

library Project1;

{ 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,
  Unit1 in 'Unit1.pas';

{$R *.res}
    exports
      countnum;

begin
end.

××××××××××××××××××××××××××××××××××

调试成功以后 会生成一个 project1.dll 文件

接着 我们把原工程关掉。生成DLL已经实现了,接着说说如何在程序中调用这个DLL,并且使用其中的countnum 函数。

新建一个工程(这个和普通工程的创建是一样的,)在unit 部分编写代码时implementation 前面写入一句

function countnum(i:integer):integer;far;external 'Project1.dll';  

意思就是调用同目录下的dll文件,当然,你要记得把刚才生成的Project1.dll先拷贝到你当前工程目录下哦。

unit的代码如下(关键是蓝色粗体部分):

××××××××××××××××××××××××××××××

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  n:integer ;
  function countnum(i:integer):integer;far;external 'Project1.dll';
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

begin

    n:=countnum(n);
    edit1.Text :=inttostr(n);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
n:=0;
end;

end.

 

 

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

以上代码测试通过,100%原创,,,,,相信看完你会对dll的理解更深了一层,其实,dll可以理解成为一个函数库,或者过程库,只是他的函数和过程,是动态连接给程序使用的,当程序用到这个函数或者过程的时候才调用dll,如果不用到的时候,就不会调用,这就是动态连接的概念了。

动态连接的好处就是可以节省系统的很多内存资源。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值