|
| 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