Hi, got a problem getting my hooking procedure to work and I'd be grateful for some pointers. Basically I want to send a custom message to another window, but currently, the hooking procedure (which is initializing and terminating fine) is not picking up any message(s) from the child window - The code is below:
- If I run the above, even when I move the mouse nothing happens - and frankly - I'm stumped. The lParam and wParam seem to contain stuff, but uMsg is always 0.
Any help greatly appericated - my first forray into Win32APi is meeting some resistance
Thanks
Yum
Code:
'Module
Option Explicit
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
ByVal myProcAddy As Long, _
ByVal WindowInstance As Long, _
ByVal dwThreadId As Long) As Long
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, _
ByVal Message As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" (ByVal hWnd As Long, _
ByVal Message As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
'Public Declare Function RegisterWindowMessage Lib "user32.dll" Alias "RegisterWindowMessageA" ( _
' ByVal lpString As String) As Long
Public Const WH_CALLWNDPROCRET = 12
Private Const GWL_HINSTANCE = (-6)
Const WM_MOUSEMOVE As Long = &H200
Public Const WH_CBT = 5
Public Const HWND_BROADCAST = 65535
Public Const WH_GETMESSAGE As Long = 3
Public Const WH_SYSMSGFILTER = 6
Public Const WH_FOREGROUNDIDLE = 11
Public ghookId As Long
Public Const CUSTOMMSG As Long = &H400
Public gCallingHwnd As Long
Public Function MyProc(ByVal uMsg As Long, _
ByVal lParam As Long, _
ByVal wParam As Long) As Long
'Debug.Print uMsg & " - " & wParam & " - " & lParam
If uMsg = WM_MOUSEMOVE Then
Debug.Print "MouseMove Detected"
End If
If uMsg = CUSTOMMSG Then
Debug.Print "Custom Message Detected"
Call UnHookMe
End If
MyProc = False
End Function
Public Function HookMe(ByVal hWnd As Long) As Boolean
Dim hInstance As Long
Dim lThreadID As Long
lThreadID = GetCurrentThreadId()
hInstance = GetWindowLong(hWnd, GWL_HINSTANCE)
'ghookId = SetWindowsHookEx(WH_CALLWNDPROCRET, AddressOf MyProc, hInstance, lThreadID)
ghookId = SetWindowsHookEx(WH_GETMESSAGE, AddressOf MyProc, hInstance, lThreadID)
'Temporary until can figure better way to get Hwnd
gCallingHwnd = hWnd
'Call UnHookMe
HookMe = True
End Function
Public Function UnHookMe() As Boolean
Dim myReturn As Long
myReturn = UnhookWindowsHookEx(ghookId)
'Temporary
ghookId = 0
UnHookMe = True
End Function
'Form frmParent
Private Sub cmdOpenChild_Click()
HookMe (Me.hWnd)
'Show my child
frmChild.Show 1
End Sub
Private Sub Form_Terminate()
'Temporary - Unload hook if not already done.
If ghookId <> 0 Then
Call UnHookMe
End If
End Sub
'Form frmChild
Private Sub cmdSendMessage_Click()
Dim myReturn As Boolean
myReturn = PostMessage(frmParent.hWnd, CUSTOMMSG, 666&, 666&)
'MsgBox myReturn
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Dim myReturn As Long
'myReturn = SendMessage(gCallingHwnd, WM_CUSTOMMSG, 0&, 0&)
End Sub
- If I run the above, even when I move the mouse nothing happens - and frankly - I'm stumped. The lParam and wParam seem to contain stuff, but uMsg is always 0.
Any help greatly appericated - my first forray into Win32APi is meeting some resistance
Thanks
Yum