最近在学习过程中碰到一个小问题:delphi中如何创建并调用dill,最好能一步一步教,先谢谢拉!

解决方案 »

  1.   

    新建dll项目
    exports 导出函数
      

  2.   

    exports  and  extend
      

  3.   

    dll创建和调用的简单例子:
    Dll工程的代码:
    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;{$R *.res}
    function NumberEquals( const numberA,numberB:Integer):Integer;stdCall;
    begin
      if numbera = numberb then
        result:=1
      else
        result:=0;end;
    function Min(X, Y: Integer): Integer;stdcall;export;
    begin
      if X < Y then
        Result := X 
      else 
        Result := Y;
    end;
    function Max(X, Y: Integer): Integer;stdcall;export;
    begin
      if X > Y then 
        Result := X
     else 
        Result := Y;
    end;
    //函数输出口定义
    exports
      NumberEquals ,
      Min ,
      Max ;
    begin
    end.
    调用Dll的工程代码:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    function  Min(X, Y: Integer): Integer; export;stdCall;external 'Project1.dll';
    function  Max(X, Y: Integer): Integer; export;stdCall;external 'Project1.dll';
    function NumberEquals( const numberA,numberB:Integer):Integer;stdCall; external 'Project1.dll';
    procedure TForm1.Button1Click(Sender: TObject);
    begin
        caption:=inttostr(Min(2,3));
        showmessage(inttostr(Max(2,3)));
        showmessage(inttostr(NumberEquals(5,4)));
    end;end.