delphi 跨版本DLL调用嵌入窗体实现

delphi 能实现把别的DLL的窗体句柄查到后,贴到PANL之中,此类文章网上不少,而如果是delphi不同版本开发的DLL互调时,一些控件内部的定义有所区别,因为无法(至少目前我觉得理论上不可行)实现不同版本的DLL融合一体式的共用同一个appcation.

因此,跨版本的DLL调用,实际上也就是把DLL当做一个独立程序,而数据连接这些需要在DLL中自己再实现一套,会有一定的麻烦,但是如果不得不这样做,这样的牺牲也是没办法的事(如果谁实现了协调的跨版本数据连接共享,希望留言指点一二)

 Function GETInterfacedFunc(iApplication:TApplication;iScreen: TScreen;Imode:integer;Userinfo:String):IInterfacedFunc ;stdcall;

 上面的代码,是常规的DELPHI 的DLL调用实现,也就是传入  

iApplication:TApplication;iScreen: TScreen; 然后返回调用接口。
 Function GETInterfacedFunc(Imode:integer;Userinfo:String):IInterfacedFunc ;stdcall;    

跨版本相当于独立的程序调用,因此入口函数,就不能再处理 Application与 Screen了,咋们,就当这个DLL是一个独立EXE

如果传了 Application与 Screen ,就是建一个简单的FORM都会报地址错,因为相同的类在不同版本里面,其定义差异很大了。

加载DLL后

下面就是显示窗体了

直接上代码吧

function Tform.Showform(Parent: THandle;PHeight:integer;PWidth:integer ): boolean;
begin
  frm:=TfrmMain.Create(nil);  //
  DM:=TDM.Create(application); //application
  if  WINAPI.Windows.SetParent(frm.Handle,Parent)=0 then
  begin
    Result:=False;
    frm.Free;
    Exit;
  end;
 SetWindowLong(frm.Handle,GWL_STYLE,GetWindowLong(frm.Handle,GWL_STYLE) and not (WS_CAPTION or WS_THICKFRAME));
//   //WS_CAPTION和WS_THICKFRAME分别表示标题栏和边框
WINAPI.Windows.MoveWindow(frm.Handle,0,0,PWidth,PHeight,True);
 frm.Show;
end;  

使用winAPI 来实现窗体的处理,比直接用delphi 常规代码,更具有兼容性。

外部EXE中的调用代码,我想不需要特别列出,其实与同版本调用,已经是完样一样的了。

就是普通的接口调用了

IInterfacedFunc=interface
  ['{1440EC99-A782-4E12-9F82-3020C8D887B4}']
    Function Showform(Parent: THandle;PHeight:integer;PWidth:integer):boolean;stdcall;
    procedure Resize(PHeight:integer;PWidth:integer);stdcall;
    procedure CloseForm ;stdcall;
    procedure SetCloseProc(Proc:TCloseProc);stdcall;
 end;

  附:接口pas代码。