Dynamic Loading

You can access routines in a library through direct calls to Win32 APIs, including LoadLibrary, FreeLibrary, and GetProcAddress. 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.

TTimeRec = record Second: Integer; Minute: Integer; Hour: Integer; end;

TGetTime = procedure(var Time: TTimeRec); THandle = Integer;

Time: TTimeRec; Handle: THandle; GetTime: TGetTime;

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.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment