title: Delphi: WM_COPYDATA work once
body: <p>Based on <a href="http://delphi.about.com/od/windowsshellapi/a/wm_copydata.htm" rel="nofollow">this post</a> I have wrote a simple project to communicate two app, but it work only once and imperfect too. </p>
  
  <p>If I send "stackoverflow" I'll receive "stackov" on the other side and once. It seems that the <code>WMGetData</code> procedure is not triggered anymore.</p>
  
  <p>Sender code:<br/>
  </p>
  
  <pre><code>procedure TForm1.Button1Click(Sender: TObject);
  var
    CDS: TCopyDataStruct;
    receiverHandle : THandle;
    res : integer;
    S: string;
  begin
    S:= Edit1.Text;
    CDS.dwData:= 0;   //Identify message
    CDS.cbData:= Length(S);
    CDS.lpData:= PChar(S);
  
    receiverHandle := FindWindow(PChar('TForm2'),PChar('Form2')) ;
    if receiverHandle &lt;&gt; 0 then
      res := SendMessage(receiverHandle, WM_COPYDATA, Integer(Handle), Integer(@CDS))
    else
      Caption:= 'Not Found';
  
    Caption:= IntToStr(Res);
    CloseHandle(receiverHandle);
  end;
  </code></pre>
  
  <p>Receiver code:<br/></p>
  
  <pre><code>  TForm2 = class(TForm)
      procedure WMGetData(var Msg : TWMCopyData) ; message WM_COPYDATA;
  
  ...
  
  procedure TForm2.WMGetData(var Msg: TWMCopyData);
  var
    S: string;
  begin
    S:= PChar(Msg.CopyDataStruct.lpData);
    Caption := Caption + S;
    msg.Result := 2006;   //ACK for sender app.
  end;
  </code></pre>