Custom Rich-Text Page

 Delphi About oops Error Handle DLL DllForms Sql Commands XML XML  Extension API MessageBox  API DELPHI Memory Leakage I|O ERROR Guest Book Malai



Dynamic Link Library

A dynamic link library or DLL is a program that another program needs for its functionality. It usually has an extension of .dll although it could sometimes have another extension. DLLs are created for various reasons, the most regular of which allows different programs to "call" the same DLL.

 

1.       Start Borland Delphi if you didn't yet. From the Standard toolbar, click the New button .

2.       From the New Items dialog box, click DLL Wizard

3.       Click OK. 4.       To save the current project, on the main menu, click File -> Save All 5.       To make things easier, from the Save Unit1 As dialog box, locate the common folder called Programs and display the Delphi folder it in the Save In combo box. 6.       Click the Create New Folder button, type DLLExercise and press Enter. 7.       Double-click the new folder to display it in the Save In combo box. In the File Name edit box, change the name of the project to DLLProject and click Save. 8.       Examine and read the whole file, especially the commented section.

                      Change the content of the file as follows:

 

library DLLProject; 
uses
SysUtils,
 Classes;
function BoxArea(L, H, W : Double) : Double;
begin
   Result := 2 * ((L*H) + (L*W) + (H*W));
end;
function BoxVolume(L, H, W : Double) : Double;
begin
  Result := L * H * W;
end;
procedure BoxProperties(L, H, W : Double; var A, V : Double);
begin
  A := BoxArea(L, H, W);
  V := BoxVolume(L, H, W);
end;
exports
  BoxProperties;
begin
end.
   To save your project, on the main menu, click File -> Save All.    To build the DLL, on the main menu, click Project -> Build DLLProject.

                        Save your project

  

 -------------------------------------------

Now we will create a project to use our DLL.

1.       Start a new application with the default form. 2.       To save the project, on the main menu, click File -> Save All. 3.       Locate and display the same folder in which the DLL project was created. 4.       Save the unit as Main and the project as ParaRect Design the form as follows:

 

1.       Besides the labels you can read, from top -> down, the form has five Edit controls named edtLength, edtHeight, edtWidth, edtArea, and edtVolume respectively. Then add two buttons named btnCalculate and btnExit.

2.       On the form, double-click the Exit button  3.       On the form, double-click the Calculate button. 4.       Return to the form and double-click the Calculate button.

5.       To call the DLL, change the file as follows:
 

 

unit Main;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls;
 
type
  TForm1 = class(TForm)
    edtLength: TEdit;
    edtHeight: TEdit;
    edtWidth: TEdit;
    edtArea: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    btnVolume: TLabel;
    edtVolume: TEdit;
    btnCalculate: TButton;
    Shape1: TShape;
    btnExit: TButton;
    procedure btnExitClick(Sender: TObject);
    procedure btnCalculateClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  procedure BoxProperties(L, H, W : Double; var A, V : Double); external 'DLLProject.dll';
 
implementation
 
{$R *.DFM}
 
procedure TForm1.btnExitClick(Sender: TObject);
begin
  Close;
end;
 
procedure TForm1.btnCalculateClick(Sender: TObject);
var
  Length, Height, Width, Area, Volume : Double;
begin
  Length := StrToFloat(edtLength.Text);ht := StrToFloat(edtHeight.Text);
  Width := StrToFloat(edtWidth.Text);
  BoxProperties(Length, Height, Width, Area, Volume);
  edtArea.Text := FloatToStr(Area);
  edtVolume.Text := FloatToStr(Volume);
end;
end. 
1.       To save the project, on the main menu, click File -> Save All.  To execute the project, on the main menu, click Run -> Run.