Implementing windows hooks using NativeWindow properly

923 views Asked by At

I dont have much of a C++ background but have successfully hooked a window and converted its msgs into raised events that my application can consume, Ive started by inheriting from NativeWindow and overriding WndProc and have determined the msgs that im interested in, WM_VSCROLL and WM_HSCROLL for instance.

Firstly are there any full implementations out there that raise all the usual events, like keypress,keydown,keyup,mousemove,mousedown,vscroll,hscroll,vresize, hresize of the window. Im interested in making sure that ive implemented the class correctly.

Secondly how do I properly throttle the events produced by my NativeWindow, as to limit the chattiness of the implementation.

2

There are 2 answers

2
Hans Passant On BEST ANSWER

I assume you are talking about hooking a window in another application. That's a non-trivial problem, the wparam and lparam arguments may contain pointers instead of simple values. Those pointers are however only valid in the virtual memory space of the process who's window you hooked. Ignoring this will buy you an AccessViolation exception.

You have to P/Invoke ReadProcessMemory() to read the pointed-to structure. That needs to be done for each individual message, you can't count on a generic implementation. That can get quite hairy when you hook a non-trivial window like a ListView or TreeView.

1
Ben Voigt On

Most programs that do this use DLL injection to handle the events from inside the process that owns the window. Of course, you mustn't inject managed code into another process, only native code that is very careful not to mess up the application state.

What are you trying to accomplish? Hooking other applications' windows' should be the last resort.