Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code sets some text to an open notepad window. My question is how does it work safely?
C++
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;

}

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 ?
Posted
Comments
CPallini 2-Apr-13 5:25am    
Does that mean we can use SendMessage effectively as an IPC mechanism ?
Yes, you can.

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[^]
 
Share this answer
 
Comments
Garth J Lancaster 2-Apr-13 5:25am    
your response is better than mine .. not sure about the 'uncommon to use it as IPC' though - I'd have looked at it before namedpipeserver - well, I guess it depends on what sort of project/program I'm working on
Zoltán Zörgő 2-Apr-13 5:31am    
You'r right. If you can live with it's limitations - can be a good, and really fast method. Still, in nowadays heterogeneous and distributed environments I would really think twice. An application need can grow quick beyond this tool's target scale and you finish up by rewriting the whole IPC architecture of your application.
iDebD 2-Apr-13 6:02am    
Hi Zoltán Zörgő and Garth J Lancaster ,

Thanks for your comments. Really appreciate that. I am aware of the message architecture of windows. What I wanted to know is the memory intricacies for the pString pointer. The string exists in the virtual memory of the first process. SendMessage() as I understand will make the windows OS call the window procedure in the notepad.exe which handles the messages sent to a notepad window. This window procedure of notepad.exe will receive the pointer to the string as a IN parameter and then sets the same to its client area.

Is this pointer address not the same as I sent from my application(with the code posted)? Because if it is so , that might not hold correct value in the context of notepad. Or does it gets handled by the operating system which calls the window procedure of notepad with a valid pointer (in its context )to the string that is passed in SendMessage()?
Jochen Arndt 2-Apr-13 6:18am    
You are passing a pointer with a valid memory address. The WM_SETTEXT handler will use this pointer to copy the text into newly allocated memory owned by the receiving control.
Zoltán Zörgő 2-Apr-13 6:36am    
As Jochen Arndt wrote, and I mentioned it too: the message you send is received by the system, not by the addressee. The system is dispatching the message to the addressee. And in the background the message structure is partly copied - partly rebuilt. This is not memory sharing. For memory sharing see my last link or search with google.
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'
 
Share this answer
 
v2
You are not pass the pointer to process "Notepad.exe". You run WinApi function in the current process, which use valid pointer to string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900