谢谢.

解决方案 »

  1.   

    以下是转贴在Delphi应用程序中使用DLL  Delphi与VisualBasic、VisualFoxPro等软件一样,属于RAD工具(快速应用开发工具)。适合开发32位或16位/32位混合应用程序。Delphi所使用的程序语言
    是ObjectPascal,是结构化、面向对象的编译型语言,具有高执行效率、可重用性、易维护性,及较强的异常处理能力、类封装能力等。VB与Delphi相比,VB不
    能对程序进行编译,只能解释执行,更重要的不同是Delphi具有较强的继承性,Delphi的应用程序可编译DLL模块,VB却无法编译,只能调用C编译的DLL。而
    Delphi不仅可调用C++产生的DLL模块,同样C++程序也可调用Delphi所产生的DLL部件。这样交互调用,无须重复开发,大大缩短了生产周期。一、用Delphi创建DLL  Delphi的DLL创建并不复杂,下面向大家介绍Delphi的DLL创建方法。  1、首先创建一个新的DLL项目(NewProject),因为DLL与调用它的主程序要分开编译。如果DLL模块已经建立在调用它的项目中(Project),则将它的窗体
    (Form)从Project移出,另建一个新的项目(NewProject)。只需从File菜单选中NewProject项,然后将DLL的Pas模块文件加入到项目中,再将其自动建立的
    Form1删除即可。  2、在DLL的DPR文件中把Program关键字改为Library,申明为动态链接库,在USES语句后面加入ExPorts语句,指明调用DLL的函数名。  3、如果主程序的DPR文件已有DLL窗体CreateForm的语句,则将其去掉。  4、在DLL的Pas文件中Type......End后加入该DLL的函数或过程的声明,形式如:  FunctionName(argment):Boolean;export;  该函数或过程应加入窗体的Create和Free(产生和释放)方法。  5、对项目进行编译即可。二、DLL的调用  调用DLL有两种方法,一种是在应用程序装载时调用,另一种是在应用程序运行时调用。首先介绍装载时DLL的调用:  (1)装载时调用DLL  在调用DLL的Pas文件中,对DLL函数进行外部声明,声明应位于Implementation的Uses语句后,形式如下:  Implementation  Uses Dialogs;  Function Name(argment):Boolean;far;External 'CallName';  ......  其中External关键字后面的引号内的字串是DLL的文件名。声明以后即可在Pas文件任何地方引用DLL函数。  装载时调用DLL的优点是速度较快,程序间也可共享代码。  (2)运行时调用DLL  DLL的另一种调用方法是在运行时调用。要调用到Windows的API函数:LoadLibrary,GetProcAddress等。主要用于调用
    DELPHI和其它语言,特别是C++编译的DLL。  假定你的DLL包括一个函数:  Function MyFunc(aparam:word):string;export;  首先在程序Type类型声明处加入一句:  Type
        TMyfunc = function(aparam:word):string;  此句的作用如同C++中声明的函数指针。  然后定义如下变量∶  Var
         aptr:TFarproc;
         lhnd:THandle;
         s:string;  其中Aptr,lhnd两变量声明必须有,s是DLL函数返回值,视情况而定。  在调用DLL处加入如下语句进行DLL装载:
      lhnd:=Loadlibrary('路径:DLL文件名');{如lhnd:=Loadlibrary('c:\aa\bb.dll');
      aptr:=GetprocAddress(lhnd,'Myfunc');  下面可直接调用DLL了:
      s:=TMyfunc(bptr)(60);{根据函数填相应的变量参数}  调用完以后,用FreeLibrary释放DLL占用的内存:
      FreeLibrary(lhnd);  下面给出一个DLL的创建以及运行时调用的示例,该DLL主要用来检查输入的口令是否正确,窗体含有一个Edit
    编辑框,两个按钮Button,一个标签Label,在编辑框内输入口令,根据比较结果返回真假值。
      {main.pas主程序(运行时调用DLL)}  unitMain;  interface
            uses WinTypes,WinProcs,Classes,Graphics,Forms,Controls,StdCtrls,ExtCtrls;  type  TForm1= class(TForm)
        Edit1:TEdit;
        Label1:TLabel;
        Button1:TButton;
           Bevel1:TBevel;
        GroupBox1:TGroupBox;
        StatusLbl:TLabel;
        procedure Button1Click(Sender:TObject);
     end;  TGetPass=function(aa:string):boolean; //函数声明  var //全局变量声明
        Form1:TForm1;
        getpass:TGetpass;
        lhnd:THandle;
        aptr:TFarProc;  implementation
      uses Dialogs;  {$R*.DFM}  {Import routine from DLL.Takes password to match and returns boolean  .}  {function GetPassword(Password:string):Boolean;far;external'CHKPWORD';}  {Call password check routine,show status information.}  procedure TForm1.Button1Click(Sender:TObject);
      begin
        if Edit1.Text = ''then
        begin
           MessageDlg('Enter sample password first',mtInformation,[mbOK],0);
           Edit1.SetFocus;
         end
       else begin
          lhnd := LoadLibrary('Chkpword.dll'); //运行的时候调用Chkpword.DLL
           aptr:= GetProcAddress(lhnd,'GetPassword'); //DLLZ的函数名称为GetPassword
          if TGetPass(aptr)(Edit1.Text) then //对DLL进行调用
               StatusLbl.Caption := 'Verified password'
          else
                      StatusLbl.Caption := 'Invalid password';
          freelibrary(lhnd); //释放
      end;  end.  
      {dllform.pasDLL模块}
      unit Dllform;  interface
      uses WinTypes,WinProcs,Classes,Graphics,Forms,Controls,Buttons,SysUtils,StdCtrls;  type  TPasswordForm = class(TForm)
        Edit1:TEdit;
        Label1:TLabel;
        BitBtn2:TBitBtn;
        BitBtn1:TBitBtn;
       end;  function GetPassword(Password:string):Boolean;export;  implementation
      uses Dialogs;  {$R*.DFM}
      functionGetPassword(Password:string):Boolean;
      var
        PasswordForm:TPasswordForm;
      begin
        Result:=False;
        PasswordForm:=TPasswordForm.Create(Application);
        try
          with PasswordForm do
           if ShowModal=mrOK then
             if UpperCase(Edit1.Text)<> UpperCase(Password)then
                MessageDlg('InvalidPassword',mtWarning,[mbOK],0)
             else
                Result:=True;
        finally
            PasswordForm.Free;
        end;
      end;  end.
      

  2.   

    library Project2;{ 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;function Add(a,b:integer):integer;stdcall;
    begin
        Result:=a+b;
    end;
    //{$R *.res}exports
     ADD Index 1;end.
      

  3.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Edit1: TEdit;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      function Add(a,b:integer):integer;Stdcall ;far;external 'Project2.dll';
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      Edit1.Text:=inttostr(add(6,6));
    end;end.