Runt888
May 24th, 2006, 03:58 PM
If I use SendMessage to send a message to a window in a different process, how does the return value get passed back to my process?
Here's what I think happens (in very broad steps):
1. I call SendMessage.
2. Windows puts the message in the message queue for the specified window, and puts my process's thread to sleep.
3. The other process's message loop eventually calls DispatchMessage with my message.
4. Some window procedure handles the message and returns a value.
5. ???
6. Windows sets the return value for the SendMessage call and wakes up my process's thread.
How does Windows deal with step 5?
You may be wondering why I need to know. I'm building an app that only allows a single instance to run at one time. When my app starts up, it calls RegisterWindowMessage to register a system wide message. If it detects that another instance is running, it sends this message to every window in the system (using SendMessageTimeout). If the return value from SendMessageTimeout is equal to my system wide message, then I know that I've found the correct window handle, and I call SetForegroundWindow to show the first instance of my app.
This works great, assuming that my window procedure handles the message correctly. However, in my windowing framework, I allow the message processing to be overridden, so there's no guarantee that the message will be handled correctly (in theory if the message processing is overridden, the default message processing will be called eventually, but it's not enforced).
So I'm trying to handle this message in my message loop, before it gets passed to the window procedure for processing. Testing for the message in the message loop is trivial. However, I don't know how to handle the return value.
Any ideas?
Thanks!
Kelly
Here's what I think happens (in very broad steps):
1. I call SendMessage.
2. Windows puts the message in the message queue for the specified window, and puts my process's thread to sleep.
3. The other process's message loop eventually calls DispatchMessage with my message.
4. Some window procedure handles the message and returns a value.
5. ???
6. Windows sets the return value for the SendMessage call and wakes up my process's thread.
How does Windows deal with step 5?
You may be wondering why I need to know. I'm building an app that only allows a single instance to run at one time. When my app starts up, it calls RegisterWindowMessage to register a system wide message. If it detects that another instance is running, it sends this message to every window in the system (using SendMessageTimeout). If the return value from SendMessageTimeout is equal to my system wide message, then I know that I've found the correct window handle, and I call SetForegroundWindow to show the first instance of my app.
This works great, assuming that my window procedure handles the message correctly. However, in my windowing framework, I allow the message processing to be overridden, so there's no guarantee that the message will be handled correctly (in theory if the message processing is overridden, the default message processing will be called eventually, but it's not enforced).
So I'm trying to handle this message in my message loop, before it gets passed to the window procedure for processing. Testing for the message in the message loop is trivial. However, I don't know how to handle the return value.
Any ideas?
Thanks!
Kelly