Question

we're working on a .Net application that does a low level keyboard hook. When we call the SetWindowsHookEx running inside the debugger the call always fail.

When running from the compiled executable everything works fine. If we attach to the processs the the SetWindowsHookEx has been called everything works too.

I've read somewhere (I can not remember) that VS already does a low level keyboard hook but this shouldn't be a problem as there is the CallNextHook function.

Someone knows what's happening?

EDIT: The code is pretty straigfoward, the exception is thrown inside debugger but not outside.

Public Sub New()
    m_callback = New NativeMethods.KeyboardHookDelegate(AddressOf KeyboardCallback)
End Sub

Public Sub Start()
    m_handle = NativeMethods.SetWindowsHookEx(NativeMethods.HookType.WH_KEYBOARD_LL, m_callback, Marshal.GetHINSTANCE(Reflection.Assembly.GetExecutingAssembly().GetModules()(0)).ToInt32, 0)
    If m_handle = 0 Then
        Throw New Exception()
    End If
End Sub
Was it helpful?

Solution

A guess might be that VS is running the .vshost.exe process that might appear differently in reflection, but I can't say for sure. We use 'Process.GetCurrentProcess().MainModule` to get the module to pass in. Maybe give that a try.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top