采用动态调用DLL时,可以用全局方式,先将其放在申明UNIT中,在程序创建调用初始化,
结束时释放。
//DLL申明UNIT dllclear.pas
unit dllclear;
interface
uses
Windows, SysUtils, hydeclear, libcs;
type
Thyclient = Record
libload:Boolean;
connected:Boolean;
hyclient:THandle;
apSendstrToHost:TFarproc;
apConnectToHost:TFarproc;
apDisConnectToHost:TFarproc;
apNetWatchOption:TFarproc;
end;
type
TNetWatchOption = function ():boolean;
TSendstrToHost = function (sendstr:string):boolean;
TConnectToHost = function ():boolean;
TDisConnectToHost = function ():boolean;
Function HyClientInit:boolean;
Function HyClientDestroy:boolean;
var
hyclient:Thyclient;
implementation
Function HyClientInit:boolean;
begin
try
hyclient.hyclient:=Loadlibrary('hyclient.dll');
hyclient.apNetWatchOption:=GetprocAddress(hyclient.hyclient,'NetWatchOption');
hyclient.apSendstrToHost:=GetprocAddress(hyclient.hyclient,'SendstrToHost');
hyclient.apConnectToHost:=GetprocAddress(hyclient.hyclient,'ConnectToHost');
hyclient.apDisConnectToHost:=GetprocAddress(hyclient.hyclient,'DisConnectToHost');
if hyclient.hyclient>0 then
begin
hyclient.libload:=True;
Result:=True;
end
else
begin
hyclient.libload:=False;
hyclient.connected:=False;
Result:=False;
end;
except
Result:=False;
end;
end;
Function HyClientDestroy:boolean;
begin
try
FreeLibrary(hyclient.hyclient);
hyclient.libload:=False;
hyclient.connected:=False;
Result:=True;
except
Result:=False;
end;
end;
end.
主程序调用
procedure Form.OnCreate(Sender:TObject);
begin
if fileexists('hyclient.dll') then
begin
if not HyclientInit then
ShowError('网络监测库初始化失败!');
end;
end;
procedure Form.FormClose(Sender: TObject
var Action: TCloseAction);
begin
if hyclient.libload then
hyclientdestroy;
end;