This feature, known as download latency , was added in Delphi 2010.
Using your code as an example, you can write your import as follows:
function Inp(PortAddress: Integer): Integer; stdcall; external 'inpout32.dll' name 'Inp32' delayed;
Linking to this external function will only be performed the first time the function is called. If the binding fails, an exception occurs at run time.
You can use SetDliNotifyHook
and SetDliFailureHook
to customize the delay loading behavior if you need even more fine-grained control.
Some blog articles that complement the product documentation:
In older versions of Delphi, you can use LoadLibrary
and GetProcAddress
. Or, if you want to smooth things out a bit, I can heartily recommend the Hallvard Vassbotn delay loading class, which he describes in this blog post . This code wraps all the boiler plates when calling LoadLibrary
and GetProcAddress
and is only slightly more cumbersome to use than the new built-in Delphi 2010 function.
I have successfully used the Hallvard library for many years. One minor word of caution is that it is not thread safe, so if multiple threads try to associate with a function at the same time, then the code may fail. This is easy enough to fix by adding internal locks to Hallvard code.
source share