调用SetWindowsHookEx函数里面VS2008调试器总是返回NULL函数、里面、调试器、SetWindowsHookEx

2023-09-07 00:20:55 作者:时光张狂得人喊疼

我们正在努力.Net应用程序,做一个低级别的键盘钩子。当我们调用SetWindowsHookEx函数内部调试器运行的呼叫总是失败。

从编译的可执行文件运行一切时工作正常。如果我们重视PROCESSS中的SetWindowsHookEx函数被调用一切正常了。

我读某处(我不记得),该VS已经做了低级别的键盘钩子,但是这不应该是一个问题,还有就是CallNextHook功能。

有人知道发生了什么事?

编辑:在code是pretty的straigfoward,引发异常的内部调试器,但没有外面

 的Public Sub New()
    m_callback =新NativeMethods.KeyboardHookDelegate(AddressOf KeyboardCallback)
结束小组

公共Sub启动()
    m_handle = NativeMethods.SetWindowsHookEx(NativeMethods.HookType.WH_KEYBOARD_LL,m_callback,Marshal.GetHINSTANCE(Reflection.Assembly.GetExecutingAssembly()。GetModules()(0))。ToInt32,0)
    如果m_handle = 0则
        抛出新的异常()
    结束如果
结束小组
 

解决方案 VS2008中如何给C 的main函数传递参数

一个猜测可能是VS运行可能会出现不同的反射.vshost.exe的过程,但我不能肯定地说。我们使用'Process.GetCurrentProcess()。MainModule`得到模块传递。也许给一个尝试。

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

解决方案

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.