Sign in to follow this  
Followers 0
sandin

the best way to hook a window?

12 posts in this topic

Hi everybody.

My 1st question is: What's the best way to hook a window on desktop (something like task manager - applications section does)?

I tried Siao's script: http://www.autoitscript.com/forum/index.ph...536&hl=hook

it's amazing, though it's not hooking all windows, like winlist() displays them (it doesn't hook RUN, or BROWSE windows, etc...)

the script Siao made is very cool, cause it can hook a window without using many CPU resources, but cause of a situation of not being able to hook all windows, I tried setting winlist() function in a loop, and comparing it constantly with the old list, but this method is taking too much CPU resources :)

so, what's the best way to hook all windows?

My 2nd question is: Is it possible and how to hook a window's icon? (if Task manager can, I suppose AutoIt can do it as well)

tnx for help :P

Share this post


Link to post
Share on other sites



Excuse my ignorance, but I don't know what you mean by "hook".

Anyway, about the icon, check out this example:

#include <Process.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$A_Allwins=WinList()

For $i=0 To $A_Allwins[0][0]
    If _IsVisible($A_Allwins[$i][1]) Then
        $s_proc=_ProcessGetName(WinGetProcess($A_Allwins[$i][1]))
        $s_tit=$A_Allwins[$i][0]
        _WindowInfo($s_tit,$s_proc)
    EndIf
Next

Func _IsVisible($hwnd)
    Return BitAND(WinGetState($hwnd),2)
EndFunc

Func _WindowInfo($s_tit,$s_proc)

    $Form1 = GUICreate("Window info", 448, 218, 281, 221)
    $Label1 = GUICtrlCreateLabel($s_tit, 44, 20, 366,50, $SS_CENTER)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    $Icon1 = GUICtrlCreateIcon(Processpath($s_proc), -1, 211, 60, 32, 32, BitOR($SS_NOTIFY,$WS_GROUP))
    $Button1 = GUICtrlCreateButton("OK", 131, 136, 193, 41, $BS_DEFPUSHBUTTON)
    GUISetState(@SW_SHOW)


    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE,$Button1
                GUIDelete()
                Return

        EndSwitch
    WEnd
EndFunc

Func Processpath($Name)
    $strComputer = "."
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Caption = '"&$Name&"'", "WQL", _
                                         $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    For $objItem In $colItems
       
    If $objItem.ExecutablePath Then Return $objItem.ExecutablePath

    Next
EndFunc

Is that what you wanted? :)

Share this post


Link to post
Share on other sites

lol! Respect! that's amazing! :) thank you! =)

I still have this one issue, is there a way to hook all windows like Siao did but with question windows, run window, etc (siao's script doesn't hook them) (look 1st post) (and if I set WinGetProcess or winlist in an infinite loop, and compare it to the old one to see if I have new window, it'll take lots of CPU resources)

Share this post


Link to post
Share on other sites

Well... my hook.dll will get whatever...

http://www.autoitscript.com/forum/index.ph...st&p=458286

Const $WH_CBT = 5

Const $HCBT_SETFOCUS = 0x1400 + 0x1A30
Const $HCBT_ACTIVATE = 0x1400 + 0x1A31
Const $HCBT_CREATEWND = 0x1400 + 0x1A32
Const $HCBT_DESTROYWND = 0x1400 + 0x1A33
Const $HCBT_MINMAX = 0x1400 + 0x1A34


Global $n,$msg,$buffer = ""

HotKeySet("{ESC}","GoAway")

$gui = GUICreate("test")

Global $DLLinst = DLLCall("kernel32.dll","hwnd","LoadLibrary","str",".\hook.dll")
Global $cbtHOOKproc = DLLCall("kernel32.dll","hwnd","GetProcAddress","hwnd",$DLLInst[0],"str","CBTProc")

Global $hhCBT = DLLCall("user32.dll","hwnd","SetWindowsHookEx","int",$WH_CBT, _
        "hwnd",$cbtHOOKproc[0],"hwnd",$DLLinst[0],"int",0)

DLLCall(".\hook.dll","int","SetValuesCBT","hwnd",$gui,"hwnd",$hhCBT[0])

GUIRegisterMsg($HCBT_ACTIVATE,"myCBTfunc")
;GUIRegisterMsg($HCBT_CREATEWND,"myCBTfunc")

While 1
    $msg = GUIGetMsg()
    If $msg = -3 Then ExitLoop
WEnd

Func MyCBTFunc($hWndGUI, $MsgID, $WParam, $LParam)
    $n += 1
    If $n > 25 Then
        $n = 25
        $buffer = StringTrimLeft($buffer,StringInStr($buffer,@LF))
    EndIf
    $buffer &= "CBT: " & $MsgID & "," &  $WParam & "," &  $LParam & @LF
    ToolTip($buffer)
EndFunc

Func GoAway()
    Exit
EndFunc

Func OnAutoItExit()
    DLLCall("user32.dll","int","UnhookWindowsHookEx","hwnd",$hhCBT[0])
    DLLCall("kernel32.dll","int","FreeLibrary","hwnd",$DLLinst[0])
EndFunc

f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

Would it be OK to cycle through your window handles and check that they WinExist() on some interval... or do you absolutely need an event driven solution?

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0