不能成功采用抽象类来实现Dll中对象的动态绑定机制,请赐教!(100)

  • 主题发起人 主题发起人 Dv
  • 开始时间 开始时间
D

Dv

Unregistered / Unconfirmed
GUEST, unregistred user!
正在研究DLL封装对象,研究了刘艺的《Delphi中的DLL封装和调用对象技术》,采用了抽象类来实现Dll中对象的动态绑定机制。但是创建对象错误。请高人赐教。例子如下:Dll工程:library DllTest;uses Test in 'Test.pas', ITest in 'ITest.pas';{$R *.res}function TTestObj: TITestClass; stdcall;begin Result := TTest;end;exports TTestObj; beginend.接口单元:unit ITest;interfaceuses Classes;type TITest = class(TObject) public constructor Create; virtual; abstract; destructor Destroy; virtual; abstract; end; TITestClass = class of TITest;implementationend.实现对象单元:unit Test;interfaceuses Classes, ITest;type TTest = class(TITest) public constructor Create; override; destructor Destroy; override; end;implementationconstructor TTest.Create;begin inherited Create;end;destructor TTest.Destroy;begin inherited Destroy;end;end.调用程序:unit host;interfaceuses Windows, Messages, SysUtils, Classes, Controls, Forms, StdCtrls, ComCtrls, ITest;type TfrmHost = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } FTest: TITest; public { Public declarations } end;var frmHost: TfrmHost;implementation{$R *.dfm}function TTestObj: TITest; stdcall; external 'DllTest.dll';procedure TfrmHost.Button1Click(Sender: TObject);begin FTest := TTestObj.Create; try Button1.Enabled := True; Button2.Enabled := Assigned(FTest); except FreeAndNil(FTest); end;end;procedure TfrmHost.Button2Click(Sender: TObject);begin if Assigned(FTest) then begin FreeAndNil(FTest); Button1.Enabled := True; end;end;end.
 
代码里没看到你引入DLL呢,程序里的TTestObj这个函数从哪里来了?----------------哦,看到了。代码看起来没问题,不知道你把DLL放到EXE同个目录了没有
 

Similar threads

后退
顶部