Click here to Skip to main content
10,500,000 members (133,906 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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"));
                                wchar_t* pString = L"hi";
                                HWND hwndEdit = FindWindowEx(hwnd,NULL,_T("Edit"),NULL);
                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 1-Apr-13 22:57pm
CPallini 2-Apr-13 5:25am
Does that mean we can use SendMessage effectively as an IPC mechanism ?
Yes, you can.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:[^]

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:[^]

For more common IPC possibilities I suggest you start here:
Windows IPC[^].[^][^]
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.
bubaidada 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.
bubaidada 2-Apr-13 6:40am
This makes sense .. Thanks. :) but why do you think the author needed to do memory related handing for the below case of SendMessage()?
Zoltán Zörgő 2-Apr-13 6:47am
Because it does read remote process memory.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You are not pass the pointer to process "Notepad.exe". You run WinApi function in the current process, which use valid pointer to string.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100