N
northwindrocker
Unregistered / Unconfirmed
GUEST, unregistred user!
我看《d6编程人员指南》中对把FORM做成DLL调用的例子!书中讲到的是把日历做为一个DLL然后在主程序里调用。源码如下:<br>Calendarlib.drp<br>{*******************************************}<br>library CalendarLib;<br><br>uses<br> ShareMem,<br> SysUtils,<br> Classes,<br> DLLFrm in 'DLLFrm.pas' {DLLForm};<br><br>exports<br> ShowCalendar;<br> <br>begin<br>end.<br><br>{********************************}<br>Dllfrm.pas<br><br>{********************************}<br>{ Copyright ?2001 Delphi 6 Developer's Guide Xavier Pacheco<br> and Steve Teixeira }<br><br>unit DLLFrm;<br><br>interface<br><br>uses<br> SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,<br> Forms, Dialogs, Grids, Calendar;<br><br>type<br><br> TDLLForm = class(TForm)<br> calDllCalendar: TCalendar;<br> procedure calDllCalendarDblClick(Sender: TObject);<br> end;<br><br>{ Declare the export function }<br>function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;<br><br>implementation<br>{$R *.DFM}<br><br>function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;<br>var<br> DLLForm: TDllForm;<br>begin<br> // Copy application handle to DLL's TApplication object<br> Application.Handle := AHandle;<br> DLLForm := TDLLForm.Create(Application); <br> try<br> DLLForm.Caption := ACaption;<br> DLLForm.ShowModal;<br> Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result<br> finally<br> DLLForm.Free;<br> end;<br>end;<br><br>procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);<br>begin<br> Close;<br>end;<br><br>end.<br><br>然后是主程序:<br>CalendarTest.drp<br><br>mainfrm<br><br>{ Copyright ?2001 Delphi 6 Developer's Guide Xavier Pacheco<br> and Steve Teixeira }<br><br><br>unit MainFfm;<br><br>interface<br><br>uses<br> SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,<br> Forms, Dialogs, StdCtrls;<br><br>type<br> { First, define a procedural data type, this should reflect the<br> procedure that is exported from the DLL. }<br> TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall;<br><br> { Create a new exception class to reflect a failed DLL load }<br> EDLLLoadError = class(Exception);<br><br> TMainForm = class(TForm)<br> lblDate: TLabel;<br> btnGetCalendar: TButton;<br> procedure btnGetCalendarClick(Sender: TObject);<br> end;<br><br>var<br> MainForm: TMainForm;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TMainForm.btnGetCalendarClick(Sender: TObject);<br>var<br> LibHandle : THandle;<br> ShowCalendar: TShowCalendar;<br>begin<br><br> { Attempt to load the DLL }<br> LibHandle := LoadLibrary('CALENDARLIB.DLL');<br> try<br> { If the load failed, LibHandle will be zero.<br> If this occurs, raise an exception. }<br> if LibHandle = 0 then<br> raise EDLLLoadError.Create('Unable to Load DLL');<br> { If the code makes it here, the DLL loaded successfully, now obtain<br> the link to the DLL's exported function so that it can be called. }<br> @ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');<br> { If the function is imported successfully, then set lblDate.Caption to reflect<br> the returned date from the function. Otherwise, show the return raise<br> an exception. }<br> if not (@ShowCalendar = nil) then<br> lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))<br> else<br> RaiseLastWin32Error; <br> finally<br> FreeLibrary(LibHandle); // Unload the DLL.<br> end;<br>end;<br><br>end.<br>然后我编译DLL运行主程序报内存错误,但程序还是能运行。我估计是他的caption用了string的原因(很肤浅不要笑我)。我不知道怎么改它。能帮我看看吗!<br>