I want to "hook" into a window`s events using the API SetWindowLong (with GWL_WNDPROC index to replace the window procedure) and overloading the WindowProc callback function. There is no problem if the window belongs to the same process as the calling thread, but if the window we want to hook into belongs to another thread the SetWindowLong function fails. I thought that the solution could be related to the use of AttachThreadInput API, but although the return value of this function is OK, the SetWindowLong keeps failing. Any idea? The goal is to flash a parent window when its child window becomes active (the problem is that the child window belongs to another process (Active X EXE)
/CODE
lngChildThreadId = GetCurrentThreadId
lngServiceThreadId = GetWindowThreadProcessId(MyServicelngWndHandle, lngServiceProcessId)
lngSuccess = AttachThreadInput(lngServiceThreadId, lngChildThreadId, True)
Call ActivateHook(MyServicelngWndHandle, Me.hwnd)
Public Sub ActivateHook(ByVal hwnd As Long, ByVal ChildWnd As Long)
lngPrevProcAdress = SetWindowLong(hwnd, GWL_WNDPROC, _
AddressOf WindowProc)
End Sub
Public Sub DeActivateHook(ByVal hwnd As Long)
Dim lngTemp as long
lngTemp = SetWindowLong(hwnd, GWL_WNDPROC, lngPrevProcAdress)
End Sub
Function WindowProc(ByVal hw As Long, ByVal uMsg As _
Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim test1 As clsTest
Set test1 = m_colHandles(CStr(hw))
If uMsg = WM_ACTIVATE Then
Call FlashWindow(test1.Child, 1)
If wParam = WA_CLICKACTIVE Then
Call SetForegroundWindow(test1.Child)
End If
End If
WindowProc = CallWindowProc(test1.Handle, hw, _
uMsg, wParam, lParam)
End Function
/END CODE
/CODE
lngChildThreadId = GetCurrentThreadId
lngServiceThreadId = GetWindowThreadProcessId(MyServicelngWndHandle, lngServiceProcessId)
lngSuccess = AttachThreadInput(lngServiceThreadId, lngChildThreadId, True)
Call ActivateHook(MyServicelngWndHandle, Me.hwnd)
Public Sub ActivateHook(ByVal hwnd As Long, ByVal ChildWnd As Long)
lngPrevProcAdress = SetWindowLong(hwnd, GWL_WNDPROC, _
AddressOf WindowProc)
End Sub
Public Sub DeActivateHook(ByVal hwnd As Long)
Dim lngTemp as long
lngTemp = SetWindowLong(hwnd, GWL_WNDPROC, lngPrevProcAdress)
End Sub
Function WindowProc(ByVal hw As Long, ByVal uMsg As _
Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim test1 As clsTest
Set test1 = m_colHandles(CStr(hw))
If uMsg = WM_ACTIVATE Then
Call FlashWindow(test1.Child, 1)
If wParam = WA_CLICKACTIVE Then
Call SetForegroundWindow(test1.Child)
End If
End If
WindowProc = CallWindowProc(test1.Handle, hw, _
uMsg, wParam, lParam)
End Function
/END CODE