1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| Option Explicit
' MessageBox hook by Montor
Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowTextW Lib "user32" (ByVal Hwnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowTextLengthW Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function SetWindowsHookExW Lib "user32" (ByVal idHook As Long, ByVal lpfn As Any, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As Long) As Boolean
Private Declare Function CallNextHookEx Lib "user32" (ByVal hhk As Long, ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetClassNameW Lib "user32" (ByVal Hwnd As Long, ByVal lpString As Long, ByVal nMaxCount As Long) As Long
Private Declare Function EndDialog Lib "user32" (ByVal hDlg As Long, ByVal nResult As Long) As Long
Private Const HCBT_ACTIVATE = 5
Private Const WH_CBT = 5
Private Const DLG_CLASS = "#32770"
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const MAX_PATH = 266
Private fHandle As Long
Private Hook As Long
Enum IDSExitCode
ID_OK = 1
ID_CANCEL = 2
ID_ABORT = 3
ID_RETRY = 4
ID_IGNORE = 5
ID_YES = 6
ID_NO = 7
ID_CLOSE = 8
ID_HELP = 9
ID_TRYAGAIN = 10
ID_CONTINUE = 11
End Enum
Private Function WinClass(ByVal Hwnd As Long) As String
Dim Tmp As String
Tmp = Space(MAX_PATH)
WinClass = Left(Tmp, GetClassNameW(Hwnd, StrPtr(Tmp), MAX_PATH))
End Function
Function BWindow(ByVal H As Long) As Long
H = GetWindow(H, GW_CHILD)
Do
BWindow = H
H = GetWindow(H, GW_HWNDNEXT)
Loop Until H = 0
End Function
Private Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim Txt As String
Dim LHandle As Long
Dim L As Long
HookProc = CallNextHookEx(Hook, nCode, wParam, lParam)
On Error GoTo ExitLab
If (nCode = HCBT_ACTIVATE) Then
If (WinClass(wParam) = DLG_CLASS) Then
StopHook
fHandle = wParam
LHandle = BWindow(wParam)
L = GetWindowTextLengthW(LHandle)
Txt = Space(L)
GetWindowTextW LHandle, StrPtr(Txt), L + 1
HookCallBack Txt
fHandle = 0
End If
End If
ExitLab:
End Function
Private Sub DlgExitCode(ByVal AExit As IDSExitCode)
If (fHandle <> 0) Then
EndDialog fHandle, AExit
fHandle = 0
End If
End Sub
Sub StartHook()
If Hook <> 0 Then Exit Sub
fHandle = 0
Hook = SetWindowsHookExW(WH_CBT, AddressOf HookProc, Application.Hinstance, 0)
End Sub
Sub StopHook()
If Hook <> 0 Then
UnhookWindowsHookEx Hook
Hook = 0
fHandle = 0
End If
End Sub
Private Sub HookCallBack(ByVal MessageText As String)
MsgBox "Message :" & vbCr & "[" & MessageText & "]"
'placer le code interception ici
DlgExitCode ID_NO 'simule click No
End Sub |
Partager