窗口服务中的全局挂钩问题(VB.Net) [英] Global Hook problem in Window Service (VB.Net)

查看:83
本文介绍了窗口服务中的全局挂钩问题(VB.Net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我想创建一个服务来检测是否按下了任何键".
但是,如果使用Form进行编译,以下模块将起作用.但是,如果我在服务项目中对其进行编译并调用setHook(),它将无法正常工作.

我已将其配置为以本地系统登录身份运行并启用与桌面交互.

Imports System.IO
Imports System.Runtime.InteropServices

Module Module1

    Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName) As Integer
    Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
    Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
    Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        If Not nCode < 0 Then
            Dim sw As StreamWriter = New StreamWriter("c:\test1.txt", True)
            sw.WriteLine("Test HOOK")
            sw.Close()
            MyKeyboardProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam)
        End If
    End Function

    <MarshalAs(UnmanagedType.FunctionPtr)> Private callback As HOOKPROC

    Public hnexthookproc As Integer = 0
    Public Const PM_KEY_SPACE = &H20
    Public Enum HookType
        WH_KEYBOARD_LL = 13
        WH_MOUSE_LL = 14
    End Enum

    Public Sub UnHook()
        If hnexthookproc <> 0 Then
            UnhookWindowsHookEx(hnexthookproc)
            hnexthookproc = 0
        End If
    End Sub

    Public Function SetHook()
        callback = New HOOKPROC(AddressOf MyKeyboardProc)
        If hnexthookproc <> 0 Then
            Exit Function
        End If

        hnexthookproc = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE(Reflection.Assembly.GetExecutingAssembly().GetModules()(0)), 0)
        Dim sw As StreamWriter = New StreamWriter("c:\test.txt", True)
        sw.WriteLine(hnexthookproc)
        sw.Close()

    End Function

End Module



我是VB.Net的新手.但是我真的很想知道我犯的错误.

谢谢:-O

解决方案

我也有同样的问题.
该钩子在论坛中的运行情况很好,但是当我将其移到该服务下工作时,我的回调函数将永远不会被调用!
对这个问题有任何更新吗?


抱歉,我仍然没有任何解决方案. http://support.microsoft.com/kb/318804).但是,有一些方法可以解决此问题.看一下以下文章:

http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx
http://www.codeproject.com/KB/cs/globalhook.aspx
http://www.codeproject.com/KB/cs/netwin32hooks.aspx

所有这三篇文章都是C#,但是可以耐心快速,轻松地移植到VB.NET.


Actually, I want to create a service to detect if "any key" is pressed.
However, the following module works if I compile it with a Form. But it does not work if I compile it and called setHook() in a service project.

I have configured it as running with logon as Local System and Enable to interact with desktop.

Imports System.IO
Imports System.Runtime.InteropServices

Module Module1

    Declare Function GetModuleHandle Lib "kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName) As Integer
    Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
    Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
    Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        If Not nCode < 0 Then
            Dim sw As StreamWriter = New StreamWriter("c:\test1.txt", True)
            sw.WriteLine("Test HOOK")
            sw.Close()
            MyKeyboardProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam)
        End If
    End Function

    <MarshalAs(UnmanagedType.FunctionPtr)> Private callback As HOOKPROC

    Public hnexthookproc As Integer = 0
    Public Const PM_KEY_SPACE = &H20
    Public Enum HookType
        WH_KEYBOARD_LL = 13
        WH_MOUSE_LL = 14
    End Enum

    Public Sub UnHook()
        If hnexthookproc <> 0 Then
            UnhookWindowsHookEx(hnexthookproc)
            hnexthookproc = 0
        End If
    End Sub

    Public Function SetHook()
        callback = New HOOKPROC(AddressOf MyKeyboardProc)
        If hnexthookproc <> 0 Then
            Exit Function
        End If

        hnexthookproc = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, callback, Marshal.GetHINSTANCE(Reflection.Assembly.GetExecutingAssembly().GetModules()(0)), 0)
        Dim sw As StreamWriter = New StreamWriter("c:\test.txt", True)
        sw.WriteLine(hnexthookproc)
        sw.Close()

    End Function

End Module



I am a newcomer of VB.Net. But I really wanted to know the mistakes I made.

Thank you :-O

解决方案

I have the same problem.
The hook works great while in the forum, but when i moved it to work under the service, my callback is never called!
any updates regarding this issue?


Sorry man, I still got no solution at all.


Global hooks are not supported in the .NET Framework (http://support.microsoft.com/kb/318804). However, there are ways to work around this. Take a look at the following articles:

http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx
http://www.codeproject.com/KB/cs/globalhook.aspx
http://www.codeproject.com/KB/cs/netwin32hooks.aspx

All three articles are C#, but can be quickly and easily ported to VB.NET with a bit of patience.


这篇关于窗口服务中的全局挂钩问题(VB.Net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆