Reading window messages of another thread

  • Thread starter Thread starter Max Khitrov
  • Start date Start date
M

Max Khitrov

Hi there,

I realize this is more of a Windows API question then C#, but thought
I'd ask anyway just in case anyone had experience with this specifically
in C#. What I'm trying to do is read window messages for windows that
aren't owned by current thread. That's the key part because if I just
wanted to read messages for my own windows I'd just use the GetMessage
function from user32.dll.

I have no problems finding the window I'm looking for, but if I call
GetMessage with that window's handle, all it does is just sit there and
never return a thing. For those of you who have used Spy++ you know that
it's possible to just read the messages of any random window. My
question is how would I be able to do this from my app. I've looked
through the MSDN docs on this topic, but any function that would let me
read messages always states that it has to be for the window owned by
current thread.

Any ideas on a way around that? Thanks.
 
The standard way of doing this in Win32 is to use SetWindowsHookEx with the
WH_GETMESSAGE and WH_CALLWNDPROC filters. I believe Spy uses this API. The
source code for Spy is in the SDK and therefore you can dump the code to
determine how to do it. There is no .NET equivalent for arbitrary windows.
For your own windows you can just overload the window procedure and raise an
event but it would dramatically slow things down. Refer to "Win32 Hooks" in
MSDN for more info on SetWindowsHookEx.

Michael Taylor - 7/3/05
 
Back
Top