Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect other application msgbox & dialogs 1

Status
Not open for further replies.

bradles

Technical User
Sep 15, 2002
110
AU
Thanks for any help on this one...
Trying to find a way to detect the creation or activation of msgbox's and dialog forms created by other applications. If possible I'd prefer to do this without running a timer and checking EnumWindows, somehow raising an event when these windows appear.

So far I've tried hooking the windows shell (RegisterWindowMessage API) which detects others windows great but not MsgBox's.

Any other ideas?

Code:
Option Explicit

Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) 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 Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

Const GWL_WNDPROC = -4

Const HSHELL_WINDOWCREATED = 1
Const HSHELL_WINDOWDESTROYED = 2
Const HSHELL_ACTIVATESHELLWINDOW = 3
Const HSHELL_WINDOWACTIVATED = 4
Const HSHELL_GETMINRECT = 5
Const HSHELL_REDRAW = 6
Const HSHELL_TASKMAN = 7
Const HSHELL_LANGUAGE = 8
Const HSHELL_SYSMENU = 9
Const HSHELL_ENDTASK = 10
Const HSHELL_ACCESSIBILITYSTATE = 11
Const HSHELL_APPCOMMAND = 12
Const HSHELL_WINDOWREPLACED = 13
Const HSHELL_WINDOWREPLACING = 14
Const HSHELL_HIGHBIT = 15
Const HSHELL_FLASH = 16
Const HSHELL_RUDEAPPACTIVATED = 17

Dim myPid As Long, lpWndProc As Long, WM_SHELLHOOK As Long

Public Sub SetupWindowHook(FormHwnd As Long)
    WM_SHELLHOOK = RegisterWindowMessage("SHELLHOOK")
    lpWndProc = SetWindowLong(FormHwnd, GWL_WNDPROC, AddressOf WndProc)
    myPid = GetCurrentProcessId
End Sub

Private Function WndProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Debug.Print hwnd, msg, wParam, lParam
    
    WndProc = CallWindowProc(lpWndProc, hwnd, msg, wParam, lParam)
End Function
 
>which detects others windows great

What - of other applications? Are you quite sure? The code shown here isn't hooking anything; it is simply subclassing one of your forms, nothing more.
 
(you certainly are not seeing WM_SHELLHOOK nor, as a result, any of the HSHELL messages)
 
Thanks strongm, apologies for not replying sooner (Easter with the family which is great).

Firstly here's my form code that I neglected to include in my first post:

Code:
Option Explicit

Private Declare Function RegisterShellHookWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function DeregisterShellHookWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Sub Form_Load()
    'Testing Shell Hooks
    RegisterShellHookWindow hwnd
    Call APIWindowTest.SetupWindowHook(Me.hwnd)
End Sub

Private Sub Form_Unload(Cancel As Integer)
    DeregisterShellHookWindow hwnd
End Sub

So I receive messages for change of focus, window creation and activation for all windows except some msgbox dialogs generated by other applications.

e.g. VBA MsgBox call from outlook works, but Word doesn't!?

My purpose is to position all child windows in the center of its parent in a terminal server environment so i need to catch all windows and do some processing from there.

Testing the GetForegroundWindow APi & a timer works (thanks to Rod Stephens and but it would mean winding the timer down to a small interval to check for the latest topmost window and I'd prefer simply to catch all messages.

Any thoughts?
 
Ah, fair enough. In that case we can move on to the second problem - SHELLHOOK only notifies you about top-level, unowned windows (as per MSDN documentation). Not all message boxes are such, depending on whether the application that creates the message box sets the owner hWnd parameter in the messagebox call.

>so i need to catch all windows

Well, you might have a problem doing that just with VB, but I'd suggest you have a look at the SetWinEventHook API call
 
Thanks strongm, here's a star for helping me on that track and I'll let you know how I go.

That'll teach me for not reading the documentation fully!
 
>That'll teach me for not reading the documentation fully!

The problem is that the RegisterShellHookWindow documentation doesn't actually make this fact clear. The documentation for ShellProc, the hook procedure for WH_SHELL, is more comprehensive as far as the HSHELL_ constants are concerend
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top