?
Solved

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

Posted on 2004-09-20
6
Medium Priority
?
730 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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