views:

128

answers:

1

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
+2  A: 

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.

Davy8
Anyway I will use your Process.GetCurrentProcess().MainModule because looks a lot cleaner.
SoMoS
When changing the code I found that I had a PInvoke wrong using Int instead of InPtr. I also start using GetModuleHandle. Both things side by side made the app work inside VS too.
SoMoS