Guest User

Untitled

a guest
Jul 29th, 2012
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2. Public Delegate Function CallBack( _
  3. ByVal nCode As Integer, _
  4. ByVal wParam As IntPtr, _
  5. ByVal lParam As IntPtr) As Integer
  6.  
  7. Public Class Form1
  8.  
  9. 'For other hook types, obtain these values from Winuser.h in Microsoft SDK.
  10. Dim WH_JOURNALRECORD As Integer = 0
  11. Dim WH_JOURNALPLAYBACK As Integer = 1
  12. Dim WH_KEYBOARD As Integer = 2
  13. Dim WH_GETMESSAGE As Integer = 3
  14. Dim WH_CALLWNDPROC As Integer = 4
  15. Dim WH_CBT As Integer = 5
  16. Dim WH_SYSMSGFILTER As Integer = 6
  17. Dim WH_MOUSE As Integer = 7
  18. Dim WH_HARDWARE As Integer = 8
  19. Dim WH_DEBUG As Integer = 9
  20. Dim WH_SHELL As Integer = 10
  21. Dim WH_FOREGROUNDIDLE As Integer = 11
  22. Dim WH_CALLWNDPROCRET As Integer = 12
  23. Dim WH_KEYBOARD_LL As Integer = 13
  24. Dim WH_MOUSE_LL As Integer = 14
  25. Shared hHook As Integer = 0
  26.  
  27. 'Keep the reference so that the delegate is not garbage collected.
  28. Private hookproc As CallBack
  29.  
  30. 'Import for the SetWindowsHookEx function.
  31. <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
  32. Public Overloads Shared Function SetWindowsHookEx _
  33. (ByVal idHook As Integer, ByVal HookProc As CallBack, _
  34. ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
  35. End Function
  36.  
  37. 'Import for the CallNextHookEx function.
  38. <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
  39. Public Overloads Shared Function CallNextHookEx _
  40. (ByVal idHook As Integer, ByVal nCode As Integer, _
  41. ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
  42. End Function
  43. 'Import for the UnhookWindowsHookEx function.
  44. <DllImport("User32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
  45. Public Overloads Shared Function UnhookWindowsHookEx _
  46. (ByVal idHook As Integer) As Boolean
  47. End Function
  48.  
  49. 'Point structure declaration.
  50. <StructLayout(LayoutKind.Sequential)> Public Structure Point
  51. Public x As Integer
  52. Public y As Integer
  53. End Structure
  54.  
  55. 'MouseHookStruct structure declaration.
  56. <StructLayout(LayoutKind.Sequential)> Public Structure MouseHookStruct
  57. Public pt As Point
  58. Public hwnd As Integer
  59. Public wHitTestCode As Integer
  60. Public dwExtraInfo As Integer
  61. End Structure
  62.  
  63. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  64. Me.RichTextBox1.AppendText("[" & Now & "] START LOGGING" & vbCrLf)
  65. If hHook.Equals(0) Then
  66. hookproc = AddressOf MouseHookProc
  67. hHook = SetWindowsHookEx(WH_MOUSE, _
  68. hookproc, _
  69. IntPtr.Zero, _
  70. AppDomain.GetCurrentThreadId())
  71. If hHook.Equals(0) Then
  72. Me.RichTextBox1.AppendText("SetWindowsHookEx Failed")
  73. Return
  74. End If
  75. Else
  76. Dim ret As Boolean = UnhookWindowsHookEx(hHook)
  77.  
  78. If ret.Equals(False) Then
  79. Me.RichTextBox1.AppendText("UnhookWindowsHookEx Failed")
  80. Return
  81. Else
  82. hHook = 0
  83. End If
  84. End If
  85.  
  86. End Sub
  87.  
  88. Private Function FocusChanged(ByVal oldhwnd As Integer, ByVal oldpid As Integer, ByVal newhwnd As Integer, ByVal newpid As Integer) As Boolean
  89.  
  90. Return True
  91. End Function
  92. Public Shared Function MouseHookProc( _
  93. ByVal nCode As Integer, _
  94. ByVal wParam As IntPtr, _
  95. ByVal lParam As IntPtr) As Integer
  96. Dim MyMouseHookStruct As New MouseHookStruct()
  97.  
  98. If (nCode < 0) Then
  99. Return CallNextHookEx(hHook, nCode, wParam, lParam)
  100. End If
  101.  
  102. MyMouseHookStruct = CType(Marshal.PtrToStructure(lParam, MyMouseHookStruct.GetType()), MouseHookStruct)
  103.  
  104. Dim tempForm As Form
  105. tempForm = Form.ActiveForm()
  106.  
  107. Dim strCaption As String
  108. strCaption = "x = " & MyMouseHookStruct.pt.x & " y = " & MyMouseHookStruct.pt.y
  109.  
  110. tempForm.Text = strCaption
  111. Return CallNextHookEx(hHook, nCode, wParam, lParam)
  112.  
  113. End Function
  114. End Class
Advertisement
Add Comment
Please, Sign In to add comment