从DLL中返回一个接口对象
添加一个Unit单元
把类的对外要用到的自定义类型以及接口对象的声明
都写上,
比如单元名称:unitMyInterface
ISendEmail = interface(IInterface)
function getEmailType: TEmailType;
function getPassWord_pop3: WideString;
function getPassWord_smtp: WideString;
property SmtpHost : WideString read getSmtpHost write setSmtpHost;
property Pop3Host : WideString read getpop3Host write setpop3Host;
property SmtpPort : integer read getSmtpPort write setSmtpPort;
function DownLoadOneEmail(const iEmailIndex : Integer):Boolean;
function CheckMessages : Integer;
function RetrievePOPHeaders:Integer;
procedure Pop3Disconnect ;
end;
在另外一个unit里具体来实现接口中的方法:
单元名称:UnitFunction
这个单元要uses unitMyInterface;
TSendEmail = class(TInterfacedObject,ISendEmail)
private
FSmtpHost : WideString;
Fpop3Host : WideString;
FUserName_smtp : WideString;
FPassWord_smtp : WideString;
public
property SmtpHost : WideString read getSmtpHost write setSmtpHost;
property Pop3Host : WideString read getpop3Host write setpop3Host;
function DownLoadOneEmail(const iEmailIndex : Integer):Boolean;
function CheckMessages : Integer;
constructor Create ;
destructor Destroy; override;
end;
otherwise
DLL单元
library Send;
uses
ShareMem,
SysUtils,
Classes,
ActiveX,
uInf in 'uInf.pas',
UnitFunction in 'UnitFunction.pas';
function CreateSendObj(): ISendEmail; stdcall;
//这一点很重要,只要是需要输出的涵数都要使用stdcall不管是在DLL文件中还是在其它的文件中。
//不要采用String类型的参数或返回值,可以用Variant或者是PChar或是WideString
begin
Result := ISendEmail(TSendEmail.Create);
end;
exports
CreateSendObj;
begin
CoInitialize(nil);
end.
采用静态调用:
function CreateSendObj: ISendEmail;stdcall external 'Send.dll';
implementation
var
MySend : ISendEmail;
begin
MySend := CreateSendObj;
MySend.DownLoadOneEmail(1);
...
...
MySend := nil; // 接口对象和类对象不一样,不需要进行释放,赋为nil即可
end;