我在主程序员调用dll中的Form,已经成功调用,但是现在出现一个问题,
当dll中的form中有菜单与主菜单融合后,如果调用了菜单事件后,当关闭主程序的时候报错,代码出下
这是dll
library ProjectDll;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
UnitDll in 'UnitDll.pas' {Form1};procedure ProvaChild(ParentApplication: TApplication; ParentForm: TForm); export; stdcall;
var
Form1: TForm1;
DllProc: Pointer; { Called whenever DLL entry point is called }begin
Application:=ParentApplication; Form1:=TForm1.Create(ParentForm);
Form1.MyParentForm:=ParentForm;
Form1.MyParentApplication:=ParentApplication;
// windows.SetParent(Form1.Handle,ParentForm.Handle);
// Form1.FormStyle:=fsMDIChild;
Form1.Show;
end;procedure DLLUnloadProc(Reason: Integer); register;
begin
if Reason = DLL_PROCESS_DETACH then Application:=DllApplication;
end;exports
ProvaChild;begin
DllApplication:=Application;
DLLProc := @DLLUnloadProc;
end.*************************************************************************
unit UnitDll;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls;
const
WM_MyClose = WM_USER + 1;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
N2121: TMenuItem;
N121: TMenuItem;
Button1: TButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
procedure N121Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyParentForm: TForm;
MyParentApplication: TApplication;
end;var
DllApplication: TApplication;implementation{$R *.DFM}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
// Application:=DllApplication;
end;procedure TForm1.N121Click(Sender: TObject);
begin
showmessage('dd');
end;procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage('dd');
end;end.
来自:KervenLee, 时间:2003-12-19 21:49:00, ID:2365631 | 编辑
这是子form
object Form1: TForm1
Left = 319
Top = 189
Width = 317
Height = 276
Caption = 'Dll Child Form'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
FormStyle = fsMDIChild
Menu = MainMenu1
OldCreateOrder = False
Position = poDefault
Visible = True
OnClose = FormClose
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 88
Top = 88
Width = 137
Height = 41
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object MainMenu1: TMainMenu
Left = 112
Top = 160
object N2121: TMenuItem
Caption = '212'
object N121: TMenuItem
Caption = '12'
OnClick = N121Click
end
end
end
end
来自:KervenLee, 时间:2003-12-19 21:50:00, ID:2365633 | 编辑
这是主程序
unit MainUnit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ToolWin, ComCtrls, Menus;type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
Start: TMenuItem;
procedure StartClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
DllHandle: THandle;
{ Private declarations }
public
{ Public declarations }
end; T_ProvaChild = procedure (ParentApplication: TApplication; ParentForm: TForm); stdcall;var
MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.StartClick(Sender: TObject);
var ProcAddr: FarProc;
ProvaChild: T_ProvaChild;
begin
DllHandle := LoadLibrary('ProjectDll');
ProcAddr := GetProcAddress(DllHandle, 'ProvaChild');
if ProcAddr <> nil then
begin
ProvaChild := ProcAddr;
ProvaChild(Application,Self);
end;
end;procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// FreeLibrary(DllHandle);
end;end.
来自:KervenLee, 时间:2003-12-19 21:51:00, ID:2365634 | 编辑
这是主form
object MainForm: TMainForm
Left = 192
Top = 107
Width = 544
Height = 375
Caption = 'Main Form'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
FormStyle = fsMDIForm
Menu = MainMenu1
OldCreateOrder = False
OnClose = FormClose
PixelsPerInch = 96
TextHeight = 13
object MainMenu1: TMainMenu
Left = 72
Top = 16
object Start: TMenuItem
Caption = 'Create DLL Form'
OnClick = StartClick
end
end
end
当dll中的form中有菜单与主菜单融合后,如果调用了菜单事件后,当关闭主程序的时候报错,代码出下
这是dll
library ProjectDll;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
Windows,
Messages,
SysUtils,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
UnitDll in 'UnitDll.pas' {Form1};procedure ProvaChild(ParentApplication: TApplication; ParentForm: TForm); export; stdcall;
var
Form1: TForm1;
DllProc: Pointer; { Called whenever DLL entry point is called }begin
Application:=ParentApplication; Form1:=TForm1.Create(ParentForm);
Form1.MyParentForm:=ParentForm;
Form1.MyParentApplication:=ParentApplication;
// windows.SetParent(Form1.Handle,ParentForm.Handle);
// Form1.FormStyle:=fsMDIChild;
Form1.Show;
end;procedure DLLUnloadProc(Reason: Integer); register;
begin
if Reason = DLL_PROCESS_DETACH then Application:=DllApplication;
end;exports
ProvaChild;begin
DllApplication:=Application;
DLLProc := @DLLUnloadProc;
end.*************************************************************************
unit UnitDll;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls;
const
WM_MyClose = WM_USER + 1;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
N2121: TMenuItem;
N121: TMenuItem;
Button1: TButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
procedure N121Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyParentForm: TForm;
MyParentApplication: TApplication;
end;var
DllApplication: TApplication;implementation{$R *.DFM}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
// Application:=DllApplication;
end;procedure TForm1.N121Click(Sender: TObject);
begin
showmessage('dd');
end;procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage('dd');
end;end.
来自:KervenLee, 时间:2003-12-19 21:49:00, ID:2365631 | 编辑
这是子form
object Form1: TForm1
Left = 319
Top = 189
Width = 317
Height = 276
Caption = 'Dll Child Form'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
FormStyle = fsMDIChild
Menu = MainMenu1
OldCreateOrder = False
Position = poDefault
Visible = True
OnClose = FormClose
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 88
Top = 88
Width = 137
Height = 41
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object MainMenu1: TMainMenu
Left = 112
Top = 160
object N2121: TMenuItem
Caption = '212'
object N121: TMenuItem
Caption = '12'
OnClick = N121Click
end
end
end
end
来自:KervenLee, 时间:2003-12-19 21:50:00, ID:2365633 | 编辑
这是主程序
unit MainUnit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ToolWin, ComCtrls, Menus;type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
Start: TMenuItem;
procedure StartClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
DllHandle: THandle;
{ Private declarations }
public
{ Public declarations }
end; T_ProvaChild = procedure (ParentApplication: TApplication; ParentForm: TForm); stdcall;var
MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.StartClick(Sender: TObject);
var ProcAddr: FarProc;
ProvaChild: T_ProvaChild;
begin
DllHandle := LoadLibrary('ProjectDll');
ProcAddr := GetProcAddress(DllHandle, 'ProvaChild');
if ProcAddr <> nil then
begin
ProvaChild := ProcAddr;
ProvaChild(Application,Self);
end;
end;procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// FreeLibrary(DllHandle);
end;end.
来自:KervenLee, 时间:2003-12-19 21:51:00, ID:2365634 | 编辑
这是主form
object MainForm: TMainForm
Left = 192
Top = 107
Width = 544
Height = 375
Caption = 'Main Form'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
FormStyle = fsMDIForm
Menu = MainMenu1
OldCreateOrder = False
OnClose = FormClose
PixelsPerInch = 96
TextHeight = 13
object MainMenu1: TMainMenu
Left = 72
Top = 16
object Start: TMenuItem
Caption = 'Create DLL Form'
OnClick = StartClick
end
end
end
OldApp: TApplication;procedure DllHandler(AReason: DWORD);
begin
case AReason of
DLL_PROCESS_ATTACH:
OldApp := Application;
DLL_PROCESS_DETACH:
Application := OldApp;
end;
end;
begin
DllProc := @DllHandler;
end.
procedure DLLUnloadProc(Reason: Integer); register;
begin
if Reason = DLL_PROCESS_DETACH then Application:=DllApplication;
end;exports
ProvaChild;begin
DllApplication:=Application;
DLLProc := @DLLUnloadProc;
end.
在入口函数中为什么这样呀:procedure DLLUnloadProc(Reason: Integer); register;
begin
if Reason = DLL_PROCESS_DETACH then Application:=DllApplication;
if Reason = DLL_PROCESS_ATTACH then DllApplication:=Application;
end;exports
ProvaChild;begin
DLLProc := @DLLUnloadProc;
end.
Desktop是exe,
其它软件不一定是Delphi做的,只能说VCL没做好
你再放个Panel放在主窗体的客户区,DevelInner=bvLowered, Align = alClient,不知谁还能看出区别来,我就是这么做的