WriteLn('The time is ', Hour, ':', Minute, ':', Second);
end;
FreeLibrary(Handle);
end;
end;
当你用这种方式导入例程时,直到LoadLibrary调用开始执行,库才被调入,库后来通过调用进行释放。这使你能节省内存,并且在某些需要的库不存在的情况下也能运行你的程序。同样的例子在Linux下也可以这样实现:
uses Libc, ...;
type
TTimeRec = record
Second: Integer;
Minute: Integer;
Hour: Integer;
end;
TGetTime = procedure(var Time: TTimeRec);
THandle = Pointer;
var
Time: TTimeRec;
Handle: THandle;
GetTime: TGetTime;
...
begin
- 132 - FreeLibrary
Libraries and packages
Handle := dlopen('datetime.so', RTLD_LAZY);
if Handle <> 0 then
begin
@GetTime := dlsym(Handle, 'GetTime');
if @GetTime <> nil then
begin
GetTime(Time);
with Time do
WriteLn('The time is ', Hour, ':', Minute, ':', Second);
end;
dlclose(Handle);
end;
end;
采用这种方式导入例程,直到dlopen调用开始执行,共享目标文件才被调入,目标文件后来通过调用dlclose进行释放。这使你能节省内存,并且在某些需要的库不存在的情况下也能运行你的程序。 Writing dynamically loadable libraries(编写动态调入库) Writing dynamically loadable libraries(编写动态调入库)
动态调入库的主源文件和程序的一样,除了它以关键字library开始(取代program)。
只有被库明确输出的例程才能被其它库或程序导入,下面的例子演示了库输出两个函数,Min和Max。 library MinMax;
function Min(X, Y: Integer): Integer; stdcall;
begin
if X < Y then Min := X else Min := Y;
end;
function Max(X, Y: Integer): Integer; stdcall;
begin
if X > Y then Max := X else Max := Y;
end;
exports
Min,
Max;
begin
end.
若要你的库对其它语言编写的程序是可见的,最安全的办法是在声明输出函数时指定stdcall调用约定,其它语言或许不支持Object Pascal默认的register调用约定。
Libraries can be built from multiple units. In this case, the library source file is frequently reduced to a uses clause, an exports clause, and the initialization code. For example,
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-23665-99.html
想出名也不能介样吧