我们可以有效地使用SendMessage作为IPC机制吗? [英] Can we use SendMessage effectively as an IPC mechanism ?

查看:153
本文介绍了我们可以有效地使用SendMessage作为IPC机制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将一些文本设置为打开的记事本窗口。我的问题是它如何安全地工作?

The following code sets some text to an open notepad window. My question is how does it work safely?

int _tmain(int argc, _TCHAR* argv[])
{
               HWND hwnd = FindWindow(_T("Notepad"),_T("Untitled - Notepad"));

                if(hwnd!=NULL)

                {

                                wchar_t* pString = L"hi";

                                HWND hwndEdit = FindWindowEx(hwnd,NULL,_T("Edit"),NULL);

                                SendMessage(hwndEdit,WM_SETTEXT,0,(LPARAM)pString);

                }

                return 0;

}



指针pString存在于当前进程空间的虚拟内存中,可能不是上下文中的有效内存地址of notepad.exe.But奇怪的是它确实正确设置了文本,这意味着不仅指针在notepad.exe上下文中有效,而且它也指向正确的字符串。这是怎么回事?



这是否意味着我们可以有效地使用SendMessage作为IPC机制?


The pointer "pString" exists in the virtual memory of the current process space, which might not be valid memory address in the context of notepad.exe.But strangely it does set the text correctly , which means not only the pointer is valid in the notepad.exe context but it points to the correct string also. How is this happening ?

Does that mean we can use SendMessage effectively as an IPC mechanism ?

推荐答案

SendMessage不会将您的字符串传递给记事本。

一个非常简约的概述:所有的Windows架构都基于消息。如果移动鼠标会生成一条消息,如果按某个键,它会生成一条消息......依此类推。有大量消息 [ ^ ]类型。消息从顶部传递到底部,可以由应用程序处理。只需使用 ManagedSpy 进行快速浏览[ ^ ],或其他查看消息和队列的工具。即使您的API大部分隐藏了这一点,您的应用程序也会有一个消息队列,一个调度程序和一些处理程序。请参阅以下简要概述: http://www.tenouk.com/visualcplusmfc/visualcplusmfc2.html [ ^ ]



现在,在您的情况下,记事本只是从系统接收文本输入消息 - 而不是从您的代码中,您只需将消息发布到发往该窗口的系统队列中。请注意,消息目标是窗口,而不是应用程序。



即使您有用户定义的消息类型,使用它作为IPC也是非常罕见的。它本身是可靠的,但不是开发人员友好的,并且有一些限制。但是,如果您真的想要,请参阅以下示例: http://code.msdn.microsoft.com / windowsdesktop / CppSendWMCOPYDATA-f75bc681 [ ^ ]



对于更常见的IPC可能性,我建议你从这里开始:

Windows IPC [ ^ ]。

http://code.msdn.microsoft.com/windowsdesktop/CppNamedPipeServer-d1778534 [ ^ ]

http:/ /www.codeguru.com/cpp/wp/system/sharedmemory/article.php/c2879/Shared-Memory-Inter-Process-Communication-IPC.htm [ ^ ]
SendMessage does not pass your string to Notepad.
A really minimalistic overview: all the windows architecture is based on messages. If you move your mouse it generates a message, if you press a key, it generates a message... and so on. There are plenty of message[^] types. The messages are passed from top to bottom and can be handled by the applications. Just do a quick tour with ManagedSpy[^], or other tools to see the messages and the queues. Even if your API is mostly hiding this from you, your application has a message queue a dispatcher and some handlers. See following brief overview: http://www.tenouk.com/visualcplusmfc/visualcplusmfc2.html[^]

Now in your case notepad simply receives a text input message from the system - not from your code, you just post the message in the system queue addressed to that window. Please note, that the message target is the window, not the application.

Even if you have a user defined message type, it is quite uncommon to use it as IPC. It is reliable on it''s own, but not developer friendly and has several limitations. But see following sample of how to do it, if you really want: http://code.msdn.microsoft.com/windowsdesktop/CppSendWMCOPYDATA-f75bc681[^]

For more common IPC possibilities I suggest you start here:
Windows IPC[^].
http://code.msdn.microsoft.com/windowsdesktop/CppNamedPipeServer-d1778534[^]
http://www.codeguru.com/cpp/w-p/system/sharedmemory/article.php/c2879/Shared-Memory-Inter-Process-Communication-IPC.htm[^]


问题的第一部分......它的工作方式如何?每个窗口基本上都有一个与之关联的消息队列 - 当你向窗口发送消息时,该消息最终会出现在窗口的消息队列中 - 因此与发送''进程'不相关



自从我处理它以来已经很长时间了,谷歌的c ++ ipc sendmessage或c ++ ipc postmessage可能会带来更好的实施细节 - 这里有一个链接可能有助于使用WM_COPYDATA的进程间通信 [ ^ ]



答案是是,你可以使用SendMessage作为IPC机制,但是有特定的技术



''g''
The first part of your question ... how dows it work ? each window basically has a message queue associated with it - when you sendmessage to a window, that message ends up on the window''s message queue - and is therefore dissasociated with the sending ''process''

Its been a long time since Ive dealt with it, a google for c++ ipc sendmessage or c++ ipc postmessage might bring up better details on implementation - there''s a link here that may help Inter-Process Communication using WM_COPYDATA[^]

The answer is ''yes'', you can use SendMessage as an IPC mechanism, but there are specific techniques

''g''


您没有传递指针来处理Notepad.exe。您在当前进程中运行WinApi函数,该函数使用指向字符串的有效指针。
You are not pass the pointer to process "Notepad.exe". You run WinApi function in the current process, which use valid pointer to string.


这篇关于我们可以有效地使用SendMessage作为IPC机制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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