Vote count:
0
I tried to create a LowLevelKeyHook, according to this example: Keystrokes handled: Understanding IntPtr return value and http://ift.tt/1lA9mLp
My code is really straightforward at the moment:
public class KeyHookHelper
{
private IntPtr _hookId = IntPtr.Zero;
private readonly KeyboardHookProc _hookedProc;
private readonly HookingObjectsList _hookingObjects;
private IntPtr _lParam;
private IntPtr _saveInstance;
public KeyHookHelper()
{
_hookingObjects = new HookingObjectsList();
_hookedProc = HookProc;
Hook();
}
public void AddHookedKey(HookingObject obj)
{
_hookingObjects.Add(obj);
}
public void Hook()
{
_saveInstance = DllImports.LoadLibrary("User32");
_hookId = DllImports.SetWindowsHookEx(CommandConstants.WH_KEYBOARD, _hookedProc, _saveInstance, 0);
}
~KeyHookHelper()
{
UnHook();
}
public void UnHook()
{
DllImports.UnhookWindowsHookEx(_hookId);
}
private int HookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
_lParam = lParam;
int vkCode = Marshal.ReadInt32(_lParam);
var key = (Keys)vkCode;
}
return (int)DllImports.CallNextHookEx(_hookId, nCode, wParam, lParam);
}
}
But as soon as Marshal.ReadInt32 is called, I get a
A first chance exception of type 'System.AccessViolationException' occurred in mscorlib.dll
The only difference from the code I've seen so far is the fact, that I'm calling the method from an excel addin, while the example is calling it from a Winforms Window.
I tried several stuff like passing the ThreadID etc., but nothing seems to work.
asked 1 min ago
LowLevelKeyHook :AccessViolationException
Aucun commentaire:
Enregistrer un commentaire