怎样释放DLL中的MDI窗体比较好
问题如上.
//主程序
procedure mxSC;
var
HDL:THandle;
vFileName:String;
ShowMDIForm:TmdlShowMDIForm;
begin
vFileName:=ExtractFileDir(Application.ExeName) + '\Module\mSC.dll';
HDL:=LoadLibrary(PChar(vFileName));
if HDL <= 0 then begin
Raise Exception.Create('加载' + vFileName + '失败!');
Exit;
end;
try
@ShowMDIForm:=GetProcAddress(HDL,Pchar('ShowMDIForm'));
if not Assigned(ShowMDIForm) then
Raise Exception.Create('读取' + vFileName + 'ShowMDIForm函数入口指针失败')
else
ShowMDIForm(Application,Application.MainForm);
finally
//*******************************************************
FreeLibrary(HDL);//不加该句,关闭MDI窗体时怎样释放装载的DLL。
//*******************************************************
end;
end;
library mSC;
{$DEFINE VENDORLL}
uses
SysUtils,
Classes,
windows,
Forms,
ActiveX,
SC in '..\SC.pas' {frmSC},
SCEdit in '..\SCEdit.pas' {frmSCEdit},
SCServer in '..\SCServer.pas' {dmSCServer: TDataModule};
var
DLLApplication:TApplication;
{$R *.res}
procedure DllEntryPoint(dwReason:DWORD);register;
begin
case dwReason of
DLL_PROCESS_ATTACH: DLLApplication:=Application;
DLL_PROCESS_DETACH: Application:=DLLApplication;
end;
end;
function ShowMDIForm(ParentApplication:TApplication;ParentForm:TForm):LongInt;export;stdcall;
begin
Application:=ParentApplication;
if not Assigned(dmSCServer) then dmSCServer:=TdmSCServer.Create(Application);
if not Assigned(frmSC) then frmSC:=TfrmSC.Create(ParentForm);
Result:=LongInt(frmSC);
dmSCServer.GetSCAllData;
Windows.SetParent(frmSC.Handle,ParentForm.Handle);
frmSC.FormStyle:=fsMDIChild;
frmSC.Show;
end;
exports
ShowMDIForm;
begin
CoInitialize(nil);
DllProc:=@DllEntryPoint;
DllEntryPoint(DLL_PROCESS_ATTACH);
end.
//DLL中的MDI
type
TmdlShowMDIForm=function (ParentApplication:TApplication;ParentForm:TForm):LongInt;stdcall;
procedure TfrmSC.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
frmSC:=nil;
end;
问题点数:20、回复次数:6Top
1 楼guolinchao(kony)回复于 2003-11-04 11:47:00 得分 0
对不起,写错了。
由于时调用非模式窗体,所有,不可以用FreeLibrary()函数释放DLL的资源。
请问怎样在主程序或者在DLL中怎样释放资源。
//主程序
type
TmdlShowMDIForm=function (ParentApplication:TApplication;ParentForm:TForm):LongInt;stdcall;
//调用DLL中的MDI过程
procedure mxSC;
var
HDL:THandle;
vFileName:String;
ShowMDIForm:TmdlShowMDIForm;
begin
vFileName:=ExtractFileDir(Application.ExeName) + '\Module\mSC.dll';
HDL:=LoadLibrary(PChar(vFileName));
if HDL <= 0 then begin
Raise Exception.Create('加载' + vFileName + '失败!');
Exit;
end;
try
@ShowMDIForm:=GetProcAddress(HDL,Pchar('ShowMDIForm'));
if not Assigned(ShowMDIForm) then
Raise Exception.Create('读取' + vFileName + 'ShowMDIForm函数入口指针失败')
else
ShowMDIForm(Application,Application.MainForm);
finally
//*******************************************************
//由于时调用非模式窗体,所有,不可以用FreeLibrary()函数释放DLL的资源。
//请问怎样在主程序或者在DLL中怎样释放资源。
//*******************************************************
end;
end;
//-------------------------------------DLL
library mSC;
{$DEFINE VENDORLL}
uses
SysUtils,
Classes,
windows,
Forms,
ActiveX,
SC in '..\SC.pas' {frmSC},
SCEdit in '..\SCEdit.pas' {frmSCEdit},
SCServer in '..\SCServer.pas' {dmSCServer: TDataModule};
var
DLLApplication:TApplication;
{$R *.res}
procedure DllEntryPoint(dwReason:DWORD);register;
begin
case dwReason of
DLL_PROCESS_ATTACH: DLLApplication:=Application;
DLL_PROCESS_DETACH: Application:=DLLApplication; //可以在此释放吗,这么做?我试过在此释放,但失败了!
end;
end;
function ShowMDIForm(ParentApplication:TApplication;ParentForm:TForm):LongInt;export;stdcall;
begin
Application:=ParentApplication;
if not Assigned(dmSCServer) then dmSCServer:=TdmSCServer.Create(Application);
if not Assigned(frmSC) then frmSC:=TfrmSC.Create(ParentForm);
Result:=LongInt(frmSC);
dmSCServer.GetSCAllData;
Windows.SetParent(frmSC.Handle,ParentForm.Handle);
frmSC.FormStyle:=fsMDIChild;
frmSC.Show;
end;
exports
ShowMDIForm;
begin
CoInitialize(nil);
DllProc:=@DllEntryPoint;
DllEntryPoint(DLL_PROCESS_ATTACH);
end.
//DLL中的MDI是这样释放的
procedure TfrmSC.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
frmSC:=nil;
end;
Top
2 楼guolinchao(kony)回复于 2003-11-04 22:33:52 得分 0
怎样及时的释放DLL资源,
procedure DllEntryPoint(dwReason:DWORD);register;
begin
case dwReason of
DLL_PROCESS_ATTACH: DLLApplication:=Application;
DLL_PROCESS_DETACH: Application:=DLLApplication; //这句只有当主调程序关闭时才执行,我想当关闭MDI Form 时就释放,怎么做呢?
end;
end;Top
3 楼guolinchao(kony)回复于 2003-11-05 16:55:41 得分 0
怎么没人回答呢?Top
4 楼deepWATERblue(深水蓝)回复于 2003-11-05 17:58:35 得分 0
qiang.Top
5 楼saien(精益求精)回复于 2003-11-05 18:59:46 得分 20
在dll中释放比较Top
6 楼guolinchao(kony)回复于 2003-11-05 22:21:27 得分 0
但是,在DLL中怎么释放呢?
这个问题已经两天了,还是没有解决,如果会就帮帮我吧!Top




