Sign in to follow this  

Monitoring another application's messages

This topic is 3376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In c++, I am trying to monitor an application's messages, more specifically, a certain list box on its form so that I can sort of replicate it on my own form. I've thought about periodically sending a wm_gettext message to the listbox, but that seems inefficient, and so I am trying to go the SetWindowsHookEx route. So far I have made a small dll, so that I can hook it into the application's message stream and I also made an exe which calls a function in the dll and sets up the hook. All of this works fine and dandy, the dll successfully sets up the hook (the hook value is greater than 0), but the only problem is that I do not know how to communicate each message intercepted by the dll to the exe. I tried passing in a callback to the dll's setup function, but since the dll is copied into a different address space that value is not carried over so that will not work, and I really do not know how I would go about communicating with the exe. I have just about 0 experience in dealing with dlls and am only working with them because I am forced to because of the nature of the hook, so if anyone can suggest an alternate way of going about what I am trying to do, that would be appreciated also.

Share this post


Link to post
Share on other sites
I remember in windows 9x i was able to send WM to another application (the value was > WM_USER)

I think that broke on XP. Look up interprocess communication. You could do this with sockets, or named pipes. But i think you want copy data
http://msdn.microsoft.com/en-us/library/aa365574(VS.85).aspx#base.using_com_for_ipc
http://msdn.microsoft.com/en-us/library/ms648710(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms649009(VS.85).aspx

Share this post


Link to post
Share on other sites
Ok I have one more question, in my hook proc, I call CallNextHookEx, but I need the value of the hook. How am I supposed to get that value, or can I just return 0 from my function instead?

Share this post


Link to post
Share on other sites
Here are a few free utilities that you might find helpful.

Code Project: MessageSender - For sending messages to target windows. Useful in testing the hook.

WinSpy++ 1.7 - For help in identifying target windows.

Dependency Walker - For examining dlls to see which functions they export. Use this to check your dll to determine if the names of your export functions match your expectations. They might be mangled.

Process Explorer v11.21 - For determining if your dll has been loaded in the target process.



GlobalMessageHook - C source code for exe and dll that I wrote several years ago. It demonstrates a global hook by setting a system wide WH_GETMESSAGE hook to capture WM_PAINT messages. Move your mouse cursor around and click on various windows to generate messages and see them fill up a list box. The code is written for lcc-win32 so it doesn't use the data_seg compiler pragma that code written for MSVC uses. It uses memory mapping instead.

Share this post


Link to post
Share on other sites
Thanks for the links, I'll check some of them out.

But as to the function returning the hook value, yes it does, but the problem is (if I understand correctly) that value is in the address space of the exe, but when the hook is called the dll is in the address space of the application I am hooking, and so it can't really access the hook variable.

Share this post


Link to post
Share on other sites
Quote:
Original post by Salvinger
Thanks for the links, I'll check some of them out.

But as to the function returning the hook value, yes it does, but the problem is (if I understand correctly) that value is in the address space of the exe, but when the hook is called the dll is in the address space of the application I am hooking, and so it can't really access the hook variable.
You need a shared data segment, which you can construct as indicated in How To Share Data Between Different Mappings of a DLL.

As for communicating information back to the main program from the DLL, I'd suggest using Mailslots since you potentially have multiple producers (hooked applications) and a single consumer (the hooking application).

Share this post


Link to post
Share on other sites

This topic is 3376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this