📄 列表3.4.txt
字号:
【列表3.4】装入动态连接库。
{ HelloDynm.pas -- Dynamic interface to libhello }
unit HelloDynm;
interface
procedure SayHello;
implementation
uses SysUtils, Libc;
const
STR_LIB_NAME = 'libhello.so.1';
STR_HELLO_NAME = 'SayHello';
type
HelloProc = procedure; cdecl;
const
libHandle : Pointer = nil;
function GetLibraryHandle : Pointer;
begin
if libHandle = nil then
begin
libHandle := dlOpen (STR_LIB_NAME, RTLD_LAZY);
if libHandle = nil then
raise Exception. CreateFmt
('Unable to open library "%s"', [STR_LIB_NAME]);
end;
Result := libHandle;
end;
function GetLibrarySymbol (const fName: String): Pointer;
begin
Result := dlSym (GetLibraryHandle, PChar(fName));
if Result = nil then
raise Exception.CreateFmt
('Unable to locate function "%s" in library "%s".',
[STR_LIB_NAME, fName]);
end;
procedure SayHello;
const
HelloPtr : HelloProc = nil;
begin
if not Assigned (HelloPtr) then
HelloPtr := GetLibrarySymbol (STR_HELLO_NAME);
HelloPtr;
end;
initialization
finalization
if Assigned (libHandle) then
dlClose (libHandle);
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -