Delphi examples of static and dynamic invocations dll

Two ways of calling a DLL in the way Delphi  comparisons

     The purpose is to write DLL output routine calls for other programs, and therefore the routine in the DLL project file should output lead-out Exports keyword. In the application calls a DLL, the method used to declare a DLL, and the DLL to declare the format in the same statement. Access DLL calls routines in the static and dynamic calls in two ways. Static Invocation Interface embodiment is the portion of the cell lists from the DLL introduced routine External pointers; dynamic invocation mode is by calling the Windows API functions LoadLibrary include, GetProcAddress and FreeLibrary function DLL function introduced in dynamic routines .

  Less code needed to invoke a static way than the required dynamic invocation, but there are some shortcomings, first, if you want to load the DLL routine or not to introduce the DLL does not exist, it will automatically terminate when the program runs; two Once the DLL is loaded has been residing in the address space of the application, even if the DLL is no longer needed. Dynamic invocation can solve the above problems, it is only when the need to use the DLL introduced by LoadLibrary function, unloaded from memory by a FreeLibrary function, and you can specify different routines by calling the GetProcAddress function after use. Most importantly, if the specified DLL errors, up to an API call fails, the program will not lead to termination. The following specific examples will be described by using the method described in this embodiment call.

1 . Static invocation

 The DLL program creates a sample, which contains only a request and a function of two integers, the input two integers in the main program by calling the DLL which, to obtain two integers.

Program code for the DLL as follows:

AddNum Library; 
uses 
the SysUtils, 
the Classes; 

{$ R & lt * .res} 

function AddNumber (Num1, Num2: Integer): Integer; _stdcall; // summation function defined in 
 the begin 
  Result: = + Num1 Num2; 
 End; 
  Exports 
  AddNumber; // summing function leads 
 the begin 
End.

When the main program calls the DLL, first declared in the interface part of the function to call:

var
Form1: TForm1;

function AddNum(Num1,Num2:integer):integer;stdcall;external 'AddNum.dll' name 'AddNumber';

Then write the following code in the event of the button control:

TForm1.Button1Click Procedure (Sender: TObject); 
var 
 Number1, Number2: Integer; 
 the Sum: Integer; 
the begin 
 Number1: = strtoint (Edit1.Text); 
 Number2: = strtoint (Edit2.Text); 
 the Sum: = AddNum (Number1, Number2 ); // call the sum function results 
 Edit3.Text: = IntToStr (the sum); 
End;

2 . Dynamic invocation 

This example creates a DLL that shows the date, which contains a form.

ShowCalendar program defines a function that returns the date set in this form. Function is defined as follows: 

ShowCalendar function (AHandle: THandle; ACaption: String): TDateTime; 
var 
 DLLForm: TDLLForm; 
the begin 
 Application.Handle: = AHandle; 
 DLLForm: = TDLLForm.Create (the Application); // create and show the form 
 the try 
  DLLForm.Caption: = ACaption; 
  DLLForm.ShowModal; // display as schematically 
  Result: = DLLForm.calDLLCalendar.CalendarDate; // return date setting 
 the finally 
  DLLForm.Free; // exhausted unload the form 
 End; 
End;

In the project file using DLL exports ShowCalendar; statement starts the function. By following a simple application to test the DLL file. Create a new project file, in the form placing a Label control and a button control, write the following code in the OnClick event of the button control:

TMainForm.Button1Click Procedure (Sender: TObject); 
var 
 OneHandle: THandle; // define a variable handle 
the begin 
 OneHandle: = the LoadLibrary ( 'Clendar.dll'); // dynamic loading DLL, and returns its handle to 
 the try 
  IF OneHandle <> 0 then // if the loaded successfully acquired function address ShowCalendar 
   @ShowCalendar: = the GetProcAddress (OneHandle, 'ShowCalendar'); 
   IF Not (@ShowCalendar = nil) the then 
    // if the main form Label1 found the function display DLL form set date 
    Label1.Caption: = DateToStr (ShowCalendar (Application.Handle, Caption)) 
   the else 
    RaiseLastWin32Error; 
 a finally 
  FreeLibrary (OneHandle); // call is completed to recover the occupied resource DLL 
 End; 
End;

From the above program can be seen in the DLL dynamic invocation way than static invocation of significant advantages. DLL routine is transferred only when used, is unloaded after the run, greatly reducing system resource. You can explicitly specify the full path to the DLL when calling LoadLibrary function, if no path, first find the application to load the runtime directory, then the path to the Windows System directory and the system environment variable Path set.  

  

  

  

Guess you like

Origin www.cnblogs.com/approx/p/12110916.html