Quick start manual

Libraries and packages
9-3
Calling dynamically loadable libraries
begin
Handle := LoadLibrary('libraryname');
if Handle <> 0 then
begin
@GetTime := GetProcAddress(Handle, 'GetTime');
if @GetTime <> nil then
begin
GetTime(Time);
with Time do
WriteLn('The time is ', Hour, ':', Minute, ':', Second);
end;
FreeLibrary(Handle);
end;
end;
When you import routines this way, the library is not loaded until the code
containing the call to LoadLibrary executes. The library is later unloaded by the call to
FreeLibrary. This allows you to conserve memory and to run your program even
when some of the libraries it uses are not present.
This same example can also be written on Linux as follows:
uses Libc, ...;
type
TTimeRec = record
Second: Integer;
Minute: Integer;
Hour: Integer;
end;
TGetTime = procedure(var Time: TTimeRec);
THandle = Pointer;
var
Time: TTimeRec;
Handle: THandle;
GetTime: TGetTime;
ƒ
begin
Handle := dlopen('datetime.so', RTLD_LAZY);
if Handle <> 0 then
begin
@GetTime := dlsym(Handle, 'GetTime');
if @GetTime <> nil then
begin
GetTime(Time);
with Time do
WriteLn('The time is ', Hour, ':', Minute, ':', Second);
end;
dlclose(Handle);
end;
end;