Delphi:WM_COPYDATA 工作一次 [英] Delphi: WM_COPYDATA work once

查看:29
本文介绍了Delphi:WM_COPYDATA 工作一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于这篇文章我写了一个简单的项目来交流两个应用程序,但它只工作一次,也不完美.

Based on this post I have wrote a simple project to communicate two app, but it work only once and imperfect too.

如果我发送stackoverflow",我会在另一端收到stackov",一次.似乎不再触发 WMGetData 过程.

If I send "stackoverflow" I'll receive "stackov" on the other side and once. It seems that the WMGetData procedure is not triggered anymore.

发件人代码:

Sender 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 <> 0 then
    res := SendMessage(receiverHandle, WM_COPYDATA, Integer(Handle), Integer(@CDS))
  else
    Caption:= 'Not Found';

  Caption:= IntToStr(Res);
  CloseHandle(receiverHandle);
end;

接收方代码:

Receiver 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;

推荐答案

作为 文档指定

CDS.cbData:= Length(S);

应该是

CDS.cbData:= Length(S) * SizeOf(S[1])

因为 cbData 成员应该包含您传输的字节数,而 delphi XE string 类型是指一个 unicode 字符串(每个字符 2 个字节)

since the cbData member should contains the number of bytes you transfer, and delphi XE string type refers to a unicode string (2 bytes per characters)

你的第二个问题

receiverHandle := FindWindow(PChar('TForm2'),PChar('Form2')) ;

我最好的猜测是该调用第二次失败,因为您将第二个表单的标题更改为 Form2Stackov.

My best guess is that this call fails the second time because you changed the caption of your second form to Form2Stackov.

这篇关于Delphi:WM_COPYDATA 工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆