两个应用程序消息通信的问题
接收的程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
MSG_USERDEFINE = WM_COPYDATA + 20;
//MSG_USERDEFINE = WM_USER + 20;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Memo1: TMemo;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ProcessMyMsg(var Msg: TMessage); message MSG_USERDEFINE;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ProcessMyMsg(var Msg: TMessage);
var
CDS: ^tagCOPYDATASTRUCT;
begin
CDS := Pointer(Msg.lParam);
Memo1.Lines.Add(StrPas(CDS.lpData));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
CDS: tagCOPYDATASTRUCT;
begin
CDS.cbData := Length(Edit2.Text)+1;
CDS.lpData := PChar(Edit2.Text);
PostMessage(handle, MSG_USERDEFINE, 0, integer(@CDS));
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Edit1.Text :=IntToStr(handle);
end;
end.
***************************************
上面的程序自发自收没有问题
发送程序
unit Unit_Sen;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
MSG_USERDEFINE = WM_USER + 20;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
aaa:string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
CDS: tagCOPYDATASTRUCT;
begin
aaa := Memo1.Text;
CDS.dwData := Length(aaa)+1;
CDS.cbData := Length(aaa)+1;
CDS.lpData := PChar(aaa);
SendMessage(StrToInt(Edit1.Text), MSG_USERDEFINE, handle, integer(@CDS));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
strMSG: string;
Data: tagCOPYDATASTRUCT;
pBuf: PChar;
begin
strMSG := Memo1.Text;
GetMem(pBuf, Length(strMSG) + 1);
try
ZeroMemory(pBuf, Length(strMSG) + 1);
StrPCopy(pBuf, strMSG);
Data.dwData:= Length(strMSG);
Data.cbData:= Length(strMSG);
Data.lpData:= pBuf;
SendMessage(StrToInt(Edit1.Text), MSG_USERDEFINE, handle, Integer(@Data));
finally
FreeMem(pBuf);
end;
end;
end.
****************************
2种发送 发送时接收程序提示错误或者接收到的内容是错误的,是不是内存访问有限制?我要实现两个应用程序间消息的通信应如何做,谢谢