?? 列表3.5.txt
字號:
【列表3.5】交叉平臺庫接口單元。
{ HelloDynmX - Cross-platform dynamic library interface
unit HelloDynmX;
interface
procedure SayHello
implementation
uses SysUtils,
{$IFDEF LINUX}
Libc
{$ELSE}
Windows
{$ENDIF}
;
const
{$IFDEF LINUX}
STR_LIB_NAME= 'libhello.so.1';
LibnandleNull =nil;
{$ ELSE}
STR_LIB_NAME = 'sample';
LIbHandleNull = 0;
{$ENDIF}
// function names in library
STR_HELLO_NAME = 'SayHello';
type
{$IFDEF LINUX}
TLibHandle = Pointer;
{$ ELSE}
TLIbHandle = HModule;
{$ENDIF}
HelloProc = procedure;
{$IFDEF LINUX} cdecl; {$ELSE} stdeall: {$ENDIF}
const
LibHandle : TLIbHandle = LIbHandleNull;
function GetLIbraryHandle : TLibHandle;
begin
if libHandle =libHandleNull then
begin
{$IFDEF LINUX}
libHandle :=dlopen (STR_LIB_HAME, RTLD_LAZY);
{$ELSE}
libHandle := LoadLIbrary (STR_LIB_NAME);
{$ENDIF}
if libHandle = libHandleNun then raise Exception.CreateFmt
('Unable to open library "%s"', [STR_LIB_NAME]);
end:
Result := libHandle;
end;
function GetLibrarySymbol (const fName: String): Pointer;
var
bErr : boolean;
begin
{$IFDEF LINUX}
dlerror; // call to clear error code
Result :=dlsym (GetLibraryHandle, PChar(fName));
bErr := (dlerror <> nil);
{$ELSE}
Result :=GetProcAddress (GetLibraryHandle, PChar(fName));
bErr := (Result = nil );
{$ENDIF}
if bErr then
raise Exception.CreateFmt
('Unable to locate symbol "%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;
dICIose (LibHandle);
LibHandle := LibHandleNull;
end;
initialization
// no initialization--needed for finalization
finalization
if libHandle <> LibHandleNull then
begin
{$IFDEF LINUX}
if dlclose (libHandle) =-1 then
{$ELSE}
if not FreeLlbrary (libHandle) then
{$ENDIF}
raise Exception.CreateFmt
('Error closing library "%s".', [STR_LIB_NAME]);
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -