DLL 파일의 Delphi 생성 및 호출

3870 단어 windowsDelphi

       ,           ,       ,              。
 DLL       。              ,             。


 .       :

library FIRSTDLL;

uses
 SysUtils,
 Classes;

{$R *.RES}
// 1.               
// --------------------------------
//    1
//   :   3     
// --------------------------------
function PenniesToSoins(SourceResult:Integer):Integer;stdCall;
begin
 if SourceResult>0 then
   Result:=SourceResult*3 //     Result
 else
   Result:=SourceResult;
end;

exports 
 PenniesToSoins; //2.       
 
end.

==
==

 . DLL   Form
=======================
1.  ,  DLL  ,       Form

library MGRPERSN;
uses
 SysUtils,
 Classes,
 MGRPERFM in 'MGRPERFM.pas' {FormPERSON};//1.Form   (    Form  )

{$R *.RES}
exports
  ShowPerSN;//2.       
begin
end.

2.  DLL   Form    
===========================================
unit MGRPERFM;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 ComCtrls, ToolWin, ImgList;

type
 TFormPERSON = class(TForm)
 private
   { Private declarations }
 public
   { Public declarations }
 end;

//        ,      ,  (    )
//var 
//  FormPERSON: TFormPERSON;

{ Declare the export function   Form    }//    
function ShowPerSN(AHandle: THandle; ACaption: String):BOOL; StdCall;

implementation

{$R *.DFM}
//       
function ShowPerSN(AHandle: THandle; ACaption: String):BOOL;
var
 FormPERSON: TFormPERSON; //     (        )
begin
 // Copy application handle to DLL's TApplication object
 //         DLL       
 Application.Handle := AHandle;
 FormPERSON := TFormPERSON.Create(Application);//    TForm
 try
   FormPERSON.Caption := ACaption;
   FormPERSON.ShowModal;//   Form
   // Pass the date back in Result
   Result := False; //     
 finally
   FormPERSON.Free;
 end;
end;

 .DLL         
==========================
1.     
--------------
implementation //            DLL

{$R *.DFM}
//DLL     
function PenniesToSoins(SourceResult:Integer):Integer;
    StdCall external 'FIRSTDLL.DLL';

........

2.     
==============
type  //         
 // 1 -------------------------------
 { First, define a procedural data type, this should reflect the
   procedure that is exported from the DLL. }
 { Create a new exception class to reflect a failed DLL load }
 TShowPerSN = function (AHandle: THandle; ACaption: String): BOOL; StdCall;
 EDLLLoadError = class(Exception);//            
 // 1 -------------------------------
 TMAINCLTR = class(TForm) //    ,      

......

procedure TMAINCLTR.ToolButton1Click(Sender: TObject);
var  //       :    
 LibHandle: THandle;
 ShowPerSN: TShowPerSN;
begin
 Application.Title:='        DLL      ';
 { Attempt to load the DLL     DLL  }
 LibHandle := LoadLibrary('MGRPERSN.DLL');
 try
   if LibHandle = 0 then
     raise EDLLLoadError.Create('Unable to Load DLL(      MGRPERSN.DLL)');
   @ShowPerSN := GetProcAddress(LibHandle, 'ShowPerSN');
   if not (@ShowPerSN = nil) then
     ShowPerSN(Application.Handle, '      ')//     
   else
     RaiseLastWin32Error;
 finally
   FreeLibrary(LibHandle); // Unload the DLL.
 end;
end;
============== END ================== 

좋은 웹페이지 즐겨찾기