• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

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


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
    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
        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)
        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.
  • 3
  • 3
1 Solution
dbrckoviAuthor Commented:

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?
SetWindowsHookEx and UnhookWindowsHookEx API calls are probably what you require.

Can't do more at the moment because the people that pay me want me to woork for *them* for a change.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

dbrckoviAuthor Commented:

>> 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.
dbrckoviAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now