Delphi中Create and call DLL

来源:互联网 发布:乐视2移动数据上不了网 编辑:程序博客网 时间:2024/04/30 15:04

          
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. 
           
  

 

原创粉丝点击