动态链接库的问题,各位大虾帮帮忙 ( 积分: 50 )

  • 主题发起人 主题发起人 我爱豆腐
  • 开始时间 开始时间

我爱豆腐

Unregistered / Unconfirmed
GUEST, unregistred user!
利用动态链接库怎样实现窗体间的互相调用?请给个具体的例子,能介绍本关于DLL编程的教程更是感激不尽
 
利用动态链接库怎样实现窗体间的互相调用?请给个具体的例子,能介绍本关于DLL编程的教程更是感激不尽
 
调用DLL文件中的FORM<br><br><br>调用DLL文件中的FORM,具体实现过程如下:<br><br>library Project1;<br><br>uses<br> &nbsp;SysUtils,<br> &nbsp; Classes,Forms,windows,dialogs,<br> &nbsp;Unit1 in 'Unit1.pas' {Form1};<br><br>{$R *.res}<br>function showform(formname:string):boolean;stdcall;<br>var<br> &nbsp;TheClass: TPersistentClass;<br> &nbsp;aForm: TForm;<br>begin<br> result:=false;<br> {如果您的Dll中有很多FORM,请在这儿注册哦<br> RegisterClasses([TForm1,TForm2,TForm3,...]);<br> }<br> RegisterClasses([TForm1]);<br> TheClass := GetClass('T' + FormName);<br> if (TheClass = nil) then &nbsp; exit;<br> if TheClass.InheritsFrom(TForm) &nbsp;then<br> begin<br> &nbsp; &nbsp;aForm := Tform(TheClass.Create).Create(nil);<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;aForm.ShowModal;<br> &nbsp; &nbsp; &nbsp;result:=true;<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;FreeAndNil(aForm);<br> &nbsp; &nbsp;end;<br><br> end;<br>end;<br><br>exports<br>showform;<br>begin<br>end.<br><br><br><br>--------------------------------------------------------------------------------<br><br><br><br>....<br><br><br>procedure &nbsp;RunDllForm(const DllFileName,DllFormName:String;const methodName:string);<br>type<br>TRunForm=function(formname:string):boolean;stdcall;<br>var<br> &nbsp;RunForm: TRunForm;<br> &nbsp;GetDllHWND: HWND;<br>begin<br> &nbsp;GetDllHWND := LoadLibrary(PChar(DllFileName));<br> &nbsp;try<br> &nbsp; &nbsp;if GetDllHWND &amp;lt; 32 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MessageBox(0, Pchar('没有找到'+DllFileName+'DLL文件!'),'加载DLL失败', MB_OK);<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;@RunForm := GetProcAddress(GetDllHWND,pchar(methodName));<br> &nbsp; &nbsp;if @RunForm &amp;lt;&amp;gt; nil then<br> &nbsp; &nbsp; &nbsp; try<br> &nbsp; &nbsp; &nbsp; &nbsp; RunForm(DllFormName);<br> &nbsp; &nbsp; &nbsp; except<br> &nbsp; &nbsp; &nbsp; &nbsp; raise Exception.Create('对不起,找不到T' + DllFormName+ '窗体!');<br> &nbsp; &nbsp; &nbsp; end<br> &nbsp; &nbsp; else<br> &nbsp; &nbsp; raise Exception.Create('无效的方法名调用');<br> &nbsp;finally<br> &nbsp; &nbsp;FreeLibrary(GetDllHWND);<br> &nbsp;end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>RunDllForm('project1.dll','form1','showform');<br>end;<br><br><br>....<br> <br>上面的例子没有释放过程,自己最后别忘了<br>取消注册
 
谢谢这个兄台指教,忠心的感谢,小弟我初来乍到,希望以后多帮帮我
 
后退
顶部