tags:

views:

41

answers:

2

Is it possible to subclass another application's control so that my application could do something before the other application executes it's code and receiving the lParam and wParam? Ex: subclassing notepad's Edit control and when the user types, being able to know what the user typed? would SetWindowSubclass work if I provide the hWnd of Notepad's edit control? And would I receive the lParam and wParam of all its messages?

Thanks

A: 

No, it's not that simple. The warning on the MSDN page for SetWindowSubclass says explicitly:

Warning You cannot use the subclassing helper functions to subclass a window across threads

This article outlines the process for injecting code into another process, but to summarise, you basically need to:

  1. Use CreateRemoteThread and LoadLibrary, or
  2. Set up the AppInit_DLLs registry key appropriately

Once you've got code running in the remote process, you can use SetWindowLongPtr to subclass the window and then use regular IPC techniques (named pipes, etc) to communicate with your process (if needed).

Dean Harding
A: 

Yes, that is easily possible, if your code is running in the application's process. You can do that with a DLL. You would simply use GetWindowLongPtr with GWLP_WNDPROC to get the application's window function, and use SetWindowLongPtr to set your own. In your window function you check for the message that you would like to alter, and call the application's window function with the altered values. In case of any other message, you will have to call it straight. Use CallWindowProc to call the window funcion, because the default windows function is not a pointer.

Another way would be to use the CallWndProc hook function. Call SetWindowsHookEx with WH_CALLWNDPROC to have it installed. Haven't tried this one yet though, but you won't need to have to be in the process's space, IIRC.

Fozi