首页 Delphi 正文
  • 本文约1185字,阅读需6分钟
  • 1984
  • 0
举报该广告
DELPHI 调用DLL文件中多FORM的调

DELPHI 调用DLL文件中多FORM的调

摘要

library Project1;
uses

SysUtils,

   Classes,Forms,windows,dialogs,

  Unit1 
in 'Unit1.pas' {Form1};
{$R *.res}
function showform(formname:string):boolean;stdca...

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 = nilthen   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;


📱 扫码关注公众号

公众号二维码

扫描二维码关注我们,获取更多精彩内容
实时更新 · 干货满满

收藏

扫描二维码,在手机上阅读
评论
更换验证码
友情链接