首页 新闻 会员 周边

动态调用dll内封装的窗体发现从exe传入任何handle到dll内部handle值变了,搜索很久发现网上没有类似相关资

0
悬赏园豆:20 [已关闭问题] 关闭于 2014-08-29 10:25

这个问题一直困扰好多时候了,一直卡着没有解决,无法使用Dll封装窗体。我想使用动态加载dll调用非模式窗体,看了很多教程,都是早几年的,我用的是delphi XE5。
按照网上实例做了(最简单的那种,仅显示无任何代码封装的窗体),一直报错,我跟踪了下,发现在dll窗体内我不把exe传进来的AppHandle赋值到Application.handle就可以用TForm.Create(Application)生成,一赋值报无效的句柄,再查传入前Exe的Application.handle与Dll内传入参数handle不一样,这是什么情况?
请高手们帮忙解答,哎,做了很多实验,一直没有解决,网上也没有新出来的教程实例,都是一些老文章,没有Delphi XE5平台下的文章(我都怀疑是否Delphi平台问题引起的)

下面是我的源码


Dll工程文件:

library mydll;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

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

{$R *.res}

//此处也试过把dll里面窗体showform函数放到这里来,问题依旧
//正常情况下这边应该还有一些dll卸载机制以及Dll的Application.handle还原机制,和我发现的问题没有什么太大关系,暂时没有加上去
exports
  showform;
begin
end.

 

Dll的窗体文件:

unit Unit1;
interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  procedure showform(ff:THandle);stdcall;

implementation

{$R *.dfm}

procedure showform(ff: THandle);stdcall;
var
  A: THandle;
begin
  a := Application.Handle;   //默认这个A值竟然是exe的application.handle
  Application.Handle := ff;  //传递进来的ff竟然不等于exe的application.handle,很无奈,颠覆了我学到的Dll的知识,但是又发现网上竟然没有类似的问题
  Form1:=TForm1.Create(Application);
  Form1.Show;
end;
end.

 

exe文件的源码:

unit Unitmain;
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses Math;

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
  showform: procedure(ff: THandle);
  hnd: THandle;
begin
  hnd := LoadLibrary(PChar('mydll.dll'));
  if hnd>0 then
  begin
    @showform := GetProcAddress(hnd,PChar('showform'));
    if @showform <> nil then
        showform(Application.Handle);
  end;
end;

end.
胡伟峰的主页 胡伟峰 | 菜鸟二级 | 园豆:202
提问于:2014-08-29 09:09
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册