Delphi采用接口实现DLL调用

时间:2021-09-10 08:22:00

Delphi使用模块化开发,可以采用DLL或者BPL,两者的区别是BPL只能被同版本的Delphi使用,DLL可以被不同版本和不同开发工具的开发的软件调用。

因此我们的软件大多使用Delphi作为界面以及部分DLL模块的开发工具。

DLL模块之间通过接口方式调用。

1.对象创建采用工厂模式,每个DLL负责某个对象或若干个对象的创建及释放,例如:

DLL工程为http客户端(prjHttp.DLL)模块,通过DLL导出的GetHttpClientFactory获取http客户端工厂接口,通过接口创建Http客户端和释放Http客户端,工程

包括3个文件:工程文件,实现单元,接口单元。

调用此DLL的程序仅需要包含接口单元。

DLL工程文件

library prjHttp; uses System.SysUtils, System.Classes, utHTTPClient in utHTTPClient.pas; {$R *.res} exports GetHttpClientFactory; end.

utHttpClient示例

unit utHttpClient; interface uses utBaseObject, utHttpInterface, Classes, SysUtils; type ......... THTTPClientConnection = class(TIntObject, IHTTPClientConnection) public function Connect: Boolean; function Info: IHTTPClientConnectionInfo; function TcpConnection: ITcpConnection; function DataConnection: IConnection; function Param:IHttpClientConnectionParam; public constructor Create; destructor Destroy; override; end; THttpClientConnectionFactory = class(TIntObject, IHttpClientConnectionFactory) protected FObjectPool: THTTPClientConnectionPool; public constructor Create; destructor Destroy; override; procedure CreateHttpClient(out Conn: IHTTPClientConnection); procedure DestroyHttpClient(var aClient); end; function GetHttpClientFactory: IHttpClientConnectionFactory; implementation ............ var HttpClients: THttpClientConnectionFactory; function GetHttpClientFactory: IHttpClientConnectionFactory; begin if not Assigned(HttpClients) then HttpClients := THttpClientConnectionFactory.Create; Result := HttpClients; end; initialization finalization if Assigned(HttpClients) then FreeAndNil(HttpClients); end.

utHttpInterface接口文件示例

unit utHttpInterface; interface uses utBaseInterface; const IID_IHTTPClientConnectionInfo = {24C3D6BF-EC3D-4783-AD98-A5C6E4F24F19}; IID_IHTTPClientConnectionParam = {0FA49A71-48BF-40CD-9D77-63B233C4F717}; IID_IHTTPClientConnection = {78C39E26-A690-4022-9E97-6035768CE75C}; IID_IHTTPClientConnectionEvent = {2FB0AC19-9994-4E77-B105-121192943EBC}; IID_IHttpClientConnectionFactory = {429C5C2B-C1E3-4871-9631-E3B943619EFD}; GUID_IHTTPClientConnectionInfo: TGUID = IID_IHTTPClientConnectionInfo; GUID_IHTTPClientConnectionParam: TGUID = IID_IHTTPClientConnectionParam; GUID_IHTTPClientConnection: TGUID = IID_IHTTPClientConnection; GUID_IHTTPClientConnectionEvent: TGUID = IID_IHTTPClientConnectionEvent; GUID_IHttpClientConnectionFactory = IID_IHttpClientConnectionFactory; type IHttpClientConnectionParam = interface [{0FA49A71-48BF-40CD-9D77-63B233C4F717}] function TcpParam: ITcpConnectionParam; function GetMethod: PAnsiChar; function GetPathAndParams: PAnsiChar; function GetAgent: PAnsiChar; function GetHeader: PAnsiChar; function GetData: PAnsiChar; function GetUserName: PAnsiChar; function GetPassword: PAnsiChar; procedure SetValue(const ServerAddr: PAnsiChar; const ServerPort: Integer; const UserName, Password, Method, PathAndParams, Agent, Header, Data: PAnsiChar); end; IHTTPClientConnectionInfo = interface(ITcpConnectionInfo) [{24C3D6BF-EC3D-4783-AD98-A5C6E4F24F19}] function Auth: PAnsiChar; end; IHTTPClientConnection = interface; IHTTPClientConnectionEvent=interface [{2FB0AC19-9994-4E77-B105-121192943EBC}] procedure OnHeader(const Http:IHTTPClientConnection; const Header:Pointer; const HeaderLenght:NativeInt); procedure OnStartReceiveContent(const Http:IHTTPClientConnection; const ContentLength:NativeInt); procedure OnReceiveProgress(const Http:IHTTPClientConnection; const ContentLenght, ContentReceived:NativeInt); procedure OnError(const Http:IHTTPClientConnection; const ErrStr:PAnsiChar); end; THttpClientConnectionEvent = (heHeader, heStartReceiveContent, heReceiveProgress, heError); IHTTPClientConnection = interface [IID_IHTTPClientConnection] function Connect: Boolean; function Info: IHTTPClientConnectionInfo; function TcpConnection: ITcpConnection; function DataConnection: IConnection; function Param:IHttpClientConnectionParam; end; IHttpClientConnectionFactory = interface [IID_IHttpClientConnectionFactory] procedure CreateHttpClient(out Conn: IHTTPClientConnection); procedure DestroyHttpClient(var aClient); end; implementation end.