Link to home
Start Free TrialLog in
Avatar of dbrckovi
dbrckoviFlag for Croatia

asked on

Trapping messages sent to another application (SetWindowLong, CallWindowProc ...)

Hi!

I have been using the following code to trap messages sent to my own window, and it worked fine, but now I'd like to trap messages sent to another application.
Is it possible?

This is my attempt to look and hook an Internet Explorer window. It finds it and hooks it, but messages are not arriving. (WindowProc function is never fired)

Do you have any suggestion?
Why doesn't this work?

on a form create a timer:
form's code:
'------------------------------------------
Dim handle As Long                          'used to store the handle of internet explorer window

Private Sub Form_Load()
    Timer1.Interval = 100
    Timer1.Enabled = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    WindowHook False, handle
End Sub

Private Sub Timer1_Timer()
    Dim Text As String
    Dim TextLength As Integer
       
    handle = GetForegroundWindow()
    TextLength = GetWindowTextLength(handle)
    Text = Space(TextLength)
    GetWindowText handle, Text, TextLength + 1                              'get the caption of the top most window
   
    Cls
    If InStr(1, Text, "Microsoft Internet Explorer", vbTextCompare) > 0 Then            'if the caption contains 'Microsoft Internet Explorer', then
        Print "Internet Explorer active"
        WindowHook True, handle                                                                           'hook it
        Timer1.Enabled = False                                                                              'stop looking any further
    Else
        Print "Internet Explorer not active"
    End If
End Sub
'--------------------------------------------------------------

 - code in module:
'--------------------------------------------------------------
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Const GWL_WNDPROC = (-4)
Private Const WM_MOVE = &H3

Private lPrevProc As Long

Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    WindowProc = CallWindowProc(lPrevProc, hwnd, uMsg, wParam, lParam)
    Form1.Print uMsg
End Function

Public Sub WindowHook(ByVal bHook As Boolean, handle As Long)
    If bHook = True Then
        lPrevProc = SetWindowLong(handle, GWL_WNDPROC, AddressOf WindowProc)
    Else
        Call SetWindowLong(handle, GWL_WNDPROC, lPrevProc)
    End If
End Sub
'--------------------------------------------------------------------------


Note that if you change the         WindowHook True, handle           TO      WindowHook True, Me.hwnd
                                 and         WindowHook False, handle           TO      WindowHook False, Me.hwnd

it will start trapping messages sent to my app's window. Why doesn't this work for IE's window?

Thanks. I hope I was clear enough.
Avatar of fds_fatboy
fds_fatboy

Avatar of dbrckovi

ASKER

OK.

Is there any other method then?

I can see that Spy++ is able to trap messages from any window of any application. Including controls.
As long as the "object" has hwnd, it can trap messages sent to it.

So, this means it is possible. The question is: is it possible to do it in VB?
ASKER CERTIFIED SOLUTION
Avatar of fds_fatboy
fds_fatboy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.

>> Can't do more at the moment because the people that pay me want me to woork for *them* for a change.

I didn't ask you to write the code for me. I only asked you to point me in the right direction, which you did. And I thank you for that.

I wasn't familliar with SetWindowsHookEx and UnhookWindowsHookEx API calls. I'll examine them, and give you the points if this is indeed what I need.
Sorry, perhaps I should have ;-) after my last comment.

Just a word of warning: using SetWindowsHookEx will cause a GPF when running inside the VB6 debugger. You must compile it and run it.
Sorry for missunderstanding your comment. No hard feelings. :-)
I allways use coution when asking questions becouse some people might occuse me for asking them to do my homework.

I've found few articles about SetWindowsHookEx and UnhookWindowsHookEx functions, and it looks like exactly what I need.

Thanks again.