Hook Control

The Hook Control is used to install an application-defined hook procedure into the hook chain of a process that is running outside the process of the developer’s own program. You would install a hook procedure to monitor the external process for certain types of events and process certain types of messages before they reach the target window procedure.  The hook control supports two types of hooks: 

 

SentMessages - enables you to only monitor messages sent to window procedures. The system calls a WH_CALLWNDPROC hook procedure before passing the message to the receiving window procedure.  These messages cannot be changed or discarded.

 

PostedMessages - enables an application to monitor messages about to be returned by the GetMessage or PeekMessage function. You can use the WH_GETMESSAGE hook to monitor and change mouse and keyboard input and other messages posted to the message queue.  These messages can be changed but not discarded before they reach the receiving window procedure.

 

Properties

  TargethWnd

[Long] – Sets or returns the handle that identifies the window to be hooked.  You can use the methods GetChildHandle and GetTopLevelHandle below to help set this property.  Once a hook has been set, you cannot change the target handle.

  Messages

A collection of messages that the user has selected to monitor by calling the AddMessage method.

 

Methods

  GetChildHandle(Caption, ClassName, TopLevelHandle) [Long]

Returns the handle to a window that is a child of another window, and identified by the following parameters.  Use this routine to retrieve a handle for setting then target handle.

Caption [String]

ClassName [String]

TopLevelHandle [Long]

 

  GetTopLevelHandle(Caption) [Long]

Returns a handle that identifies a top-level window that has a caption that matches the one specified.  Use this routine to get the top-level handle for the above routine.

Caption [String] – The caption of the window whose handle is desired.

 

   SetHook [Boolean]

Sets both hooks into the thread of the window that is specified by TargethWnd.  Returns true if the operation was successful, otherwise returns false.  You must set the target handle before calling this function.

  AddMessage(NewMessage, strMessage)

          Adds a message to the list of messages to be monitored in the external process.

NewMessage [Long] – The numerical value of the message.  See winuser.h for a list of messages and associated values

strMessage [String] – A user defined string to associate with the message, used for display purposes only.

 

  isHooked

[Boolean – read only] – if the hooks have been set, then this function returns true, otherwise it returns false.

 

  RemoveAllHooks [Boolean]

Uninstalls all hooks from the target thread.  Returns true on success, otherwise false.

 

 

Events

SentMessage(uMsg As Long, wParam As Long, lParam As Long)

This routine is called when a sent message notification arrives.

 

PostedMessage(uMsg As Long, wParam As Long, lParam As Long)

This routine is called when a posted message notification arrives.  The data passed to this event can be changed by the user before is is passed on to the hooked process.  To discard a message simply change uMsg to 0.

 

HookError(Number As Long, LocalMsg As String, APIMsg As String)

This event is raised upon error conditions.   The cause of the error can be determined by examining the two description strings.  If the error came from an API call, then the API string will have a message, otherwise it will be empty.

 

UnHook()

This event is triggered when the hook is removed.  This will result when the hooked process shuts down or when the user makes a call to RemoveHook.

 

 

Examples

This example hooks a button control, labeled "Button", which is in a Visual Basic Form titled "TestForm"

 

Const ParentCaption = "TestForm"

Const ChildClassName = "ThunderRT6CommandButton"

Const ChildCaption = "Button"

 

Private Sub cmdHook_Click()

'This routine sets the hook to monitor specified messages

  Dim lhWnd As Long

  With HookControl

 

    'handle to main window

    lhWnd = .GetTopLevelHandle(ParentCaption)

 

    'monitor the following messages

    .AddMessage WM_LBUTTONUP, "WM_LBUTTONUP"

    .AddMessage WM_LBUTTONDOWN, "WM_LBUTTONDOWN"

    .AddMessage WM_SETTEXT, "WM_SETTEXT"

    .AddMessage WM_CHAR, “WM_CHAR”

 

    'handle to the button we want to hook

    .TargethWnd = .GetChildHandle(ChildCaption, ChildClassName, lhWnd)

 

    'Set the hook(s)

    If .SetHook Then

      cmdHook.Enabled = False

      cmdUnHook.Enabled = True

    Else

      cmdUnHook.Enabled = False

      cmdHook.Enabled = True

      MsgBox "Error Installing Hook."

    End If

 

  End With

End Sub

 

 

Public Sub cmdUnHook_Click()

'This routine removes the hook

 

  If HookControl.RemoveAllHooks Then

    cmdUnHook.Enabled = False

    cmdHook.Enabled = True

  Else

    cmdHook.Enabled = False

    cmdUnHook.Enabled = True

    MsgBox "Error Uninstalling Hook."

  End If

End Sub

 

 

'Here is where the posted messages will arrive

'we cannot discard posted messages, but can change them

Private Sub HookControl_PostedMessage( _

ByVal uMsg As Long, _

ByVal wParam As Long, _

ByVal lParam As Long _

)

 

  Dim Msg

  'display the messages as they arrive

  For Each Msg In HookControl.Messages

    If uMsg = Msg.Value Then

        Debug.Print "Posted: " & Msg.Label & "  " & Chr(wParam)

        Exit For

    End If

  Next

  'Here is an example of how to change a message

  ‘Change all keyboard input to ‘X’

  If uMsg = WM_CHAR Then wParam = 88

End Sub