有两种方式,一种是模式窗口,一种是无模式窗口。 D5程序员开发指南有例子. 下面是模式窗口的例子。unit MainFfm; interfaceuses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type { First, define a procedural data type, this should reflect the procedure that is exported from the DLL. } TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall; { Create a new exception class to reflect a failed DLL load } EDLLLoadError = class(Exception); TMainForm = class(TForm) lblDate: TLabel; btnGetCalendar: TButton; procedure btnGetCalendarClick(Sender: TObject); end;var MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.btnGetCalendarClick(Sender: TObject); var LibHandle : THandle; ShowCalendar: TShowCalendar; begin { Attempt to load the DLL } LibHandle := LoadLibrary('CALENDARLIB.DLL'); try { If the load failed, LibHandle will be zero. If this occurs, raise an exception. } if LibHandle = 0 then raise EDLLLoadError.Create('Unable to Load DLL'); { If the code makes it here, the DLL loaded successfully, now obtain the link to the DLL's exported function so that it can be called. } @ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar'); { If the function is imported successfully, then set lblDate.Caption to reflect the returned date from the function. Otherwise, show the return raise an exception. } if not (@ShowCalendar = nil) then lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption)) else RaiseLastWin32Error; finally FreeLibrary(LibHandle); // Unload the DLL. end; end;end.unit DLLFrm; interfaceuses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, Grids, Calendar;type TDLLForm = class(TForm) calDllCalendar: TCalendar; procedure calDllCalendarDblClick(Sender: TObject); end;{ Declare the export function } function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;implementation {$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; var DLLForm: TDllForm; begin // Copy application handle to DLL's TApplication object Application.Handle := AHandle; DLLForm := TDLLForm.Create(Application); try DLLForm.Caption := ACaption; DLLForm.ShowModal; Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result finally DLLForm.Free; end; end;procedure TDLLForm.calDllCalendarDblClick(Sender: TObject); begin Close; end;end.
下面是无模式窗口的例子。unit MainFfm; interfaceuses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TMainForm = class(TForm) btnShowCalendar: TButton; btnCloseCalendar: TButton; procedure btnShowCalendarClick(Sender: TObject); procedure btnCloseCalendarClick(Sender: TObject); procedure FormCreate(Sender: TObject); private FFormRef: TForm; end;var MainForm: TMainForm;function ShowCalendar(AHandle: THandle; ACaption: String): Longint; StdCall; external 'CALENDARMLLIB.DLL';procedure CloseCalendar(AFormRef: Longint); stdcall; external 'CALENDARMLLIB.DLL';implementation{$R *.DFM}procedure TMainForm.btnShowCalendarClick(Sender: TObject); begin if not Assigned(FFormRef) then FFormRef := TForm(ShowCalendar(Application.Handle, Caption)); end;procedure TMainForm.btnCloseCalendarClick(Sender: TObject); begin if Assigned(FFormRef) then begin CloseCalendar(Longint(FFormRef)); FFormRef := nil; end; end;procedure TMainForm.FormCreate(Sender: TObject); begin FFormRef := nil; // Initialize the FFormRef field to nil. end;end. unit DLLFrm; interfaceuses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, Grids, Calendar;type TDLLForm = class(TForm) calDllCalendar: TCalendar; end;{ Declare the export function } function ShowCalendar(AHandle: THandle; ACaption: String): Longint; stdCall; procedure CloseCalendar(AFormRef: Longint); stdcall; implementation {$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): Longint; var DLLForm: TDllForm; begin // Copy application handle to DLL's TApplication object Application.Handle := AHandle; DLLForm := TDLLForm.Create(Application); Result := Longint(DLLForm); DLLForm.Caption := ACaption; DLLForm.Show; end;procedure CloseCalendar(AFormRef: Longint); begin if AFormRef > 0 then TDLLForm(AFormRef).Release; end;end.
function showtestform():integer;export;stdcall;
begin
i:=0;
try
frmtest:=tfrmtest.create(frmtest);
frmtest.showmodal();
result:= 1;
except
frmtest.free;
result:= -1;
end;
end;exports
showtestform index 1 name 'showform' resident;
调用代码:function showform():integer; external 'Project1.dll';procedure TForm1.Button1Click(Sender: TObject);
begin
showform();
end;
在DLL中有多个函数,每个函数执行的状态我想在这个窗体的LABEL上显示,应该怎么处理呢
function showtestform():integer;export;stdcall;
begin
i:=0;
try
frmtest:=tfrmtest.create(frmtest);
frmtest.showmodal();
result:= 1;
except
frmtest.free;
result:= -1;
end;
end;exports
showtestform index 1 name 'showform' resident;
调用代码:function showform():integer; external 'Project1.dll';procedure TForm1.Button1Click(Sender: TObject);
begin
showform();
end;
我试了,不成功,我在前面定义了type
Tfrmtest = class(TForm)
end;然后用了上面的语法,它提示窗体没初始化
lgqTiger(【老虎】,你能帮我做个完整的例子吗,
万分感谢
在DLL中有多个函数,每个函数执行的状态我想在这个窗体的LABEL上显示,应该怎么处理呢
谢谢
[email protected]
好了吗?
D5程序员开发指南有例子.
下面是模式窗口的例子。unit MainFfm;
interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;type
{ First, define a procedural data type, this should reflect the
procedure that is exported from the DLL. }
TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall; { Create a new exception class to reflect a failed DLL load }
EDLLLoadError = class(Exception); TMainForm = class(TForm)
lblDate: TLabel;
btnGetCalendar: TButton;
procedure btnGetCalendarClick(Sender: TObject);
end;var
MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.btnGetCalendarClick(Sender: TObject);
var
LibHandle : THandle;
ShowCalendar: TShowCalendar;
begin { Attempt to load the DLL }
LibHandle := LoadLibrary('CALENDARLIB.DLL');
try
{ If the load failed, LibHandle will be zero.
If this occurs, raise an exception. }
if LibHandle = 0 then
raise EDLLLoadError.Create('Unable to Load DLL');
{ If the code makes it here, the DLL loaded successfully, now obtain
the link to the DLL's exported function so that it can be called. }
@ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');
{ If the function is imported successfully, then set lblDate.Caption to reflect
the returned date from the function. Otherwise, show the return raise
an exception. }
if not (@ShowCalendar = nil) then
lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
else
RaiseLastWin32Error;
finally
FreeLibrary(LibHandle); // Unload the DLL.
end;
end;end.unit DLLFrm;
interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Grids, Calendar;type TDLLForm = class(TForm)
calDllCalendar: TCalendar;
procedure calDllCalendarDblClick(Sender: TObject);
end;{ Declare the export function }
function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;implementation
{$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;
var
DLLForm: TDllForm;
begin
// Copy application handle to DLL's TApplication object
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application);
try
DLLForm.Caption := ACaption;
DLLForm.ShowModal;
Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result
finally
DLLForm.Free;
end;
end;procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);
begin
Close;
end;end.
interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;type TMainForm = class(TForm)
btnShowCalendar: TButton;
btnCloseCalendar: TButton;
procedure btnShowCalendarClick(Sender: TObject);
procedure btnCloseCalendarClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
FFormRef: TForm;
end;var
MainForm: TMainForm;function ShowCalendar(AHandle: THandle; ACaption: String): Longint; StdCall;
external 'CALENDARMLLIB.DLL';procedure CloseCalendar(AFormRef: Longint); stdcall;
external 'CALENDARMLLIB.DLL';implementation{$R *.DFM}procedure TMainForm.btnShowCalendarClick(Sender: TObject);
begin
if not Assigned(FFormRef) then
FFormRef := TForm(ShowCalendar(Application.Handle, Caption));
end;procedure TMainForm.btnCloseCalendarClick(Sender: TObject);
begin
if Assigned(FFormRef) then
begin
CloseCalendar(Longint(FFormRef));
FFormRef := nil;
end;
end;procedure TMainForm.FormCreate(Sender: TObject);
begin
FFormRef := nil; // Initialize the FFormRef field to nil.
end;end.
unit DLLFrm;
interfaceuses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Grids, Calendar;type TDLLForm = class(TForm)
calDllCalendar: TCalendar;
end;{ Declare the export function }
function ShowCalendar(AHandle: THandle; ACaption: String): Longint; stdCall;
procedure CloseCalendar(AFormRef: Longint); stdcall;
implementation
{$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): Longint;
var
DLLForm: TDllForm;
begin
// Copy application handle to DLL's TApplication object
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application);
Result := Longint(DLLForm);
DLLForm.Caption := ACaption;
DLLForm.Show;
end;procedure CloseCalendar(AFormRef: Longint);
begin
if AFormRef > 0 then
TDLLForm(AFormRef).Release;
end;end.