当前位置:主页>Delphi教程>文章内容
调用DLL文件中的FORM
来源: 作者: 发布时间:2007-04-29  
 

作者:e梦缘

好久发表文章了!!!

调用DLL文件中的FORM,具体实现过程如下:

library Project1;

uses
  SysUtils,
   Classes,Forms,windows,dialogs,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}
function showform(formname:string):boolean;stdcall;
var
  TheClass: TPersistentClass;
  aForm: TForm;
begin
 result:=false;
 {如果您的Dll中有很多FORM,请在这儿注册哦
 RegisterClasses([TForm1,TForm2,TForm3,...]);
 }
 RegisterClasses([TForm1]);
 TheClass := GetClass('T' + FormName);
 if (TheClass = nil) then   exit;
 if TheClass.InheritsFrom(TForm)  then
 begin
    aForm := Tform(TheClass.Create).Create(nil);
    try
      aForm.ShowModal;
      result:=true;
    finally
      FreeAndNil(aForm);
    end;

 end;
end;

exports
showform;
begin
end.



....


procedure  RunDllForm(const DllFileName,DllFormName:String;const methodName:string);
type
TRunForm=function(formname:string):boolean;stdcall;
var
  RunForm: TRunForm;
  GetDllHWND: HWND;
begin
  GetDllHWND := LoadLibrary(PChar(DllFileName));
  try
    if GetDllHWND < 32 then
    begin
      MessageBox(0, Pchar('没有找到'+DllFileName+'DLL文件!'),'加载DLL失败', MB_OK);
      Exit;
    end;
    @RunForm := GetProcAddress(GetDllHWND,pchar(methodName));
    if @RunForm <> nil then
       try
         RunForm(DllFormName);
       except
         raise Exception.Create('对不起,找不到T' + DllFormName+ '窗体!');
       end
     else
     raise Exception.Create('无效的方法名调用');
  finally
    FreeLibrary(GetDllHWND);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RunDllForm('project1.dll','form1','showform');
end;


....


 
上一篇:钩子(HOOK)机制的使用   下一篇:程序间参数传递
 
  相关文章
·钩子(HOOK)机制的使用
·程序间参数传递
·程序关联实现
·如何获取自己在程序中运行的外部EXE的H
·一种利用EXCEL快速写SQL语句的方法
·使用Dephi组件的生存期管理
·传说中的DELPHI9--DiamondBack
·mscomm32的简单应用
·MapX使用数据库数据添加专题图(系列之
·基于阻塞Socket(Indy)的远程控制类库-T
·Delphi中取某整数的某位的位状态的小函
·大自然的BUG、人的BUG、软件的疑难杂症
 
【关闭窗口】
推荐本站资源
最新文章