Delphi中Create and call DLL

          
1 . create DLL (方法1): 
library MyDLL; 
uses 
  SysUtils, 
  Classes, 
  DLL 
in   ' DLL.pas '
{$R *.res}  
exports 
  AddIt, SubIt; 
begin 
end. 

unit DLL;    
//  新增一個單元文件 
interface  
function AddIt(x1, x2: Integer): Integer; stdcall; 
function SubIt(x1, x2: Integer): Integer; stdcall; 
implementation 
function AddIt(x1, x2: Integer): Integer; stdcall; 
begin 
  result :
=  x1  +  x2;    //  or AddIt := x1 + x2; 
end; 
function SubIt(x1, x2: Integer): Integer; stdcall; 
begin 
  result :
=  x1  -  x2;     //  or SubIt := x1 - x2; 

end; 
end. 

create DLL (方法2): 
library MyDLL; 
uses 
  SysUtils, 
  Classes, 
  function AddIt(x1, x2: Integer): Integer; export; 
  begin 
    result :
=  x1  +  x2;   //  or AddIt := x1 + x2;   
  end; 
  function SubIt(x1, x2: Integer): Integer; export; 
  begin 
    result :
=  x1  -  x2;  //  or SubIt := x1 - x2; 
  end 
{$R *.res}  
exporTs 
  AddIt, SubIt; 
begin 
end. 

2 . Static Call DLL 
unit MainForm; 
interface  
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls; 
type 
  TForm1 
=   class (TForm) 
    Edit1: TEdit; 
    Edit2: TEdit; 
    Label1: TLabel; 
    Label2: TLabel; 
    Button1: TButton; 
    Button2: TButton; 
    procedure Button1Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
  
private  
    
{ Private declarations }  
  
public  
    
{ Public declarations }  
  end; 
var 
  Form1: TForm1; 

implementation 
function AddIt(x1, x2: Integer): Integer; stdcall; external 
' MyDLL.dll '
function SubIt(x1, x2: Integer): Integer; stdcall; external 
' MyDLL.dll '
{$R *.dfm}  
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  
if  (Edit1.Text <> '' ) and (Edit2.Text <> '' ) then 
  begin 
    
try  
      Label1.Caption :
=  IntToStr( AddIt(StrToInt(Edit1.Text), StrToInt(Edit2.Text)) ); 
      Label2.Caption :
=  IntToStr( SubIt(StrToInt(Edit1.Text), StrToInt(Edit2.Text)) ); 
    except 
      ShowMessage(
' Input an integer number! ' ); 
      Edit1.SetFocus; 
    end; 
  end; 
end; 
procedure TForm1.Button2Click(Sender: TObject); 
begin 
  Edit1.Text :
=  IntToStr(Random(Width)); 
  Edit2.Text :
=  IntToStr(Random(Width)); 
end; 
end. 

3 . Automatic Call DLL 
unit AutoDLL; 
interface  
uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls; 
type 
  TMyDLL 
=  function(x, y: Integer): Integer; stdcall; 
  TForm1 
=   class (TForm) 
    Button1: TButton; 
    Edit1: TEdit; 
    Edit2: TEdit; 
    Label1: TLabel; 
    Button2: TButton; 
    Label2: TLabel; 
    Label3: TLabel; 
    procedure Button1Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
  
private  
    
{ Private declarations }  
  
public  
    
{ Public declarations }  
  end; 
var 
  Form1: TForm1; 
  Gv_DLLHandle: THandle; 
  Gv_DLLPointer: Pointer; 
  Gv_MyDLLFunc: TMyDLL; 
implementation 
{$R *.dfm}  
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  
if  (Edit1.Text = '' ) or (Edit2.Text = '' ) then 
    Exit; 
  Gv_DLLHandle :
=  LoadLibrary( ' MyDLL.dll ' ); 
  
if  Gv_DLLHandle > 0  then   // 判斷句柄是否有值﹐有值為載入成功 
     try  
      Gv_DLLPointer :
=  GetProcAddress(Gv_DLLHandle, PChar( ' AddIt ' ));  // 根據函數名稱獲得函數指針 
       if  Gv_DLLPointer <> nil then     // 判斷Gv_DLLPointer是否為空指針﹐不為空則為得到了函數地址 
      begin 
        Gv_MyDLLFunc :
=  TMyDLL(Gv_DLLPointer);   // 將指針強制轉換為函數指針 
         try  
          Label1.Caption :
=  IntToStr(Gv_MyDLLFunc( StrToInt(Edit1.Text), StrToInt(Edit2.Text) )); 
        except 
          ShowMessage(
' Input an integer number! ' ); 
        end; 
      end 
      
else  
        ShowMessage(
' No found "AddIt" ' ); 
      Gv_DLLPointer :
=  GetProcAddress(Gv_DLLHandle, PChar( ' SubIt ' )); 
      
if  Gv_DLLPointer <> nil then 
      begin 
        Gv_MyDLLFunc :
=  TMyDLL(Gv_DLLPointer); 
        
try  
          Label2.Caption :
=  IntToStr(Gv_MyDLLFunc(StrToInt(Edit1.Text), StrToInt(Edit2.Text))); 
        except 
          ShowMessage(
' Input an integer number! ' ); 
        end; 
      end 
      
else  
        ShowMessage(
' No found "SubIt" ' ); 
         
    
finally  
      FreeLibrary(Gv_DLLHandle); 
// 釋放DLL 
    end; 
end; 
procedure TForm1.Button2Click(Sender: TObject); 
begin 
  Edit1.Text :
=  IntToStr(Random(Height)); 
  Edit2.Text :
=  IntToStr(Random(Height)); 
end; 
end. 
           
  

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值