关于网友提出的“ 动态调用dll内封装的窗体发现从exe传入任何handle到dll内部handle值变了,搜索很久发现网上没有类似相关资料”问题疑问,本网通过在网上对“ 动态调用dll内封装的窗体发现从exe传入任何handle到dll内部handle值变了,搜索很久发现网上没有类似相关资料”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 动态调用dll内封装的窗体发现从exe传入任何handle到dll内部handle值变了,搜索很久发现网上没有类似相关资料
描述:Delphidll非模式窗体handle传递
这个问题一直困扰好多时候了,一直卡着没有解决,无法使用Dll封装窗体。我想使用动态加载dll调用非模式窗体,看了很多jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播,都是早几年的,我用的是delphi XE5。
按照网上实例做了(最简单的那种,仅显示无任何代码封装的窗体),一直报错,我跟踪了下,发现在dll窗体内我不把exe传进来的AppHandle赋值到Application.handle就可以用TForm.Create(Application)生成,一赋值报无效的句柄,再查传入前Exe的Application.handle与Dll内传入参数handle不一样,这是什么情况?
请高手们帮忙解答,哎,做了很多实验,一直没有解决,网上也没有新出来的jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播实例,都是一些老文章,没有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.
解决方案1: 同一个进程内Application.handle的值应该是唯一的,
即使在dll中创建窗体也不需要传入exe的Application.handle到dll中,
在dll中直接Application.handle就可以了
解决方案2: 传入的hanle值是不会变的,除非代码有问题
以上介绍了“ 动态调用dll内封装的窗体发现从exe传入任何handle到dll内部handle值变了,搜索很久发现网上没有类似相关资料”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2188699.html