如何在单独的进程中使用 SendMessage(..) 发送数据记录
我用来在两个单独的进程上发送数据,但失败了。它只能在相同的进程下工作......这是概念。
//------------------------------------------------ -----------------------------------
主要应用
//------------------------------------------------ -----------------------------------
Type
PMyrec = ^TMyrec;
TMyrec = Record
name : string;
add : string;
age : integer;
end;
:OnButtonSend
var aData : PMyrec;
begin
new(aData);
aData.Name := 'MyName';
aData.Add := 'My Address';
aData.Age : 18;
SendMessage(FindWindow('SubApps'),WM_MyMessage,0,Integer(@aData));
end;
//---------- -------------------------------------------------- -----------------------
子应用程序
//------------------------------------------------ -----------------------------------
Type
PMyrec = ^TMyrec;
TMyrec = Record
name : string;
add : string;
age : integer;
end;
:OnCaptureMessage
var
aData : PMyrec;
begin
aData := PMyrec(Msg.LParam);
showmessage(aData^.Name);
end;
i use to send a data on two separate process but it fails. it works only under same process... this is concept.
//-----------------------------------------------------------------------------------
MainApps
//-----------------------------------------------------------------------------------
Type
PMyrec = ^TMyrec;
TMyrec = Record
name : string;
add : string;
age : integer;
end;
:OnButtonSend
var aData : PMyrec;
begin
new(aData);
aData.Name := 'MyName';
aData.Add := 'My Address';
aData.Age : 18;
SendMessage(FindWindow('SubApps'),WM_MyMessage,0,Integer(@aData));
end;
//-----------------------------------------------------------------------------------
SubApps
//-----------------------------------------------------------------------------------
Type
PMyrec = ^TMyrec;
TMyrec = Record
name : string;
add : string;
age : integer;
end;
:OnCaptureMessage
var
aData : PMyrec;
begin
aData := PMyrec(Msg.LParam);
showmessage(aData^.Name);
end;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你说得对。地址仅在单个进程内才有意义。您在第一个进程中创建的 PMyRec 值只是目标进程中的垃圾地址。
要通过窗口消息将任意内存块发送到另一个进程,您应该使用
wm_CopyData
消息。您向该消息提供数据的地址和大小,操作系统负责将其复制到目标进程的地址空间中。由于您的数据包含一个字符串,该字符串在内部表示为另一个指针,因此仅复制记录的 12 个字节是不够的。您需要分配额外的内存来将记录和字符串数据保存在单个内存块中,以便 wm_CopyData 可以复制它并且目标进程可以读取它。
这是一种方法,使用流将数据收集到单个内存块中。
除了字符串的字符之外,我们还写入字符串的长度,以便接收者知道每个字符串有多少个字符。接收者的代码将如下所示:
我使用了非标准的
TReadOnlyMemoryStream
因为它使一切变得更容易。这是一个简单的实现:You're right. Addresses only have meaning within a single process. The PMyRec value you create in the first process is just a garbage address in the target process.
To send an arbitrary block of memory to another process via a window message, you should use the
wm_CopyData
message. You give that message the address of the data and the size, and the OS takes care of copying it into the target process's address space.Since your data includes a string, which is represented internally as a another pointer, it won't be enough to just copy the 12 bytes of your record. You'll need to allocate additional memory to hold the record and the string data in a single block of memory so
wm_CopyData
can copy it and the target process can read it.Here's one way to do it, using a stream to collect the data into a single block of memory.
We write the lengths of the strings in addition to the strings' characters so that the recipient knows how many characters belong to each one. The recipient's code will look like this:
I've used the non-standard
TReadOnlyMemoryStream
since it makes everything easier. Here's a simple implementation for it: