???? ID: 319524 - ????? ???????: 19 ????? 2010 - ??????: 1.0

???? Visual Basic.NET ??? ??? ??? ???? ?? ??? ???? ????

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
?? ??? ?? Microsoft Visual c# ??????? ?? ???? ?? ?????318804  (http://support.microsoft.com/kb/318804/ ) ?

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

?? ???? ????? ?? ?? ????? ?? ??? ?? ?? ??? ????????? ???? ?? ??? ??????? ?? ?? ???? ??? ??? ???? ?? ??? ???? ????? ?? ???? ?? ??? ??? ?? ?????? ???? ??? ?? ????? ???? ???

??????? ?????? ?? ??????? ?? ???????? ???? ?? ??? ?? hooks ?? ????? ?? ???? ???? ??? ?? ???????? ?? ??? ??? ?? ?????? ?????. ??? ??? ??????? ?? ???? ??????? ????? ?? ??? ?? ??????? ?? ?????? ?? ???? ???

???? ???????

???? ???? ??? ??? ????

??? ?? ??? ??, ?? ???SetWindowsHookExDLL ????? ?? ???????? ?? ?????? ??? ????????? ??????? ??? ?? ????????? ??? ???. ?? ??? ?????? ?? ?? ??? ???????? ??????? ???? ??

???? ???? ??? ??? ???? ?? ??? ?? ???? ????? ?????? ???? ?? ???, ????? ????? ?? ???? ????:
  1. Microsoft Visual Studio ??????? ???? .NET?
  2. ???????????, ?? ??? ????????, ?? ???? ??? ??????????????.
  3. ?????? ?????????????? ????? ???, ????? ????Visual Basic ??????????? ???????????????? ??????? ????? ????, ???Windows ??????????? ???????????????.
  4. ??????????? ???, ??????ThreadSpecificMouseHook? ???????? ??? ??, Form1 ??? ???? ??????? ????? ??.
  5. Form1.vb ????? ?? ?????? ??? ??????? ????? ????
    Imports System.Runtime.InteropServices
    Public Delegate Function CallBack( _
        ByVal nCode As Integer, _
        ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As Integer
  6. ???Form1????, ??????? ????? ????
    'Declare the mouse hook constant.
    'For other hook types, obtain these values from Winuser.h in Microsoft SDK.
        Dim WH_MOUSE As Integer = 7
        Shared hHook As Integer = 0
    
        'Keep the reference so that the delegate is not garbage collected.
        Private hookproc As CallBack
    
        'Import for the SetWindowsHookEx function.
        <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
         Public Overloads Shared Function SetWindowsHookEx _
              (ByVal idHook As Integer, ByVal HookProc As CallBack, _
               ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
        End Function
    
        'Import for the CallNextHookEx function.
        <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
         Public Overloads Shared Function CallNextHookEx _
              (ByVal idHook As Integer, ByVal nCode As Integer, _
               ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
        End Function
        'Import for the UnhookWindowsHookEx function.
        <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
             Public Overloads Shared Function UnhookWindowsHookEx _
                  (ByVal idHook As Integer) As Boolean
        End Function
    
        'Point structure declaration.
        <StructLayout(LayoutKind.Sequential)> Public Structure Point
            Public x As Integer
            Public y As Integer
        End Structure
    
        'MouseHookStruct structure declaration.
        <StructLayout(LayoutKind.Sequential)> Public Structure MouseHookStruct
            Public pt As Point
            Public hwnd As Integer
            Public wHitTestCode As Integer
            Public dwExtraInfo As Integer
        End Structure
  7. ??????? ?? ??? ?? ??? ??????? ???, ??? ????? ??? ???????button1_click??????????
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If hHook.Equals(0) Then
                hookproc = AddressOf MouseHookProc
                hHook = SetWindowsHookEx(WH_MOUSE, _
                                         hookproc, _
                                         IntPtr.Zero, _
    AppDomain.CurrentDomain.GetCurrentThreadId())
                If hHook.Equals(0) Then
                    MsgBox("SetWindowsHookEx Failed")
                    Return
                Else
                    Button1.Text = "UnHook Windows Hook"
                End If
            Else
                Dim ret As Boolean = UnhookWindowsHookEx(hHook)
    
                If ret.Equals(False) Then
                    MsgBox("UnhookWindowsHookEx Failed")
                    Return
                Else
                    hHook = 0
                    Button1.Text = "Set Windows Hook"
                    Me.Text = "Mouse Hook"
                End If
            End If
    
        End Sub
  8. ???Form1????, ?? ??? ????? ??? ???????MouseHookProc????????
        Public Shared Function MouseHookProc( _
        ByVal nCode As Integer, _
        ByVal wParam As IntPtr, _
        ByVal lParam As IntPtr) As Integer
    Dim MyMouseHookStruct As New MouseHookStruct()
    
            Dim ret As Integer
    
            If (nCode < 0) Then
                Return CallNextHookEx(hHook, nCode, wParam, lParam)
            End If
    
            MyMouseHookStruct = CType(Marshal.PtrToStructure(lParam, MyMouseHookStruct.GetType()), MouseHookStruct)
    
            Dim tempForm As Form
            tempForm = Form.ActiveForm()
    
            Dim strCaption As String
            strCaption = "x = " & MyMouseHookStruct.pt.x & " y = " & MyMouseHookStruct.pt.y
    
            tempForm.Text = strCaption
            Return CallNextHookEx(hHook, nCode, wParam, lParam)
    
        End Function
  9. ????????? ?? ????? ?? ??? F5 ?? ?????? ??? ??? ???? ?? ??? ??????? ?? ??? ????? ????? ???? ???????????? ?? ????? ??????? ?????? ????? ?? ??????? ?? ???? ?? ???? ???
  10. ?? ??????? ?? ??? ??? ????? ???? ????

??????? hooks .NET Framework ??? ??????? ???? ??

.NET Framework ??? ??????? hooks ??????????? ???? ???? ?? ???? ??? ????? ???? hooks ???? ?? ????????? ??? ?????? ?? hooks ?? ???? ?? ???????? ?? ?? ???? ??????????? ??? ??? injected ??? ?? ???????? ??? ??? ???? ???? ?? ???? ??, ????? ?? ????? ???? ?? ??? ???? ???? ?? ???? ??? ???????? ????? ????? ??? dispatched ?? ???? ???? ??? ?? ??? ????? ???? ??? ?? ????????? ???? ?????, ??????? ??????????? ?? ?????? ?? ??? ???? hooks ????????? ???????????? ?? ???? ???? Windows ?????? ????????? ???? ?? ??? ???? ???? ???? ?? ?? ???? ?? ??? ??????? ??? ???????? ??? ?? ??? ??? ??? ?????? collector ???? ????????? ???? ?? ????, ??????? ?? ?? ??? ????????? ????? ?????? ?????? ???????? ?????? ???? ?? ???? ?? ??? ??? ??????.

??????

Hooks ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ?????? ??????? (MSDN) ??? ???? ?? ????:
http://MSDN.Microsoft.com/en-us/library/ms644959.aspx (http://msdn.microsoft.com/en-us/library/ms644959.aspx)

???? ???? ???? ??:
  • Microsoft .NET Framework 2.0
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbhowtomaster kbmt KB319524 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:319524  (http://support.microsoft.com/kb/319524/en-us/ )