WH_KEYBOARD Global Hook
-
Hello, http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/ I don't know if any of you are familiar with Dino Esposito article in MSDN magazine on .NET Hooks but I used the class he created named LocalWindowsHook that imports the below API calls protected static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID); and a few other necessary functions from user32.dll to implement a hook. I ended up changing his Install function from public void Install() { m_hhook = SetWindowsHookEx( m_hookType, m_filterFunc, IntPtr.Zero, (int)AppDomain.GetCurrentThreadId()); } to public void Install() { m_hhook = SetWindowsHookEx( m_hookType, m_filterFunc, GetHInstance(), 0); } public static IntPtr GetHInstance() { return Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]); } In an attempt to create a global hook using WH_KEYBOARD. The problem I have is it doesn't work. :| It works globally with WH_KEYBOARD_LL but as soon as I use WH_KEYBOARD it doesn't work at all. This meaning it doesn't capture keyboard strokes in either the windows form or anywhere else. Now Dino's code worked fine for a local hook (aka the form) but I want a global or system hook. Now I have the class he wrote in an external DLL but there must be something else i am missing. If anyone has any suggestions they would be very much appreciated. Thanks very much. Dino Esposito modified project and my test windows form can be found here http://scaninc.ca/hook/globalhook.zip
-
Hello, http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/ I don't know if any of you are familiar with Dino Esposito article in MSDN magazine on .NET Hooks but I used the class he created named LocalWindowsHook that imports the below API calls protected static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID); and a few other necessary functions from user32.dll to implement a hook. I ended up changing his Install function from public void Install() { m_hhook = SetWindowsHookEx( m_hookType, m_filterFunc, IntPtr.Zero, (int)AppDomain.GetCurrentThreadId()); } to public void Install() { m_hhook = SetWindowsHookEx( m_hookType, m_filterFunc, GetHInstance(), 0); } public static IntPtr GetHInstance() { return Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]); } In an attempt to create a global hook using WH_KEYBOARD. The problem I have is it doesn't work. :| It works globally with WH_KEYBOARD_LL but as soon as I use WH_KEYBOARD it doesn't work at all. This meaning it doesn't capture keyboard strokes in either the windows form or anywhere else. Now Dino's code worked fine for a local hook (aka the form) but I want a global or system hook. Now I have the class he wrote in an external DLL but there must be something else i am missing. If anyone has any suggestions they would be very much appreciated. Thanks very much. Dino Esposito modified project and my test windows form can be found here http://scaninc.ca/hook/globalhook.zip
The WH_KEYBOARD_LL hook is called in your app. context so can easily be made in C#. A global WH_KEYBOARD hook however executes in teh context of the app. that is recieving the keyboard message so your code has to be injected into every running process. This is NOT a good idea IMHO. It only seems to work if the m_filterfunc is exported from the DLL in the [EXPORTS] table. '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
-
The WH_KEYBOARD_LL hook is called in your app. context so can easily be made in C#. A global WH_KEYBOARD hook however executes in teh context of the app. that is recieving the keyboard message so your code has to be injected into every running process. This is NOT a good idea IMHO. It only seems to work if the m_filterfunc is exported from the DLL in the [EXPORTS] table. '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
Hmm.. I don't believe this is the case. If a hook is in place globally whether it is WH_MOUSE or WH_KEYBOARD or WH_KEYBOARD_LL the hook functions the exact same for each case. Nothing is injected into any running process, it doesn't work like that. A hook is ultimately a callback function that applications register with a particular system event. It is not injected into every running process as you say. Doing this with C# is no different then any other language as I am just using the windows API. Now I have the code that can capture WH_MOUSE events at the thread or local application level but I need them to be captured at the global or system level. Any suggestions?
-
Hmm.. I don't believe this is the case. If a hook is in place globally whether it is WH_MOUSE or WH_KEYBOARD or WH_KEYBOARD_LL the hook functions the exact same for each case. Nothing is injected into any running process, it doesn't work like that. A hook is ultimately a callback function that applications register with a particular system event. It is not injected into every running process as you say. Doing this with C# is no different then any other language as I am just using the windows API. Now I have the code that can capture WH_MOUSE events at the thread or local application level but I need them to be captured at the global or system level. Any suggestions?
I take it you have read the MSDN article on hooks? It states: ....This is because global hook procedures are called in the process context of every application in the desktop, causing an implicit call to the LoadLibrary function for all of those processes.... If you only want to be aware of mouse or keyboard events system wide use a WH_JOURNALRECORD hook. Otherwise write your hook stuff in a standard DLL with an EXPORTS section. '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd