Delphi DLL Loader

Nothing special, just a little helper class for loading DLLs and getting hold of the function entry points.

unit DllLoader;

interface

uses
  Windows;

type
  TDllLoader = class
    private
      ModuleHandle : HMODULE;
    public
      constructor Create(FileName : string);
      destructor Destroy; override;
      function GetProcAddress(ProcedureName : string) : FARPROC;
  end;

////////////////////////////////////////////////////////////////////////////////

implementation

uses
  SysUtils;

////////////////////////////////////////////////////////////////////////////////
{ TDllLoader }

constructor TDllLoader.Create(FileName: string);
begin
  inherited Create;
  ModuleHandle := LoadLibrary(PChar(FileName));
  if ModuleHandle = 0 then
    RaiseLastWin32Error;
end;

////////////////////////////////////////////////////////////////////////////////

destructor TDllLoader.Destroy;
begin
  if ModuleHandle <> 0 then
    FreeLibrary(ModuleHandle);
  inherited;
end;

////////////////////////////////////////////////////////////////////////////////

function TDllLoader.GetProcAddress(ProcedureName: string): FARPROC;
begin
  Result := Windows.GetProcAddress(ModuleHandle, PChar(ProcedureName));
  if (Result = nil) then
    RaiseLastWin32Error;
end;

end.