大家来看一下,以下的DLL代码,Exe调用它一切都正常,就是退出Exe文件时出错!怎么办是好?!(0分)

  • 主题发起人 主题发起人 oicu
  • 开始时间 开始时间
O

oicu

Unregistered / Unconfirmed
GUEST, unregistred user!
//
library MyForms;
uses
SysUtils,
Forms,
Messages,
Windows,
Classes,
UFrmBaseForm in 'UFrmBaseForm.pas' {FrmBaseForm},
UFrmSingleBase in 'UFrmSingleBase.pas' {FrmSingleBase},
UFrmDoubleBase in 'UFrmDoubleBase.pas' {FrmDoubleBase},
UPublicObject in 'UPublicObject.pas',
UDataModal in 'UDataModal.pas' {DM: TDataModule};
{$R *.res}
var
DllApp:TApplication;

{ 用于初始化:保存DLL本身的Application,然后设置DLL的Application指向Host的Application }
procedure InitDLL(App:TApplication);stdcall;
begin
DllApp:=Application;
Application:=App;
end;

{ 善后工作:恢复DLL原来的Application }
procedure FreeDLL;stdcall;
begin
Application:=DllApp;
end;

{ 这是DLL的主要功能 !!}
procedure RunDLL(DLLName, FormName: PChar;
ExeAppHandle, ExeAppIconHandle: Integer) stdcall;
var
theClass: TPersistentClass;
TheForm: TFrmBaseForm;
begin

Application.Handle := ExeAppHandle;
Application.Icon.Handle := ExeAppIconHandle;
RegisterClasses([TFrmSingleBase, TFrmDoubleBase]);
theClass := GetClass('T' + FormName);
if theClass.InheritsFrom(TForm) and (theClass <> nil) then
begin
try
TheForm := TFrmBaseForm(theClass.Create).Create(nil);
try
TheForm.ShowDLLForm;
finally
FreeAndNil(TheForm);
end;
except
raise;
end;
end;
end;

exports
InitDLL,FreeDLL,RunDLL;
begin
end.
 
关于此类封装的dll讨论过了一些,但好像有些参数的传递有些秘决,你这样做好像是会出
错,因为在释放变量时会出问题,你先查查以前的贴子,或者在dll的线程注入、注消时加
些处理代码才行吧
 
你在 调用的时候 加
try
rundllform;
except
end;
 
人在昆明老教人坏方法,这可以过滤错误,可内部的错误并没解决,当积到一定程度就容易
引起系统OVER了
 
乌乌。。
有秘决的来啊!
 
人在昆明 大哥
在 调用的时候 加
try
RunDLL;
except
end;
捉不住错误啊,,

 
EXE代码呢?
很有可能是调用了InitDLL而没有调用FreeDLL引起的!再有可能是Icon.Handle引起的.
我觉得InitDLL和FreeDLL没必要的!

Application.Handle := ExeAppHandle;
//Application.Icon.Handle := CopyIcon(ExeAppIconHandle);
RegisterClasses([TFrmSingleBase, TFrmDoubleBase]);
theClass := GetClass('T' + FormName);
if theClass.InheritsFrom(TForm) and (theClass <> nil) then
begin
try
TheForm := TFrmBaseForm(theClass.Create).Create(nil);
//
TheForm.Icon.Handle := CopyIcon(ExeAppIconHandle);
//一定要CopyIcon!!!
//
try
TheForm.ShowDLLForm;
 
我也发觉,用不用InitDLL,FreeDLL,好象都没什么区别!(都是出错!!)
//我的调用DLL的代码
unit UseDLL;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons,StdCtrls;
type
TForm1 = class(TForm)
SpeedButton1: TSpeedButton;
procedure SpeedButton1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
procedure InitDLL(App:TApplication);stdcall;external 'MyForms.dll';
procedure RunDLL(DLLName, FormName: PChar;
ExeAppHandle, ExeAppIconHandle: Integer) stdcall;
external 'MyForms.dll';

procedure FreeDLL;stdcall;external 'MyForms.dll';
implementation
{$R *.dfm}
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
RunDLL(Pchar('MyForms.dll'),Pchar('FrmSingleBase'),Application.Handle,Application.Icon.Handle);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
InitDLL(Application);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeDLL;
end;

end.
 

//Application.Icon.Handle := CopyIcon(ExeAppIconHandle);
//
TheForm.Icon.Handle := CopyIcon(ExeAppIconHandle);
//一定要CopyIcon!!!
//
 
to wolaixue:
我去掉InitDLL ,FreeDLL 和用CopyIcon都是不行!
我想问题的关键不是这些!
是什么呢?!
!!
 

TheForm := TFrmBaseForm(theClass.Create).Create(nil);

有问题!
var
theClass: TFormClass;
theClass := TFormClass(FindClass('T' + FormName));

theForm := theClass.Create(Application);//or nil

另:finalization有代码吗?
 
To wolaixue:
theForm := theClass.Create(Application);//or nil
这句就通不过啊,
 
TheForm := TFrmBaseForm(theClass.Create).Create(nil);
瓦赛,宝贝儿,你浪费了我很多时间呢。
上面这句话,=formbaseform.create(nil);两个Create。
TClass.create.create。
问题就在这个地方。
 
是静态加载DLL的吗?
 
哈哈,xiangya说的很对。
 
var
theClass: TFormClass;
theClass := TFormClass(FindClass('T' + FormName));

theForm := TFrmBaseForm(theClass.Create(Application));//or nil 可以是:theForm: TFrmBaseForm;
theForm := theClass.Create(Application);//or nil 注:theForm: TForm;
告诉你的是方法,不是源代码!
 
后退
顶部