Skip to content

Commit 3558c95

Browse files
committed
Failure load library by "delayed" : addeed exception information
1 parent ef06f78 commit 3558c95

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

demo/Project1.dpr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ program Project1;
55
uses
66
Forms,
77

8+
{$IFDEF UNICODE}{$IFNDEF FPC}
9+
uFxtDelayedHandler in '..\uFxtDelayedHandler.pas',
10+
{$ENDIF}{$ENDIF}
11+
812
libssh2 in '..\libssh2.pas',
913
libssh2_publickey in '..\libssh2_publickey.pas',
1014
libssh2_sftp in '..\libssh2_sftp.pas',

uFxtDelayedHandler.pas

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

0 commit comments

Comments
 (0)