Delphi 7 Dynamic Linking
In object pascal for Windows a DLL can be linked into a unit at compile time and at runtime.
Compile Time Dyanamic Linking
The DLL will be loaded by the operating system and linked when the programme is started.
unit sqlite3;
interface
function sqlite3_open(filename: PChar; ctx: Pointer): Integer;
cdecl; external 'sqlite3.dll';
function sqlite3_close(ctx: Cardinal): Integer; cdecl;
external 'sqlite3.dll';
implementation
end.
cdecl
is for the calling convention used, it is often stdcall
for system
dlls.
Runtime Dynamic Linking
The DLL will be opened loaded into the process virtual address space when
LoadLibrary
is called and each function must be assigned individually via
GetProcAddress
.
unit sqlite3;
interface
var
sqlite3_open : function(filename: PChar; ctx: Pointer): Integer; cdecl;
sqlite3_close : function(ctx: Cardinal): Integer; cdecl;
function sqlite3_loadlibrary: Integer;
function sqlite3_freelibrary: Integer;
implementation
uses
Windows;
var
sqlite3_handle: Cardinal;
function sqlite3_loadlibrary: Integer;
begin
sqlite3_handle := LoadLibrary('sqlite3.dll');
sqlite3_open := GetProcAddress(sqlite3_handle, 'sqlite3_open');
sqlite3_close := GetProcAddress(sqlite3_handle, 'sqlite3_close');
end;
function sqlite3_freelibrary: Integer;
begin
FreeLibrary(sqlite3_handle);
end;
cdecl
is for the calling convention used, it is often stdcall
for system
dlls.
References
For the DLL used in this example see SQLite3 Compile Windows DLL.