Solved

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

Posted on 2004-09-20
6
718 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:dbrckovi
  • 3
  • 3
6 Comments
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12100514
0
 
LVL 11

Author Comment

by:dbrckovi
ID: 12100815
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?
0
 
LVL 10

Accepted Solution

by:
fds_fatboy earned 400 total points
ID: 12100977
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Author Comment

by:dbrckovi
ID: 12101229
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.
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12101352
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.
0
 
LVL 11

Author Comment

by:dbrckovi
ID: 12101597
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now