WM_COPYDATA中出现的问题 ( 积分: 20 )

  • 主题发起人 主题发起人 iforward
  • 开始时间 开始时间
I

iforward

Unregistered / Unconfirmed
GUEST, unregistred user!
发送端的程序的代码如下<br>unit&nbsp;Unit1;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls;<br><br>type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;Button1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit1:&nbsp;TEdit;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;SendMessageTo(hwndSend:HWND;s:string);<br><br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br>&nbsp;&nbsp;oldhwnd:HWND;<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;oldhwnd:=FindWindow(nil,'Form2');<br>&nbsp;&nbsp;SendMessageTo(oldhwnd,'hello&nbsp;copydata');<br>end;<br><br>procedure&nbsp;TForm1.SendMessageTo(hwndSend:&nbsp;HWND;&nbsp;s:&nbsp;string);<br>const<br>&nbsp;&nbsp;Msg=$1357;<br>var<br>&nbsp;&nbsp;DataBuffer:TCopyDataStruct;<br>&nbsp;&nbsp;Buf:PChar;<br>&nbsp;&nbsp;BufSize:Integer;<br>begin<br>&nbsp;&nbsp;BufSize:=Length(s);<br>&nbsp;&nbsp;Buf:=AllocMem(BufSize);<br>&nbsp;&nbsp;strCopy(Buf,Pchar(s));<br><br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;DataBuffer&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwData:=Msg;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cbData:=BufSize;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpData:=Buf;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;SendMessage(hwndSend,WM_COPYDATA,0,lParam(@DataBuffer));<br>&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;FreeMem(Buf,Bufsize);<br>&nbsp;&nbsp;end;<br>end;<br><br>end.<br><br>接收端的代码如下<br>unit&nbsp;Unit2;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls;<br><br>type<br>&nbsp;&nbsp;TForm2&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;Memo1:&nbsp;TMemo;<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;WMCopyData(var&nbsp;M:TMessage);message&nbsp;WM_COPYDATA;<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form2:&nbsp;TForm2;<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>{&nbsp;TForm2&nbsp;}<br><br>procedure&nbsp;TForm2.WMCopyData(var&nbsp;M:&nbsp;TMessage);<br>const<br>&nbsp;&nbsp;Msg=$1357;<br>begin<br>&nbsp;&nbsp;Memo1.Clear;<br>&nbsp;&nbsp;if&nbsp;PCopyDataStruct(M.LParam)^.dwData=Msg&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;Memo1.Lines.Add(string(PCopyDataStruct(M.LParam)^.lpData));<br>end;<br><br>end.<br><br>现在出现的问题是接收端受到的是hello&nbsp;copydata后面还有一堆乱码<br>我试了好几次都没能解决
 
procedure&nbsp;TForm2.WMCopyData(var&nbsp;M:&nbsp;TMessage);<br>const<br>&nbsp;&nbsp;Msg=$1357;<br>var<br>&nbsp;&nbsp;s:string;<br>begin<br>&nbsp;&nbsp;Memo1.Clear;<br><br>&nbsp;&nbsp;if&nbsp;PCopyDataStruct(M.LParam)^.dwData=Msg&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;SetLength(S,&nbsp;Msg.CopyDataStruct.cbData);<br>&nbsp;&nbsp;&nbsp;&nbsp;CopyMemory(@S[1],&nbsp;Msg.CopyDataStruct.lpData,&nbsp;Msg.CopyDataStruct.cbData);<br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;Memo1.Lines.Add(s);<br>end;
 
这样子试试看
 
改发送端:<br>&nbsp;&nbsp;BufSize:=Length(s)&nbsp;+&nbsp;1;<br>&nbsp;&nbsp;Buf:=AllocMem(BufSize);<br>&nbsp;&nbsp;ZeroMemory(Buf,&nbsp;BufSize);<br>&nbsp;&nbsp;strCopy(Buf,Pchar(s));<br><br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;DataBuffer&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwData:=Msg;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cbData:=BufSize;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpData:=Buf;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;SendMessage(hwndSend,WM_COPYDATA,0,lParam(@DataBuffer));<br>&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;FreeMem(Buf,Bufsize);<br>&nbsp;&nbsp;end;<br><br>或者改接收端:<br>var<br>&nbsp;&nbsp;s&nbsp;:&nbsp;string;<br>begin<br>&nbsp;&nbsp;Memo1.Clear;<br>&nbsp;&nbsp;if&nbsp;PCopyDataStruct(M.LParam)^.dwData=Msg&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;:=&nbsp;Copy(PChar(PCopyDataStruct(M.LParam)^.lpData),&nbsp;0,&nbsp;PCopyDataStruct(M.LParam)^.cbData&nbsp;-&nbsp;1);<br>&nbsp;&nbsp;&nbsp;&nbsp;Memo1.Lines.Add(s);<br>&nbsp;&nbsp;end;<br>end;
 
发现了一个奇怪的问题<br>出现在发送端代码<br>procedure&nbsp;TForm1.SendMessageTo(hwndSend:&nbsp;HWND;&nbsp;s:&nbsp;string);<br>const<br>&nbsp;&nbsp;Msg=$1357;<br>var<br>&nbsp;&nbsp;DataBuffer:TCopyDataStruct;<br>&nbsp;&nbsp;Buf:PChar;<br>&nbsp;&nbsp;BufSize:Integer;<br>begin<br>{//下面这么写接收端仍然会有乱码<br>&nbsp;&nbsp;BufSize:=Length(s);<br>&nbsp;&nbsp;Buf:=AllocMem(BufSize+1);<br>}<br>//而改成如下的话<br>&nbsp;&nbsp;BufSize:=Length(s)+1;<br>&nbsp;&nbsp;Buf:=AllocMem(BufSize);<br>&nbsp;就正常了<br>&nbsp;&nbsp;strCopy(Buf,Pchar(s));<br><br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;DataBuffer&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwData:=Msg;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cbData:=BufSize;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpData:=Buf;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;SendMessage(hwndSend,WM_COPYDATA,0,lParam(@DataBuffer));<br>&nbsp;&nbsp;finally<br>&nbsp;&nbsp;&nbsp;&nbsp;FreeMem(Buf,Bufsize);<br>&nbsp;&nbsp;end;<br>end;<br><br>还有allocMem在分配完缓存后是会自动用0填充的<br>所以那个ZeroMemory可以不用的<br><br>只是上面的那种情况又让我迷惑了,真是奇怪了
 
procedure&nbsp;WMCopyData(var&nbsp;M:TCOPYDATA);message&nbsp;WM_COPYDATA;<br>可能是这样的,具体我记不起来了
 
后退
顶部