列表3.4.txt

来自「klinux书籍的配套光盘。可以学习学习。」· 文本 代码 · 共 47 行

TXT
47
字号
【列表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 + =
减小字号Ctrl + -
显示快捷键?