Results 1 to 40 of 109

Thread: Subclass External Programs done for you

Threaded View

  1. #1

    Thread Starter
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Subclass External Programs done for you

    This code uses a C++ dll and an ActiveX VB control to hook external programs.
    Note: The source is included for both in case you want to change it or just learn from it.

    There is another post on this subject that deals with a system wide hook. Here I am posting complete code that allows the user to perform a thread-specific hook on an external program from their VB program and respond to any messages sent to that external program from within the VB Program.

    In this method the hook is set in the C++ code so we don't need to mess with passing procedure addresses back and forth. This code handles two types of hooks, and forwards messages along with their complete data structures back to your VB code. This code also allows the VB user to change messages where permitted. See the next post in this thread for more details on how the code works.

    There are two parts to the code: a DLL written in C++ (don't worry, it's already compiled for you) that is injected into the thread of the target program, and an ActiveX control that handles communication between the DLL and the developer's VB program.

    These components can be used as-is to trap any message sent to external programs, or since I am including the source, can be modified. They handle two types of hooks:

    WH_CALLWNDPROC - For sent messages
    WH_GETMESSAGE - For posted messages, messages can be changed before being sent on to App

    After hooking the target App and setting the messages to monitor, an event will be triggered in the control for each trapped message. The data for each message is supplied to the events in it's original data structure.

    After unzipping the files, register the dll and the ocx component. Instructions for using the ActiveX control are included in an html file 'ReadMe.htm'

    If anyone finds a problem with this code then let me know so I can fix it.

    The following is an example of how to use the Hook Control in a VB App. For another example see Adding Submenus to External Programs.

    Example:
    This example hooks the text area in Notepad

    start a VB project, add the hookcontrol to your tool box and drag it over to your own app.
    Add two command buttons cmdHook and cmdUnHook
    Before running your app, start Notepad.exe.

    VB Code:
    1. Option Explicit
    2.  
    3. Const ParentCaption = "Untitled - Notepad"
    4. Const ChildClassName = "Edit"
    5. Const ChildCaption = ""
    6.  
    7. Private Const WM_LBUTTONDOWN = &H201
    8. Private Const WM_LBUTTONUP = &H202
    9. Private Const WM_SETTEXT = &HC
    10. Private Const WM_CHAR = &H102
    11. Private Const WM_KEYDOWN = &H100
    12. Private Const WM_KEYUP = &H101
    13.  
    14.  
    15. Private Sub cmdHook_Click()
    16. 'This routine sets the hook to monitor specified messages
    17.   Dim lhWnd As Long
    18.   With HookControl
    19.  
    20.     'handle to main window
    21.     lhWnd = .GetTopLevelHandle(ParentCaption)
    22.     While lhWnd = 0
    23.      If MsgBox("Open Notepad", vbOKCancel) = vbCancel Then Exit Sub
    24.      lhWnd = .GetTopLevelHandle(ParentCaption)
    25.     Wend
    26.    
    27.  
    28.     'monitor the following message(s)
    29.     .AddMessage WM_CHAR, "WM_CHAR"
    30.    
    31.     'handle to the textbox we want to hook
    32.     .TargethWnd = .GetChildHandle(ChildCaption, ChildClassName, lhWnd)
    33.  
    34.     'Set the hooks
    35.     If .SetHook Then
    36.       cmdHook.Enabled = False
    37.       cmdUnHook.Enabled = True
    38.     Else
    39.       cmdUnHook.Enabled = False
    40.       cmdHook.Enabled = True
    41.       MsgBox "Error Installing Hook."
    42.     End If
    43.  
    44.   End With
    45.  
    46. End Sub
    47.  
    48.  
    49. Public Sub cmdUnHook_Click()
    50. 'This routine removes the hook
    51.   If HookControl.RemoveAllHooks Then
    52.     cmdUnHook.Enabled = False
    53.     cmdHook.Enabled = True
    54.   Else
    55.     cmdHook.Enabled = False
    56.     cmdUnHook.Enabled = True
    57.     MsgBox "Error Uninstalling Hook."
    58.   End If
    59.  
    60. End Sub
    61.  
    62.  
    63. 'Here is where the posted messages will arrive
    64. 'we cannot discard posted messages, but can change them
    65. Private Sub HookControl_PostedMessage( _
    66.     uMsg As Long, _
    67.     wParam As Long, _
    68.     lParam As Long _
    69. )
    70.   Dim Msg
    71.  
    72.   'display the messages as they arrive
    73.   For Each Msg In HookControl.Messages
    74.     If uMsg = Msg.Value Then
    75.         Debug.Print "Posted: " & Msg.Label & "  " & wParam
    76.         Exit For
    77.     End If
    78.   Next
    79.  
    80.   'Here is an example of how to change a message
    81.   'Change all a's  to ‘X’
    82.   If uMsg = WM_CHAR And wParam = Asc("a") Then wParam = Asc("X")
    83.  
    84.   'change message to WM_NULL if key is "s" so Notepad ignores it
    85.   If uMsg = WM_CHAR And wParam = Asc("s") Then uMsg = WM_NULL
    86.  
    87. End Sub
    88.  
    89.  
    90. 'reset our buttons if notepad is closed and hook is released
    91. Private Sub HookControl_UnHook()
    92.   cmdHook.Enabled = True
    93.   cmdUnHook.Enabled = False
    94. End Sub
    NOTES:
    Fixed problem with multiple instances (2/22/2005)
    Added Thread local storage to dll to help with multiple instances (2/27/2005)
    Fixed problem with decleration in dll that was causing control to crash (3/08/2005)
    Fixed problem that prevented control from being used in an array. (10/12/2005)
    Uploaded the latest dll source code
    Attached Files Attached Files
    Last edited by moeur; Jan 28th, 2007 at 06:27 PM. Reason: Update Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width