I
iforward
Unregistered / Unconfirmed
GUEST, unregistred user!
发送端的程序的代码如下<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Edit1: TEdit;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> procedure SendMessageTo(hwndSend:HWND;s:string);<br><br> end;<br><br>var<br> Form1: TForm1;<br> oldhwnd:HWND;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> oldhwnd:=FindWindow(nil,'Form2');<br> SendMessageTo(oldhwnd,'hello copydata');<br>end;<br><br>procedure TForm1.SendMessageTo(hwndSend: HWND; s: string);<br>const<br> Msg=$1357;<br>var<br> DataBuffer:TCopyDataStruct;<br> BufChar;<br> BufSize:Integer;<br>begin<br> BufSize:=Length(s);<br> Buf:=AllocMem(BufSize);<br> strCopy(Buf,Pchar(s));<br><br> try<br> with DataBuffer do<br> begin<br> dwData:=Msg;<br> cbData:=BufSize;<br> lpData:=Buf;<br> end;<br> SendMessage(hwndSend,WM_COPYDATA,0,lParam(@DataBuffer));<br> finally<br> FreeMem(Buf,Bufsize);<br> end;<br>end;<br><br>end.<br><br>接收端的代码如下<br>unit Unit2;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm2 = class(TForm)<br> Memo1: TMemo;<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> procedure WMCopyData(var M:TMessage);message WM_COPYDATA;<br> end;<br><br>var<br> Form2: TForm2;<br><br>implementation<br><br>{$R *.dfm}<br><br>{ TForm2 }<br><br>procedure TForm2.WMCopyData(var M: TMessage);<br>const<br> Msg=$1357;<br>begin<br> Memo1.Clear;<br> if PCopyDataStruct(M.LParam)^.dwData=Msg then<br> Memo1.Lines.Add(string(PCopyDataStruct(M.LParam)^.lpData));<br>end;<br><br>end.<br><br>现在出现的问题是接收端受到的是hello copydata后面还有一堆乱码<br>我试了好几次都没能解决