Hi,
I'm building a program that at some point needs to hook the keyboard input to prevent the user from pressing some key combinations. Then, at a later point I need to unhook the keyboard, and then, I need to hook again the keyboard.
I succeed in hooking the keyboard for the first time and then unhhok the keyboard, but when I try to hook it again using the same code, it seems that the hooking-method is not called at all, although 'SetWindowsHook Ex' method succeeds.
Here is how I do it:
First, declarations:
[DllImport("user 32", EntryPoint = "SetWindowsHook ExA", CharSet = CharSet.Ansi, CallingConventi on = CallingConventi on.StdCall, SetLastError = true, ExactSpelling = true)]
private static extern int SetWindowsHookE x(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
[DllImport("user 32", EntryPoint = "UnhookWindowsH ookEx", CharSet = CharSet.Ansi, CallingConventi on = CallingConventi on.StdCall, SetLastError = true, ExactSpelling = true)]
private static extern int UnhookWindowsHo okEx(int hHook);
[DllImport("user 32", EntryPoint = "CallNextHookEx ", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int CallNextHookEx( int hHook, int nCode, int wParam, IntPtr lParam);
private static int LowLevelKeyboar dProc(int nCode, int wParam, IntPtr lParam)
{
...
return CallNextHookEx( intLLKeyboard, nCode, wParam, lParam);
}
private delegate int HookProc(int nCode, int wParam, IntPtr lParam);
private static HookProc KeyboardDelegat e = null; //Static member
private static int intLLKeyboard = 0; //Static member
Then, setting the hook:
KeyboardDelegat e = new HookProc(LowLev elKeyboardProc) ;
System.Reflecti on.Module[] modules = System.Reflecti on.Assembly.Get ExecutingAssemb ly().GetModules ();
intLLKeyboard = SetWindowsHookE x(WH_KEYBOARD_L L, KeyboardDelegat e, System.Runtime. InteropServices .Marshal.GetHIN STANCE(modules[0]), 0);
Later, unhooking :
UnhookWindowsHo okEx(intLLKeybo ard);
Then, trying to hook again :
KeyboardDelegat e = new HookProc(LowLev elKeyboardProc) ;
System.Reflecti on.Module[] modules = System.Reflecti on.Assembly.Get ExecutingAssemb ly().GetModules ();
intLLKeyboard = SetWindowsHookE x(WH_KEYBOARD_L L, KeyboardDelegat e, System.Runtime. InteropServices .Marshal.GetHIN STANCE(modules[0]), 0);
At this point 'intLLKeyboard' is not null, but nevertheless, it seems that the hooking procedure isn't called at all.
I'm building a program that at some point needs to hook the keyboard input to prevent the user from pressing some key combinations. Then, at a later point I need to unhook the keyboard, and then, I need to hook again the keyboard.
I succeed in hooking the keyboard for the first time and then unhhok the keyboard, but when I try to hook it again using the same code, it seems that the hooking-method is not called at all, although 'SetWindowsHook Ex' method succeeds.
Here is how I do it:
First, declarations:
[DllImport("user 32", EntryPoint = "SetWindowsHook ExA", CharSet = CharSet.Ansi, CallingConventi on = CallingConventi on.StdCall, SetLastError = true, ExactSpelling = true)]
private static extern int SetWindowsHookE x(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
[DllImport("user 32", EntryPoint = "UnhookWindowsH ookEx", CharSet = CharSet.Ansi, CallingConventi on = CallingConventi on.StdCall, SetLastError = true, ExactSpelling = true)]
private static extern int UnhookWindowsHo okEx(int hHook);
[DllImport("user 32", EntryPoint = "CallNextHookEx ", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int CallNextHookEx( int hHook, int nCode, int wParam, IntPtr lParam);
private static int LowLevelKeyboar dProc(int nCode, int wParam, IntPtr lParam)
{
...
return CallNextHookEx( intLLKeyboard, nCode, wParam, lParam);
}
private delegate int HookProc(int nCode, int wParam, IntPtr lParam);
private static HookProc KeyboardDelegat e = null; //Static member
private static int intLLKeyboard = 0; //Static member
Then, setting the hook:
KeyboardDelegat e = new HookProc(LowLev elKeyboardProc) ;
System.Reflecti on.Module[] modules = System.Reflecti on.Assembly.Get ExecutingAssemb ly().GetModules ();
intLLKeyboard = SetWindowsHookE x(WH_KEYBOARD_L L, KeyboardDelegat e, System.Runtime. InteropServices .Marshal.GetHIN STANCE(modules[0]), 0);
Later, unhooking :
UnhookWindowsHo okEx(intLLKeybo ard);
Then, trying to hook again :
KeyboardDelegat e = new HookProc(LowLev elKeyboardProc) ;
System.Reflecti on.Module[] modules = System.Reflecti on.Assembly.Get ExecutingAssemb ly().GetModules ();
intLLKeyboard = SetWindowsHookE x(WH_KEYBOARD_L L, KeyboardDelegat e, System.Runtime. InteropServices .Marshal.GetHIN STANCE(modules[0]), 0);
At this point 'intLLKeyboard' is not null, but nevertheless, it seems that the hooking procedure isn't called at all.