1、在dll工程中加类和接口

type
IMyClass = interface
['{A1B2C3D4-E5F6-4789-ABCD-1234567890AB}'] // 接口 ID
function Foo(inInt: Integer):Integer; stdcall;
end;

TMyCls = class(TInterfacedObject, IMyClass)
public
function Foo(inInt: Integer):Integer; stdcall;
end;

function TMyCls.Foo(inInt: Integer):Integer;stdcall;
begin
Result:=inInt*inInt;
end;

function CreateMyClass: IMyClass; stdcall;
begin
Result := TMyCls.Create; // 创建并返回一个接口实例
end;

exports
CreateMyClass;

 

2、调用

type

IMyClass = interface
['{A1B2C3D4-E5F6-4789-ABCD-1234567890AB}'] // 接口 ID
function Foo(inInt: Integer):Integer; stdcall;
end;
function CreateMyClass():IMyClass;stdcall; external 'DllClass.dll';

 

var
MyObj: IMyClass;
outint:Integer;
begin
MyObj:=CreateMyClass();
outint:= MyObj.Foo(5);
ShowMessage(IntToStr(outint));
end;

 根据盒子论坛dbyoung的指点,还有一种用虚函数的方法。

1、dll工程

type
TMyCls = class(TObject)
public
function Foo(inInt: Integer):Integer; virtual;
end;

function TMyCls.Foo(inInt: Integer):Integer;
begin
Result:=inInt*inInt;
end;

function CreateMyClass: TMyCls; stdcall;
begin
Result := TMyCls.Create;
end;
exports
CreateMyClass;

2、调用

TMyCls = class(TObject)
public
function Foo(inInt: Integer):Integer; virtual;abstract;
end;
function CreateMyClass():TMyCls;stdcall; external 'DllClass.dll';

 

MyObj:=CreateMyClass();
outint:= MyObj.Foo(5);

posted on 2024-08-02 11:51  禁卫军  阅读(57)  评论(0编辑  收藏  举报