Solved

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

Posted on 2004-09-20
6
716 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

707 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

15 Experts available now in Live!

Get 1:1 Help Now