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

胡伟峰 2014-08-29 09:02:06
这个问题一直困扰好多时候了,一直卡着没有解决,无法使用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.
...全文
108 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hj8090 2014-08-29
  • 打赏
  • 举报
回复
协议不一致啊, dll中指定为stdcall exe中使用了默认值,应该是Register吧。 方法1 修改dll中的stdcall为Register。 方法2:修改调用方法 procedure TForm1.btn1Click(Sender: TObject); Type tshowform = Procedure(ff: THandle) stdcall; Var hnd: THandle; //showform: tshowform; showform: Procedure(ff: 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;
胡伟峰 2014-08-29
  • 打赏
  • 举报
回复
恩,问题已解决,是动态调用dll 的时候少了stdcall。结贴
程晨c 2014-08-29
  • 打赏
  • 举报
回复
同一个进程内Application.handle的值应该是唯一的, 即使在dll中创建窗体也不需要传入exe的Application.handle到dll中, 在dll中直接Application.handle就可以了
程晨c 2014-08-29
  • 打赏
  • 举报
回复
传入的hanle值是不会变的,除非代码有问题

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧