我在Dll里写了一个包含自定义接口(ITest)的TMyButton(继承于TButton), 用一个函数创建这种类的对象并返回成TButton类型.
在测试程序里, 我用动态加载的方法使用这个Dll, 创建了一个TMyButton, 然后调用TMyButton的方法GetInterface取得了它的ITest接口, 发现这个接口是可用的.
问题出现在关闭测试程序时, 等到FormDestroy(或者重载的Destroy)返回后, 弹出一个EAccessViolation异常. 我试过, 使用静态链接的方法不会出现这个异常. 不知道是不是Dll的释放,接口的释放没处理好... 请高手指点啊!!!
这里是源代码:
//-----------------Dll部分---------------------------
library Dll;uses
SysUtils, Classes, Controls, StdCtrls, Dialogs, Intf in 'Intf.pas';{$R *.res}
type TMyButton=class(TButton, ITest)
published
procedure SayHello;
end;{ TMyButton } procedure TMyButton.SayHello;
begin
ShowMessage(self.Caption + ' says Hello !');
end; function Create(Owner: TWinControl): TButton;
begin
result := TMyButton.Create(Owner);
result.Parent := Owner;
end; exports Create;
begin
end.//-------------------公共使用的文件Intf.pas--------------------
unit Intf;interface
uses Controls, StdCtrls;type ITest=interface
['{6BDA0612-5842-4865-9550-154C34F8FD98}']
procedure SayHello;
end;
TCreate=function(Owner: TWinControl): TButton;implementationend.//-------------------测试程序部分---------------------------------
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Intf, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
Button: TButton;
DllInstance: HINST;
TheInterface: ITest;
public
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
DllInstance := LoadLibrary('Dll.dll');
Button := TCreate(GetProcAddress(DllInstance, 'Create'))(self);
with Button do
begin
Caption := 'Tom';
if GetInterface(ITest, TheInterface)=false then
ShowMessage('GetInterface failed.');
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
TheInterface.SayHello;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if TheInterface<>nil then
TheInterface._Release;
if Button<>nil then
Button.Free;
if DllInstance<>0 then
FreeLibrary(DllInstance);
end;end.
在测试程序里, 我用动态加载的方法使用这个Dll, 创建了一个TMyButton, 然后调用TMyButton的方法GetInterface取得了它的ITest接口, 发现这个接口是可用的.
问题出现在关闭测试程序时, 等到FormDestroy(或者重载的Destroy)返回后, 弹出一个EAccessViolation异常. 我试过, 使用静态链接的方法不会出现这个异常. 不知道是不是Dll的释放,接口的释放没处理好... 请高手指点啊!!!
这里是源代码:
//-----------------Dll部分---------------------------
library Dll;uses
SysUtils, Classes, Controls, StdCtrls, Dialogs, Intf in 'Intf.pas';{$R *.res}
type TMyButton=class(TButton, ITest)
published
procedure SayHello;
end;{ TMyButton } procedure TMyButton.SayHello;
begin
ShowMessage(self.Caption + ' says Hello !');
end; function Create(Owner: TWinControl): TButton;
begin
result := TMyButton.Create(Owner);
result.Parent := Owner;
end; exports Create;
begin
end.//-------------------公共使用的文件Intf.pas--------------------
unit Intf;interface
uses Controls, StdCtrls;type ITest=interface
['{6BDA0612-5842-4865-9550-154C34F8FD98}']
procedure SayHello;
end;
TCreate=function(Owner: TWinControl): TButton;implementationend.//-------------------测试程序部分---------------------------------
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Intf, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
Button: TButton;
DllInstance: HINST;
TheInterface: ITest;
public
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
DllInstance := LoadLibrary('Dll.dll');
Button := TCreate(GetProcAddress(DllInstance, 'Create'))(self);
with Button do
begin
Caption := 'Tom';
if GetInterface(ITest, TheInterface)=false then
ShowMessage('GetInterface failed.');
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
TheInterface.SayHello;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if TheInterface<>nil then
TheInterface._Release;
if Button<>nil then
Button.Free;
if DllInstance<>0 then
FreeLibrary(DllInstance);
end;end.
解决方案 »
- 求一个获取COM口和LPT口的代码
- Delphi网络数据采集
- 在单元中用$DEFINE定义的Symbol在引用他的单元中可见吗?
- 当代中国大学毕业生地悲哀-人事局强行扣留毕业生档案,是何道理?
- COM+组件的结构(应用于进销存系统),实践中。。。
- 关于dll中的方法重复怎么办
- 怎样用语句把BDE中的STANDARD的引擎删除?
- reset,rewrite的第二个参数表示什么意思,根据什么来设定它的值
- 明华读卡器的动态库MWIC_32.dll的DES加密问题
- createsemaphorek跟ReleaseSemaphore的问题
- 上传文件到SQL SERVER数据库,并可从数据下载保存为文件,上传下载均有进度条;实现C/S模式通过数据库在线升级
- 在线等待:急。一个打印问题
TheInterface := nil;
aiirii(ari-爱的眼睛), 能否解释一下?