-
Notifications
You must be signed in to change notification settings - Fork 33
/
uFxtDelayedHandler.pas
78 lines (65 loc) · 1.96 KB
/
uFxtDelayedHandler.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{ uFxtDelayedHandler.pas } // version: 2020.0615.1000
// Failure load library by "delayed" : addeed exception information
// http://docwiki.embarcadero.com/Libraries/XE6/en/SysInit.SetDliFailureHook2
// http://docwiki.embarcadero.com/CodeExamples/XE6/en/DelayedLoading_%28Delphi%29
// http://www.drbob42.com/examines/examinc1.htm
//
// sample: function func_add(x,y: double): double; overload; stdcall
// external 'sample.dll' name 'func_add_float' delayed;
//
unit uFxtDelayedHandler;
interface
{$IF CompilerVersion >= 21.00}
{$IF CompilerVersion >= 33.00} // DX 10.3 Rio
{$WARN EXPLICIT_STRING_CAST OFF} // W1059
{$IFEND}
uses
SysUtils;
type
ELoadLibrary = class(Exception);
EGetProcAddress = class(Exception);
{$IFEND}
implementation
{$IF CompilerVersion >= 21.00}
var
LOldFailureHook: TDelayedLoadHook;
function DelayedHandlerHook(dliNotify: dliNotification; pdli: PDelayLoadInfo): Pointer; stdcall;
var
S: string;
begin
if dliNotify = dliFailLoadLibrary then
begin
raise ELoadLibrary.Create('Could not load library "' + string(AnsiString(pdli.szDll)) + '"');
end
else if dliNotify = dliFailGetProcAddress then
begin
if pdli.dlp.fImportByName then
begin
S := '"' + string(AnsiString(pdli.dlp.szProcName)) + '"';
end
else
begin
S := 'index ' + IntToStr(pdli.dlp.dwOrdinal);
end;
S := 'Could not load function ' + S + ' from library "' + string(AnsiString(pdli.szDll)) + '"';
raise EGetProcAddress.Create(S);
end;
if Assigned(LOldFailureHook) then
Result := LOldFailureHook(dliNotify, pdli)
else
Result := nil;
end;
initialization
{$IF CompilerVersion >= 24.00}
LOldFailureHook := SetDliFailureHook2(DelayedHandlerHook);
{$ELSE}
LOldFailureHook := SetDliFailureHook(DelayedHandlerHook);
{$IFEND}
finalization
{$IF CompilerVersion >= 24.00}
SetDliFailureHook2(LOldFailureHook);
{$ELSE}
SetDliFailureHook(LOldFailureHook);
{$IFEND}
{$IFEND IF CompilerVersion >= 21.00}
end.