Static Loading

Static loading is the simpler of the two possible ways to load a dynamic link library. Static loading is also known as load-time dynamic linking, because the used DLLs get automatically loaded when the application starts.

To statically load a DLL, you have to copy the routine declaration to the calling application and mark it with the external directive, which tells the compiler that the routine resides in either an object file or a dynamic link library.

When you're importing routines from a dynamic link library, you have to mark the routine with the external directive followed by the name of the DLL that contains the routine's implementation. Here's how you can import the Max3 function from the image from book FirstLib.dll:

function Max3(Num1, Num2, Num3: Integer): Integer;   stdcall; external 'FirstLib.dll'; 

If you want, you can even rename the routine when importing it. To import a routine by renaming, declare the routine with a different name and specify the original name at the end of the declaration using the name directive:

function Max(Num1, Num2, Num3: Integer): Integer;   stdcall; external 'FirstLib.dll' name 'Max3';

You can also import a function from a dynamic link library by creating an import unit and writing the standard routine header in the interface part of the unit and its external implementation in the implementation part of the unit. The following listing shows the entire image from book FirstLib.dll import unit.

Listing 21-3: The FirstLib.dll import unit

image from book
unit FirstLibIntf; interface function Max3(Num1, Num2, Num3: Integer): Integer; stdcall; implementation const   FirstLib = 'FirstLib.dll';     { tell the compiler that the implementation of the       Max3 function is in the FirstLib.dll }   function Max3; external FirstLib; end.
image from book

Once you've created the DLL and its import unit, you have to test the DLL to see if the routines work. Since you can't run the DLL, you have to create a test application that will use the DLL. The fastest way to create a test application is to create a project group by adding a new project to the current one. You can do this by right-clicking on the ProjectGroup1 item in the Project Manager and selecting Add New Project, as shown in Figure 21-2.

image from book
Figure 21-2: Creating a test application for the DLL

Now that you have the DLL, the import unit, and an empty VCL Forms application (or other) project, you can use the DLL's routines as you would all others — add the import unit to the uses list of the test application and call whichever routine you need (see Listing 21-3). The test application is displayed in Figure 21-3.

Listing 21-3: Testing the Max3 routine imported from the FirstLib.dll

image from book
unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs, StdCtrls, XPMan, FirstLibIntf; type   TMainForm = class(TForm)     procedure Max3ButtonClick(Sender: TObject);   private     { Private declarations }   public     { Public declarations }   end; var   MainForm: TMainForm; implementation {$R *.dfm} procedure TMainForm.Max3ButtonClick(Sender: TObject); var   LargestNumber: Integer; begin   LargestNumber := Max3(StrToInt(Edit1.Text),     StrToInt(Edit2.Text), StrToInt(Edit3.Text));   MessageDlg(Format('The largest number is %d.', [LargestNumber]),     mtInformation, [mbOK], 0); end; end.
image from book

image from book
Figure 21-3: The FirstLib.dll test application

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: