System Delphi

Title: Try loading DLL in dynamic mode. IT`S EASY!
Question: Static DLL loading is hard to handle? Try loading in dynamic mode...
Answer:
If you'll use DLL in a Delphi Program, you can load it in two types:
- static loading
- dynamic loading
Let me see:
CREATING A SIMPLE DLL LIBRARY
=============================
My example library only calculates the bouble of a number:
Project file name: c:\example\exdouble\exdouble.dpr
----------------------------------------------------------------------library ExDouble;
// my simple dll
function calc_double ( r: real ): real; stdcall;
begin
result := r * 2;
end;
exports
calc_double index 1;
end;
----------------------------------------------------------------------
OK
My simple library is functional. Now we will load it...
STATIC DLL LOADING
==================
In this loading type, its more simple, but you will need put the DLL
file in your directory or Windows directory, or Windows\System,
Windows\Command. But if it not is on this directory, Windows will
display an error message box (DLL not found, more or less this) and
you cannot handle this (IN THIS TYPE[LOADING MODE]).
----------------------------------------------------------------------unit untMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
font style="color: #990000"bfunction calc_double ( r: real ): real; stdcall; external 'ExDouble.dll';/b/font
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
// the message box will shows 21 (oohhhhhh!)
showMessage ( floatToStr ( calc_double ( 10.5 ) ) );
end;
end.
----------------------------------------------------------------------
DYNAMIC DLL LOADING
===================
In dynamic loading, you will need to type more code, but it's easy to handle the process.
Before loading your application, you can do a "find process" to find your functions library.
You can see the code below.
----------------------------------------------------------------------
unit untMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
font style="color: #990000"bTcalc_double = function ( r: real ): real;/b/font
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
hndDLLHandle: THandle;
font style="color: #990000"bcalc_double: Tcalc_double;/b/font
begin
try
// load dll in dinamic type(mode)
hndDLLHandle := loadLibrary ( 'ExDouble.dll' );
if hndDLLHandle 0 then begin
// get function address
@calc_double := getProcAddress ( hndDLLHandle, 'calc_double' );
// if function address exists
if addr ( calc_double ) nil then begin
// shows result (it's really 21...)
showMessage ( floatToStr ( calc_double ( 10.5 ) ) );
end else
// DLL not found ("handleable")
showMessage ( 'Function not exists...' );
end else
// DLL not found ("handleable")
showMessage ( 'DLL not found...' );
finally
// free the DLL handle
freeLibrary ( hndDLLHandle );
end;
end;
end.
----------------------------------------------------------------------
If I can help you in any type, good profit...
Carlos A. Longen
----------------------------------------------------------------------