在DLL中封装一个FORM,DLL原代码如下:
unit moduleForm;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;type
TDllFrm = class(TForm)
MonthCalendar1: TMonthCalendar;
procedure MonthCalendar1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function showCalendar(AHandle: THandle; ACaption: pchar):TDate; stdcall;implementation{$R *.dfm}procedure TDllFrm.MonthCalendar1DblClick(Sender: TObject);
begin
close;
end;function showCalendar(AHandle: THandle; ACaption: pchar):TDate;
var
DllFrm: TDllFrm;
begin
Application.Handle:=AHandle;
DllFrm:=TDllFrm.Create(application);
try
DllFrm.Caption :=ACaption;
DllFrm.ShowModal;
result:=DllFrm.MonthCalendar1.Date;
finally
DllFrm.Free;
end;
end;end.library DllForm;{ 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
SysUtils,
Classes,
moduleForm in 'moduleForm.pas' {DllFrm};exports
showCalendar; {$R *.res}begin
end.
测试程序如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function showCalendar(AHandle: THandle; ACaption: pchar):TDate; stdcall; external'DllForm';procedure TForm1.Button1Click(Sender: TObject);
begin
showCalendar(application.handle,'testDLLForm');
end;end.
program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin(***)
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
现在出现的问题是在程序运行到(***)行时就出现一个CPU信息窗口.提示程序有错.请问这是什么原因呢?怎样解决!多谢每个回答的朋友!
unit moduleForm;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;type
TDllFrm = class(TForm)
MonthCalendar1: TMonthCalendar;
procedure MonthCalendar1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function showCalendar(AHandle: THandle; ACaption: pchar):TDate; stdcall;implementation{$R *.dfm}procedure TDllFrm.MonthCalendar1DblClick(Sender: TObject);
begin
close;
end;function showCalendar(AHandle: THandle; ACaption: pchar):TDate;
var
DllFrm: TDllFrm;
begin
Application.Handle:=AHandle;
DllFrm:=TDllFrm.Create(application);
try
DllFrm.Caption :=ACaption;
DllFrm.ShowModal;
result:=DllFrm.MonthCalendar1.Date;
finally
DllFrm.Free;
end;
end;end.library DllForm;{ 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
SysUtils,
Classes,
moduleForm in 'moduleForm.pas' {DllFrm};exports
showCalendar; {$R *.res}begin
end.
测试程序如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}function showCalendar(AHandle: THandle; ACaption: pchar):TDate; stdcall; external'DllForm';procedure TForm1.Button1Click(Sender: TObject);
begin
showCalendar(application.handle,'testDLLForm');
end;end.
program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin(***)
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
现在出现的问题是在程序运行到(***)行时就出现一个CPU信息窗口.提示程序有错.请问这是什么原因呢?怎样解决!多谢每个回答的朋友!
function showCalendar(AHandle: THandle; ACaption: pchar):TDate; stdcall; external'E:\MyErp\BIN\DllForm.dll';
你把你做的dll和你的测试程序放一个目录吧~
first unit in your library's USES clause