2005. január 18., kedd

Try loading DLL in dynamic mode


Problem/Question/Abstract:

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;

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

function calc_double(r: real): real; stdcall; external 'ExDouble.dll';

{$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
  Tcalc_double = function(r: real): real;

  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;
  calc_double: Tcalc_double;
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.

Nincsenek megjegyzések:

Megjegyzés küldése