Jump to content

Recommended Posts

Posted (edited)

I am using windows hook for detecting new windows instead of having a polling win list but i am finding the code below is not working

#include <GuiConstants.au3>
#include <Misc.au3>

#NoTrayIcon
Opt("GUICloseOnESC", 0)
Opt("GUIOnEventMode", 1)
Opt("WinWaitDelay", 0)

;ShellHook notification codes:
Global Const $HSHELL_WINDOWCREATED = 1;
Global Const $HSHELL_WINDOWACTIVATED = 4;
Global Const $HSHELL_REDRAW = 6;
Global $bHook = 1


;Example1()
Example2()

Func Example1()
   $hGui = GUICreate("")
   GUIRegisterMsg(RegisterWindowMessage("SHELLHOOK"), "HShellWndProc")
   ShellHookWindow($hGui, $bHook)
EndFunc

Func Example2()
   Global $hGui = GUICreate("")
   GUICreate("", @DesktopWidth, @DesktopHeight,0,0, $WS_POPUP)
   GUIRegisterMsg(RegisterWindowMessage("SHELLHOOK"), "HShellWndProc")
   ShellHookWindow($hGui, $bHook)
EndFunc


While 1
    Sleep(1000)
WEnd

Func HShellWndProc($hWnd, $Msg, $wParam, $lParam)
    Switch $wParam
        Case $HSHELL_WINDOWCREATED
            ConsoleWrite("Window created: " & $lParam & " (" & WinGetTitle($lParam) & ")" & @CRLF)
        Case $HSHELL_WINDOWACTIVATED
            ConsoleWrite("Window activated: " & $lParam & " (" & WinGetTitle($lParam) & ")" & @CRLF)
        Case $HSHELL_REDRAW
            ConsoleWrite("Window redraw: " & $lParam & " (" & WinGetTitle($lParam) & ")" & @CRLF)
      EndSwitch
EndFunc

;register/unregister ShellHook
Func ShellHookWindow($hWnd, $bFlag)
    Local $sFunc = 'DeregisterShellHookWindow'
    If $bFlag Then $sFunc = 'RegisterShellHookWindow'
    Local $aRet = DllCall('user32.dll', 'int', $sFunc, 'hwnd', $hWnd)

    Return $aRet[0]
EndFunc
;register window message
Func RegisterWindowMessage($sText)
    Local $aRet = DllCall('user32.dll', 'int', 'RegisterWindowMessage', 'str', $sText)
    Return $aRet[0]
EndFunc

The only thing i can put it down is in the second example it has this

GUICreate("", @DesktopWidth, @DesktopHeight,0,0)

Why would that cause the hook to fail when it is not even registered to that gui?

*Edit after more testing i reduce the width / height it starts working again (ie  GUICreate("", 1910, 1080,0,0)) but i need it full size because its a full-screen application

 

Edited by IanN1990
Posted

Closing.  Found this code

#include <APISysConstants.au3>
#include <WinAPISys.au3>

Opt('TrayAutoPause', 0)

OnAutoItExitRegister('OnAutoItExit')

Global $g_hForm = GUICreate('')
GUIRegisterMsg(_WinAPI_RegisterWindowMessage('SHELLHOOK'), 'WM_SHELLHOOK')
_WinAPI_RegisterShellHookWindow($g_hForm)

While 1
    Sleep(1000)
WEnd

Func WM_SHELLHOOK($hWnd, $iMsg, $wParam, $lParam)
    #forceref $iMsg

    Switch $hWnd
        Case $g_hForm
            Local $sTitle = WinGetTitle($lParam)
            Switch $wParam
                Case $HSHELL_REDRAW
                    If IsString($sTitle) Then
                        ConsoleWrite('Redrawn: ' & $sTitle & @CRLF)
                     EndIf
                  Case $HSHELL_WINDOWCREATED
                     If IsString($sTitle) Then
                        ConsoleWrite('Created: ' & $sTitle & @CRLF)
                     EndIf
                Case Else
                    If BitAND($wParam, $HSHELL_WINDOWACTIVATED) = $HSHELL_WINDOWACTIVATED And IsString($sTitle) Then
                        ConsoleWrite('Activated: ' & $sTitle & @CRLF)
                    EndIf
            EndSwitch
    EndSwitch
EndFunc   ;==>WM_SHELLHOOK

Func OnAutoItExit()
    _WinAPI_DeregisterShellHookWindow($g_hForm)
EndFunc   ;==>OnAutoItExit

I guess the code above is original post is buggy :) Hopefully this will help others if they come across this issue one day

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...