先来说一下我如何写我的Dll文件的。
先看代码:
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://>library pub_form; uses SysUtils, Classes, Windows, Forms, Parameter_Object, frm_LoginU in ‘..\公用库单元\frm_LoginU.pas‘ {frm_Login}, pub_Event in ‘..\公用库单元\pub_Event.pas‘, Dll_LIB_ConstU in ‘..\公用库单元\Dll_LIB_ConstU.pas‘; var DLLApp: TApplication; {begin-----------------------登录窗体相关-------------------------} procedure Login(var mLoginParam: TLoginParam); export; begin {获取调用窗体的Application,显而易见的功能是 能使你的窗体融合到调用程序中。通过它还能进行很多操作} Application := mLoginParam.App; //将DLL的Application转为App if frm_Login = nil then frm_Login := Tfrm_Login.Create(mLoginParam.ParentForm); frm_Login.DoLoginClick := mLoginParam.DoLogin; frm_Login.DoErrCatch:=mLoginParam.ErrDo; frm_Login.E_User.Text:=mLoginParam.OldUser; frm_Login.ShowModal; end; procedure CloseLogin; export; begin if frm_Login.Showing then frm_Login.Close; end; {end-----------------------登录窗体相关-------------------------} {重写Dll入口函数,否则程序会出错} procedure DLLUnloadProc(Reason: Integer); register; begin {DLL取消调用时,,发送DLL_PROCESS_DETACH消息,此时将DLL的Application返回为本身} if Reason = DLL_PROCESS_DETACH then Application := DLLApp; end; exports Login,CloseLogin; begin {在DLL入口预先储存DLL的Application} DLLApp := Application; {DllProc:DLL入口函数指针。Delphi定义为 DllProc: TDLLProc;} {在此指向我们自己定义的函数} DLLProc := @DLLUnloadProc; end.
代码中都有注释了,这个不多说了,说一下Login的过程,我这里是直接传递对象指针进来。这个对象的定义在下面的代码中:
代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://>unit Parameter_Object; { 调用DLL的参数对象类 } interface uses pub_Event,Forms; type TDllFormParam=class(TObject) public App: TApplication; ParentForm:TForm; ErrDo:TExceptionEvent; constructor CreateByObject(var mApp: TApplication;var mParentForm:TForm;mErrDo:TExceptionEvent); end; TLoginParam=class(TDllFormParam) public DoLogin:TLoginClickEvent; OldUser:String;//上次登录的用户 constructor CreateByObject(var mApp: TApplication;var mParentForm:TForm;mErrDo:TExceptionEvent;mDoLogin:TLoginClickEvent;mUser:string=‘‘); end; implementation { TLoginParam } constructor TLoginParam.CreateByObject(var mApp: TApplication; var mParentForm: TForm;mErrDo:TExceptionEvent; mDoLogin: TLoginClickEvent;mUser:string=‘‘); begin inherited CreateByObject(mApp,mParentForm,mErrDo); DoLogin:=mDoLogin; OldUser:=mUser; end; { TDllFormParam } constructor TDllFormParam.CreateByObject(var mApp: TApplication; var mParentForm: TForm;mErrDo:TExceptionEvent); begin App:=mApp; ParentForm:=mParentForm; ErrDo:=mErrDo; end; end.
这样做的好处是不用那么多参数出现,也方便动态的调用方式。
还有一个不太重要的单元: