Quick start manual
9-2
Delphi Language Guide
Calling dynamically loadable libraries
Static loading
The simplest way to import a procedure or function is to declare it using the external
directive. For example,
On Windows:
procedure DoSomething; external 'MYLIB.DLL';
On Linux:
procedure DoSomething; external 'mylib.so';
If you include this declaration in a program, MYLIB.DLL (Windows) or mylib.so
(Linux) is loaded once, when the program starts. Throughout execution of the
program, the identifier DoSomething always refers to the same entry point in the same
shared library.
Declarations of imported routines can be placed directly in the program or unit
where they are called. To simplify maintenance, however, you can collect external
declarations into a separate “import unit” that also contains any constants and types
required for interfacing with the library. Other modules that use the import unit can
call any routines declared in it.
For more information about external declarations, see “External declarations” on
page 6-6.
Dynamic loading
You can access routines in a library through direct calls to OS library functions,
including LoadLibrary, FreeLibrary, and GetProcAddress. In Windows, these functions
are declared in Windows.pas; on Linux, they are implemented for compatibility in
SysUtils.pas; the actual Linux OS routines are dlopen, dlclose, and dlsym (all declared
in libc; see the man pages for more information). In this case, use procedural-type
variables to reference the imported routines.
For example, on Windows or Linux:
uses Windows, ...; {On Linux, replace Windows with SysUtils }
type
TTimeRec = record
Second: Integer;
Minute: Integer;
Hour: Integer;
end;
TGetTime = procedure(var Time: TTimeRec);
THandle = Integer;
var
Time: TTimeRec;
Handle: THandle;
GetTime: TGetTime;
ƒ