delphi編寫與調用DLL(delphi7下測試通過)


http://blog.sina.com.cn/s/blog_4dbbf76f01000anz.html

  1 delphi編寫DLL
  2 下面在delphi中編寫一個簡單的dll,在該dll中只有一個max函數,返回2個數中的大數(Delphi 5.0)
  3 1、New->DLL;取名為DLL_0001,編寫代碼:
  4 library dll_0001;
  5 uses
  6    SysUtils,
  7    Classes;
  8 {$R *.RES}
  9 function max(x,y:integer):integer;stdcall;
 10 begin
 11      if(x>y) then
 12       max :=x
 13       else
 14       max :=y;
 15 end;
 16 exports max;
 17 begin
 18 end.
 19 紅色部分為自己編寫,這里和普通的delphi函數是一樣的,只是在返回值中帶個stdcall參數,然后用exports把函數導出
 20 ================================================================================
 21 Delphl調用dll
 22 調用dll分動態調用和靜態調用2中,動態調用寫起來簡單,安全點,動態調用復雜很多,但很靈活;
 23 現在編寫一個程序,來調用上面寫的dll_0001.dll文件中的max函數
 24 一、new一個Application,在Form中放入2個TEdit、1個TLabek;
 25 二、
 26 1、靜態調用
 27 unit Unit1;
 28 interface
 29 uses
 30    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 31    StdCtrls;
 32 type
 33    TForm1 = class(TForm)
 34      Edit1: TEdit;
 35      Edit2: TEdit;
 36      Label1: TLabel;
 37      procedure Edit2KeyDown(Sender: TObject; var Key: Word;
 38        Shift: TShiftState);
 39    private
 40      { Private declarations }
 41    public
 42      { Public declarations }
 43    end;
 44 var
 45    Form1: TForm1;
 46 implementation
 47 {$R *.DFM}
 48 function max(x,y:integer):integer;stdcall;
 49 external 'dll_0001.dll';
 50 procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
 51    Shift: TShiftState);
 52 begin
 53 if key =vk_return then
 54 label1.Caption :=IntToStr(max(StrToInt(Edit1.text),StrToInt(edit2.text)));
 55 end;
 56 end.
 57 紅色代碼自己添加,其中external "dll_name"中的dll_name可以是dll的絕對路徑,要是該dll文件在你的搜索路徑中,可以直接寫文件名,但是.dll不能少寫
 58 2、動態調用,代碼如下;
 59 unit Unit1;
 60 interface
 61 uses
 62    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 63    StdCtrls;
 64 type
 65    TForm1 = class(TForm)
 66      Edit1: TEdit;
 67      Edit2: TEdit;
 68      Label1: TLabel;
 69      procedure Edit2KeyDown(Sender: TObject; var Key: Word;
 70        Shift: TShiftState);
 71    private
 72      { Private declarations }
 73    public
 74      { Public declarations }
 75    end;
 76 var
 77    Form1: TForm1;
 78 implementation
 79 {$R *.DFM}
 80 procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
 81    Shift: TShiftState);
 82 type
 83 TFunc =function(x,y:integer):integer;stdcall;
 84 var
 85 Th:Thandle;
 86 Tf:TFunc;
 87 Tp:TFarProc;
 88 begin
 89 if key =vk_return then
 90 begin
 91 Th :=LoadLibrary('dll_0001.dll');    {load dll}
 92 if(Th   >0) then
 93 try
 94 Tp :=GetProcAddress(Th,PChar('max'));
 95 if(Tp <>nil) then
 96 begin         { begin 1}
 97 Tf :=TFunc(Tp);
 98 Label1.Caption :=IntToStr(
 99 Tf(StrToInt(Edit1.text),StrToInt(Edit2.text)));
100 end   { end 1}
101 else
102    ShowMessage('function max not found.');
103 finally
104 FreeLibrary(Th);
105 end
106 else
107 ShowMessage('dll_0001.dll not exsit.');
108 
109 end;
110 end;
111 end.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM