Friday, March 5, 2010

Delphi 2010 Delayed Dynamic Link Libraries

Traditionally, Dynamic Link Libraries (DLLs) can be loaded in two different ways: implicit or explicit.

unit DelayedHandler;
interface
uses

SysUtils;

type
ELoadLibrary = class(Exception);
EGetProcAddress = class(Exception);

implementation

function
DelayedHandlerHook(dliNotify: dliNotification;
pdli: PDelayLoadInfo): Pointer; stdcall;
begin
if
dliNotify = dliFailLoadLibrary then
raise ELoadLibrary.Create('Could not load ' + pdli.szDll)
else
if
dliNotify = dliFailGetProcAddress then
if
pdli.dlp.fImportByName then
raise
EGetProcAddress.Create('Could not load ' +
pdli.dlp.szProcName + ' from ' + pdli.szDll)
else
raise
EGetProcAddress.Create('Could not load index ' +
IntToStr(pdli.dlp.dwOrdinal) + ' from ' + pdli.szDll)
end;

initialization
SetDliFailureHook(DelayedHandlerHook);
finalization
SetDliFailureHook(nil);
end.

No comments: