如何解决这样的dll释放问题(dll中的form嵌入在调用窗体的scrollbox中),这个问题已经困扰好几天了,急!(100分)

  • 主题发起人 主题发起人 Lion_sj
  • 开始时间 开始时间
L

Lion_sj

Unregistered / Unconfirmed
GUEST, unregistred user!
源码如下:都是带包编译
调用程序:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons;

const SX_CLOSEFRMMESSAGE = WM_USER + 5000;

type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Pnl_main: TPanel;
ScrollBox1: TScrollBox;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
procedure SXCloseFrmMessage(var Msg: TMessage)
message SX_CLOSEFRMMESSAGE;
public

end;

T_ProvaChild = procedure (ParentApplication: TApplication
ParentScrollbox: TScrollBox;AParentHand,AParentWinHand: HWND)
stdcall;
var
Form1: TForm1;
DllHandle: THandle;

implementation

{$R *.DFM}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
ProcAddr: FarProc;
ProvaChild: T_ProvaChild;
// lasterror:DWord;
begin
DllHandle := LoadLibrary('testdll');
ProcAddr := GetProcAddress(DllHandle, 'ProvaChild');
if ProcAddr <> nil then
begin
ProvaChild := ProcAddr;
ProvaChild(Application,ScrollBox1,Pnl_main.Handle,form1.Handle);
end;
end;

procedure TForm1.SXCloseFrmMessage(var Msg: TMessage);
begin
showmessage('aaaaa');
if DllHandle<>0 then
begin
// SetOldApplication;
FreeLiBrary(DllHandle);
end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
SendMessage(Form1.Handle,WM_USER+5000,0,0);
end;

end.

dll程序
library testdll;

uses
forms,
extctrls,
windows,
Dialogs,
SysUtils,
Classes,
Controls,
testdllunit in 'testdllunit.pas' {Form2};

procedure ProvaChild(ParentApplication: TApplication
ParentScrollbox: TScrollbox;AParentHand,AParentWinHand: HWND
)
export
stdcall;
begin
Application.handle := AParentHand; //传递程序句柄,即主控程序的Panel的句柄
ParentWinHand:=AParentWinhand
//传递主控程序窗口的句柄
//创建应用程序主窗口
if not Assigned(form2) then
begin
form2 := Tform2.Create(ParentScrollbox);
form2.Parent:=ParentScrollbox;
end;

with form2 do
begin
align:=alClient;
BorderStyle := bsnone;
BorderIcons := [];
Top := 0;
Left := 0;
form2.WindowState:=wsMaximized;
Show;
end;
end;

exports
ProvaChild;

begin
end.


unit testdllunit;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, Mask, Grids;

type
TForm2 = class(TForm)
StringGrid1: TStringGrid;
MaskEdit1: TMaskEdit;
BitBtn1: TBitBtn;
procedure FormClose(Sender: TObject
var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public

end;

var
DllApplication1: TApplication;

ParentWinHand : hwnd;
Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.FormClose(Sender: TObject
var Action: TCloseAction);
begin
Action:=caFree;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
SendMessage(ParentWinHand,WM_USER+5000,longint(form2.Handle),0);
// showmessage('111');
Form2:=nil;
end;

procedure TForm2.BitBtn1Click(Sender: TObject);
begin
close;
end;

end.
 
释放时,要用
Application.handle := 0;
...
form2.Parent:= nil吧;
 
后退
顶部