EXE和DLL相互之间如何发送消息?(100分)

  • 主题发起人 主题发起人 HNXXCXG
  • 开始时间 开始时间
相互传各自的 handle
在 exe 和 dll 中都定义一个相同的消息 wm_fset = wm_user + 100
exe 调用 dll 时把 自己的 handle 传过去,然后 dll 可以发消息 wm_fset 到 这个 exe ,当然是发给 handle 的

关于 dll 的handle ,动态调用的话,返回的就是了

要是共享数据也可以用内存映象文件
 
好像楼上的办法在DLL和EXE间不适用哦:)
我记得在BPL和主程序间发消息是要用广播消息的,DLL应该也是如此吧。
 
同一进程空间的 dll 和 exe 需要这么麻烦么 ?
使用消息需要消息窗口函数;使用“内存映象文件”确实可以共享,不过同样需要辅助通知机制啊。
dll 直接 export 一个函数,实现 exe call dll
dll 再 export 一个 callback 回调函数,exe 实现 callback 扔给 dll,实现 dll call exe
 
不说了,楼上的兄弟们都说的差不多了,我就来贴代码吧。

//主程序代码
unit Unit1;

interface

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

const
WM_MYMSG = WM_USER + 2008;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FDLLHandle: THandle;
procedure ShellMsgFunc(var AMsg: TMessage); message WM_MYMSG;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

function ShowDLLForm(const AppHandle: THandle): THandle; stdcall;
external 'Project2.dll';

procedure TForm1.ShellMsgFunc(var AMsg: TMessage);
begin
case AMsg.WParam of
2001: {代码};
2002: {代码};
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
FDLLHandle := ShowDLLForm(Handle);
SendMessage(FDLLHandle,WM_MYMSG,2001,0);
end;

end.

//DLL 代码
unit Unit1;

interface

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

const
WM_MYMSG = WM_USER + 2008;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FDLLHandle: THandle;
procedure ShellMsgFunc(var AMsg: TMessage); message WM_MYMSG;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

function ShowDLLForm(const AppHandle: THandle): THandle; stdcall;
external 'Project2.dll';

procedure TForm1.ShellMsgFunc(var AMsg: TMessage);
begin
case AMsg.WParam of
2001: {代码};
2002: {代码};
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
FDLLHandle := ShowDLLForm(Handle);
SendMessage(FDLLHandle,WM_MYMSG,2001,0);
end;

end.

//DLL 中窗体代码
unit Unit2;

interface

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

const
WM_MYMSG = WM_USER + 2008;

type
TForm2 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FAppHWND: THandle;
procedure SetAppHWND(const Value: THandle);
procedure ShellMsgFunc(var AMsg: TMessage); message WM_MYMSG;
{ Private declarations }
public
property AppHWND: THandle read FAppHWND write SetAppHWND;
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;

procedure TForm2.SetAppHWND(const Value: THandle);
begin
FAppHWND := Value;
end;

procedure TForm2.ShellMsgFunc(var AMsg: TMessage);
begin
case AMsg.WParam of
2001: Caption := IntToStr(2001);
2002: {代码};
end;
end;

end.
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
925
DelphiTeacher的专栏
D
D
回复
0
查看
880
DelphiTeacher的专栏
D
D
回复
0
查看
853
DelphiTeacher的专栏
D
后退
顶部