美文网首页
Delphi 7:操作DLL

Delphi 7:操作DLL

作者: 酸奶不错 | 来源:发表于2020-10-21 17:54 被阅读0次

    一、编写DLL

    打开 Delphi 7 主窗体,File -> New -> Other -> DLL Wizard -> OK,创建 DLL 工程,保存并将工程名命名为 Practice_For_DLL。实例代码如下:

    library Practice_For_DLL;
    
    uses
      SysUtils, Classes;
    
    {$R *.res}
    
    function Plus(x, y: integer): Integer; stdcall;    //stdcall,表示这个函数遵循 DLL标准调用协议
    begin                                              //其他语言都按这个协议来调用 DLL,就不会出错
      Result:= x + y;
    end;
    
    exports    //导出函数,被它导出的函数可以被其他程序调用,例如这里的 Plus。
      Plus;
    
    begin
    end.
    

    运行一下,可以看到工程目录下多出了一个 Practice_For_DLL.dll 文件,这个 DLL 文件可以被其他程序调用。

    二、静态调用

    首先 File -> New -> Application 创建一个项目,保存并将工程命名为 Practice_For_DLL_Static,然后将上面编写的 DLL 文件拷贝到此目录下,编写代码如下:

    unit un_Main;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TFrm_Main = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Frm_Main: TFrm_Main;
    
      //通过 external 来指定所要调用的 DLL 文件的名称,没有写路径是因为此 DLL 文件和调用者在同一目录下。
      function Plus(x, y: Integer): Integer; stdcall; external 'Practice_For_DLL.dll' name 'Plus';
    
      //自定义函数名 与 DLL中函数名 相同时,可以省略掉后面的 name 参数。
      //function Plus(x, y: Integer): Integer; stdcall; external 'Practice_For_DLL.dll';
    
    implementation
    
    {$R *.dfm}
    
    procedure TFrm_Main.Button1Click(Sender: TObject);
    var
      m, n: Integer;
    begin
      m:= StrToInt(Edit1.Text);
      n:= StrToInt(Edit2.Text);
      Caption:= IntToStr(Plus(m, n));
    end;
    
    end.
    

    运行效果图如下:

    三、动态调用

    首先 File -> New -> Application 创建一个项目,保存并将工程命名为 Practice_For_DLL_Dynamic,然后将上面编写的 DLL 文件拷贝到此目录下,编写代码如下:

    unit un_Main;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TFrm_Main = class(TForm)
        Edit1: TEdit;
        Edit2: TEdit;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
      //声明一个函数类型
      TPlus = function (x, y: Integer): Integer; stdcall;
    
    var
      Frm_Main: TFrm_Main;
    
    implementation
    
    {$R *.dfm}
    
    procedure TFrm_Main.Button1Click(Sender: TObject);
    var
      hDll: THandle;
      pFnc: TFarProc;
      Plus: TPlus;
      m, n: Integer;
    begin
      //装载DLL
      hDll:= LoadLibrary(PChar('Practice_For_DLL.dll'));
      if hDll = 0 then Exit;
    
      //获取函数地址
      pFnc:= GetProcAddress(hDll, 'Plus');
    
      //实例化函数
      Plus:= TPlus(pFnc);
    
      m:= StrToInt(Edit1.Text);
      n:= StrToInt(Edit2.Text);
      Caption:= IntToStr(Plus(m, n));
    
      //释放资源
      FreeLibrary(hDll);
    end;
    
    end.
    

    运行效果同静态调用。

    四、静态调用与动态调用的区别

    1. 动态在使用时加载,不需要时卸载,这样节省硬件资源
    2. 静态在主程序加载时就被加载到内存,使用时不必再次加载直接使用,速度更快



    ※ 注意点:如果你的 dll 要给其他语言调用,比如 C++,那么不要在 dll 中封装任何与界面有关的东西;以及使用 PChar 代替 string。

    相关文章

      网友评论

          本文标题:Delphi 7:操作DLL

          本文链接:https://www.haomeiwen.com/subject/ezvdmktx.html