views:

125

answers:

2

Hi guys 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.

+1  A: 

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.

Hans Passant
Yes i do want to hook another window, Would calling readProcessMemory for each msg cause a bottleneck? I am a noob in this area any help you could provide would be appresiated.
almog.ori
RPM is fast, a microsecond. Peanuts compared to the cost of the thread switch you've now got. You can only do this in a reasonably fast way by injecting a DLL into the process. You can't do that with C# code. Google SetWindowsHookEx(). This is not noob material btw.
Hans Passant
+1  A: 

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.

Ben Voigt
I want to hook a notepad like application and add additional features based on keyboard and mouse input. so I need to get the keyboard input, mouse input, scroll input, selected text ect. I dont think there is any injection exepting capturing this and raising events to be handled in my app.
almog.ori