Search code examples
c#c++winapinativewindow

Implementing windows hooks using NativeWindow properly


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.


Solution

  • 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.