Hooking Overview

Category: Hooking

Date: 02-16-2022

Return to Index


 
 
'A hook is a point in the Microsoft� Windows� message-handling mechanism
'where an application can install a subroutine to monitor the message
'traffic in the system and process certain types of messages before
'they reach the target window procedure.
 
'Hooks tend to slow down the system because they increase the amount of
'processing the system must perform for each message. You should install
'a hook only when necessary, and remove it as soon as possible.
 
'Global hooks must be in a separate DLL. Thread-specific hooks do not.
 
'Hook Types:
'Each type of hook enables an application to monitor a different aspect
'of the system's message-handling mechanism.
 
   'Most used:
   WH_CBT  (structres: CBT_CreateWnd, CBTActiveStruct)
   'system calls a WH_CBT hook procedure before activating, creating, destroying,
   'minimizing, maximizing, moving, or sizing a window; before completing a system
   'command;
   WH_KEYBOARD   (structure: KBDLLHookStruct)
   'monitor message traffic for WM_KEYDOWN and WM_KEYUP messages
   WH_MOUSE    (structures: MouseHookStruct, MouseHookStructEX
   'monitor mouse messages
 
    'Others
    WH_CALLWNDPROC and WH_CALLWNDPROCRET
    WH_DEBUG
    WH_FOREGROUNDIDLE
    WH_GETMESSAGE
    WH_JOURNALPLAYBACK
    WH_JOURNALRECORD
    WH_KEYBOARD_LL
    WH_MOUSE_LL
    WH_MSGFILTER and WH_SYSMSGFILTER
    WH_SHELL
 
'Hook Procedures:
SetWindowsHookEx  - install hook at beginning of a hook chain
CallNextHookEx    - pass event to the next procedure
 
'Note that the hook procedures for some types of hooks can only monitor
'messages. the system passes messages to each hook procedure, regardless
'of whether a particular procedure calls CallNextHookEx.
 
'Install a hook:
ghHook = SetWindowsHookEx(%WH_CBT, CodePtr(InputBoxProc), GetModuleHandle(""), GetCurrentThreadId)
'Remove a hook:
UnhookWindowsHookEx ghHook
 
Function HookProc(nCode,wParam,lParam)
   'nCode
   Function = CallNextHookEx(ByVal ghHook, ByVal nCode, ByVal wParam, ByVal lParam)
End Function
 
 
Function InputBoxProc(ByVal nCode As LongByVal wParam As LongByVal lParam As LongAs Long
   'do stuff here ...
   Function = CallNextHookEx(ByVal ghHook, ByVal nCode, ByVal wParam, ByVal lParam)
End Function
 
'Structures
TYPE CBT_CREATEWND
    lpcs AS CREATESTRUCT PTR
    hWndInsertAfter AS DWord
END TYPE
 
TYPE CREATESTRUCT
  lpCreateParams As Long
  hInstance AS DWord
  hMenu AS DWord
  hwndParent AS DWord
  cy As Long
  cx As Long
  y As Long
  x As Long
  STYLE As Long
  lpszName As AsciiZ PTR
  lpszClass As AsciiZ PTR
  dwExStyle AS DWord
END TYPE
 
TYPE KBDLLHOOKSTRUCT
    vkCode AS DWord
    scanCode AS DWord
    flags AS DWord
    time AS DWord
    dwExtraInfo AS DWord
END TYPE
 
TYPE MOUSEHOOKSTRUCT
    pt AS POINTAPI
    hWnd AS DWord
    wHitTestCode AS DWord
    dwExtraInfo AS DWord
END TYPE
 
TYPE MOUSEHOOKSTRUCTEX
    pt AS POINTAPI
    hWnd AS DWord
    wHitTestCode AS DWord
    dwExtraInfo AS DWord
    mouseData AS DWord
END TYPE
 
 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
 
%IDC_Ask = 500
Global ix, iy As Long, hDlg As Dword, ghHook As Dword
 
Function PBMain() As Long
   ix = 300 : iy = 300
   Dialog New Pixels, 0, "Toolbar Test",ix,iy,400,300, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, %IDC_Ask, "Ask", 10,10,50,25
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local temp$
   Select Case Cb.Msg
      Case %WM_Command
         Select Case Cb.Ctl
            Case %IDC_Ask
               ghHook = SetWindowsHookEx(%WH_CBT, CodePtr(InputBoxProc), GetModuleHandle(""), GetCurrentThreadId)
               temp$ = InputBox$("Enter your name:", "InputBox Test", "Gary Beene",ix+50,iy+50)
         End Select
   End Select
End Function
 
Function InputBoxProc(ByVal nCode As LongByVal wParam As LongByVal lParam As LongAs Long
   Local szTemp As Asciiz * %Max_Path, cw As CBT_CREATEWND Ptr, cst As CREATESTRUCT Ptr
   Function = CallNextHookEx(ByVal ghHook, ByVal nCode, ByVal wParam, ByVal lParam)
   If nCode < 0 Then Exit Function
   If nCode = %HCBT_ACTIVATE Then UnhookWindowsHookEx ghHook
   If nCode = %HCBT_CREATEWND Then
      cw = lParam         ' Get pointer to CBT_CREATEWND struct so we can...    TT: Nick Melnick
      cst = @cw.lpcs      ' get a pointer to the CREATESTRUCT struct
      GetClassName wParam, szTemp, %Max_Path      ' for each window / control as it is created
      If UCase$(szTemp) = "STATICThen @cst.cy = 20                           'prompt
      If UCase$(szTemp) = "EDIT"   Then @cst.cx = 190 : @cst.y = @cst.y - 130  'textbox
      If UCase$(szTemp) = "#32770Then @cst.cy = @cst.cy - 130                'dialog
   End If
End Function
 
'gbs_00830
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm