D
dsheiner
Hi,
I'm trying to implement a keyboard hook that will receive all keyboard
events within my application, and only my application, using C# .NET
3.5 and VS 2008.
I'm using the following code:
private delegate IntPtr KeyboardHookCallback(int nCode, IntPtr wParam,
IntPtr lParam);
private static KeyboardHookCallback _callback = new
KeyboardHookCallback(HookCallback);
private const int WH_KEYBOARD_LL = 13;
private static IntPtr SetHook()
{
return SetWindowsHookEx(WH_KEYBOARD_LL, _callback, IntPtr.Zero,
Thread.CurrentThread.ManagedThreadId);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true,
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr SetWindowsHookEx(int idHook,
KeyboardHookCallback lpfn, IntPtr hMod, int threadId);
SetWindowsHookEx is returning 0, and Marshal.GetLastWin32Error() is
returning 87. I understand this means there's an invalid parameter,
but that's as far as I can get. I've tried using WH_KEYBOARD (2)
instead of WH_KEYBOARD_LL, and I've tried the deprecated
AppDomain.GetCurrentThreadId(), but this doesn't seem to make a
difference. Any idea where I've gone wrong?
Thanks,
Daniel Sheiner
I'm trying to implement a keyboard hook that will receive all keyboard
events within my application, and only my application, using C# .NET
3.5 and VS 2008.
I'm using the following code:
private delegate IntPtr KeyboardHookCallback(int nCode, IntPtr wParam,
IntPtr lParam);
private static KeyboardHookCallback _callback = new
KeyboardHookCallback(HookCallback);
private const int WH_KEYBOARD_LL = 13;
private static IntPtr SetHook()
{
return SetWindowsHookEx(WH_KEYBOARD_LL, _callback, IntPtr.Zero,
Thread.CurrentThread.ManagedThreadId);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true,
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr SetWindowsHookEx(int idHook,
KeyboardHookCallback lpfn, IntPtr hMod, int threadId);
SetWindowsHookEx is returning 0, and Marshal.GetLastWin32Error() is
returning 87. I understand this means there's an invalid parameter,
but that's as far as I can get. I've tried using WH_KEYBOARD (2)
instead of WH_KEYBOARD_LL, and I've tried the deprecated
AppDomain.GetCurrentThreadId(), but this doesn't seem to make a
difference. Any idea where I've gone wrong?
Thanks,
Daniel Sheiner