Jump to content

Loop through list of window/window handles?


Go to solution Solved by olo,

Recommended Posts

Im trying to get a list of windows that are open along with there handles.

Then I wanna search through the list for specific text contained in the $olos array and then if found...(well the if statement will take care of that).

 

#include-once

#include <WinAPI.au3>
#include <Array.au3>

#include <.\backup_100\Library\Error_log.au3>


Func ExceptionWindow()

    $aWnd = _WinAPI_EnumWindowsTop()

    ;_ArrayDisplay($aWnd)


    Local $contents = 15
    Local $olos[$contents] = ["Exception", "EExternal", "EOutOfMemory", "EOutOfResources", "EAccessViolation" &  _
        "EIntError", "EMathError", "EPrivilege", "EStackOverflow", "EInvalidArgument", "EInvalidOp", "EOverflow" &  _
        "EUnderflow", "EZeroDivide", "EDivByZero"]

    $gInteractiveLog = 0

    ACRAOpenLog("ERRORs.log", 2)




    If ( $aWnd AND WinGetText($olos)) Then


        Send("{CTRLDOWN}c{CTRLUP}")

        Local $olo = ClipGet()

        ACRAOutputerror($olo)

        Send("{ALTDOWN}{F4}{ALTUP}")

        Return 1

    EndIf

    ACRACloselog()

EndFunc

Func Test_ExceptionWindow()
    Return ExceptionWindow()
EndFunc

Test_ExceptionWindow()
 
 
Link to comment
Share on other sites

$aWind and $Olos are both arrays, you're going to need to loop through both arrays to find what you're looking for to see if they match. Once you find the match it's up to you to decide what you want to do with the information. You can't directly use the arrays the way you're doing.

And before you ask, do a search for looping through an array on the forum for how to do that, it's programming 101, and you should be able to find dozens of examples and methods to use.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You were shown on May 16th how to use an array, so there is little excuse.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

You have to know that if there is variables types, they need to be manipulated differently.

In you script, $aWnd is an array. If you want to test it, you have two functions : IsArray and Ubound.

If $aWnd Then ;means nothing, incorrect
    ;dostuff
EndIf

If IsArray($aWnd) = 1 Then ;$aWnd is an array
    ;dostuff
EndIf
Almost same thing for WinGetText, it returns a string.

If WinGetText($olos) Then ;incorrect (epic btw) (gets the text of an array pointer and then... ok I stop)
    ;dostuff
EndIf

If WinGetText($aWnd[1][0]) = $olos[0] Then ;compares the text of the window handle on the index 1 of $aWnd with the first item index of $olos which is Exception.
;dostuff
EndIf
On this second snippet you will have to include two nested For statements :

-One for looping through the $aWnd array

-One for looping through the $olos array

 

aWnd[1][0] = handle1
aWnd[2][0] = handle2

olos[0] = text1
olos[1] = text2

for i = 1 to 2
    for j = 0 to 1
        if WinGetText(aWnd[i][0]) = olos[j] then
            ;you're brilliant
        endif
    next
next
Edit: Well I can see that my keyboard, my brain, my fingers, my whatever are... slow.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

aWnd[1][0] = handle1
aWnd[2][0] = handle2
olos[0] = text1
olos[1] = text2

Do I need these two or can I just carry on with my declarations?

Local $contents = 15
    Local $olos[$contents] = ["Exception", "EExternal", "EOutOfMemory", "EOutOfResources", "EAccessViolation" &  _
        "EIntError", "EMathError", "EPrivilege", "EStackOverflow", "EInvalidArgument", "EInvalidOp", "EOverflow" &  _
        "EUnderflow", "EZeroDivide", "EDivByZero"]
Edited by olo
Link to comment
Share on other sites

You have to know that if there is variables types, they need to be manipulated differently.

In you script, $aWnd is an array. If you want to test it, you have two functions : IsArray and Ubound.

If $aWnd Then ;means nothing, incorrect
    ;dostuff
EndIf

If IsArray($aWnd) = 1 Then ;$aWnd is an array
    ;dostuff
EndIf
Almost same thing for WinGetText, it returns a string.

If WinGetText($olos) Then ;incorrect (epic btw) (gets the text of an array pointer and then... ok I stop)
    ;dostuff
EndIf

If WinGetText($aWnd[1][0]) = $olos[0] Then ;compares the text of the window handle on the index 1 of $aWnd with the first item index of $olos which is Exception.
;dostuff
EndIf
On this second snippet you will have to include two nested For statements :

-One for looping through the $aWnd array

-One for looping through the $olos array

 

aWnd[1][0] = handle1
aWnd[2][0] = handle2

olos[0] = text1
olos[1] = text2

for i = 1 to 2
    for j = 0 to 1
        if WinGetText(aWnd[i][0]) = olos[j] then
            ;you're brilliant
        endif
    next
next
Edit: Well I can see that my keyboard, my brain, my fingers, my whatever are... slow.

Br, FireFox.

 

Got it Working :) thanks again

Link to comment
Share on other sites

Here is my full script now. The code looks fine however the results do not reflect this in the error log. Attached is a screenshot and the actual error window im trying to get the text from.

#include-once

#include <WinAPI.au3>
#include <Array.au3>

#include <.\backup_100\Library\Error_log.au3>



Local $aWindows = _WinAPI_EnumWindowsTop()

; Very handy debugging function, but not left it in the framework code live
;_ArrayDisplay($aWindows)

Local const $contents = 15
Local $olos[$contents] = ["EDivByZero", "EExternal", "EOutOfMemory", "EOutOfResources", "EAccessViolation" &  _
    "EIntError", "EMathError", "EPrivilege", "EStackOverflow", "EInvalidArgument", "EInvalidOp", "EOverflow" &  _
    "EUnderflow", "EZeroDivide", "Exception"]

Func ExceptionWindow()

    $gInteractiveLog = 0

    ACRAOpenLog("FUUUUUUU.log", 2)

    For $i = 1 to $aWindows[0][0]

        For $j = 0 to 14
            If WinGetText($aWindows[$i][0]) = $olos[$j] Then

                Sleep(500)

                Send("{CTRLDOWN}c{CTRLUP}")

                Local $olo = ClipGet()

                ACRAOutputerror($olo)

                ;Send("{ENTER}")

                Return 1
            Else
                ACRAOutputinfo("Nothing suspicious found.")
            EndIf
        Next
    Next

    ACRACloseLog()

EndFunc

Func Test_ExceptionWindow()
    Return ExceptionWindow()
EndFunc

ExceptionWindow()
 
Link to comment
Share on other sites

Here is your fixed code, please read my comments :

;~ #include-once ;only required if this script is an UDF itself

#include <WinAPI.au3>
#include <Array.au3>

#include ".\backup_100\Library\Error_log.au3" ;use "" for local scripts and <> for internal includes

Global $aWindows = _WinAPI_EnumWindowsTop()

; Very handy debugging function, but not left it in the framework code live
;_ArrayDisplay($aWindows)

Global Const $iContents = 15

; & _ is used to continue a string, not an array
Global $aErrors[$iContents] = ["EDivByZero", "EExternal", "EOutOfMemory", "EOutOfResources", "EAccessViolation", _
        "EIntError", "EMathError", "EPrivilege", "EStackOverflow", "EInvalidArgument", "EInvalidOp", "EOverflow", _
        "EUnderflow", "EZeroDivide", "Exception"]

ExceptionWindow()

Func ExceptionWindow()
    Local $sClip = "" ;never declare a variable in a loop

    $gInteractiveLog = 0

    ACRAOpenLog("FUUUUUUU.log", 2)

    For $i = 1 To $aWindows[0][0]
        For $i2 = 0 To $iContents - 1
            If WinGetText($aWindows[$i][0]) = $aErrors[$i2] Then
                Sleep(500) ;what is this for?

                Send("^c") ;better than: {CTRLDOWN}c{CTRLUP}

                $sClip = ClipGet()

                ACRAOutputerror($olo)

                ;Send("{ENTER}")

                Return 1
            Else
                ACRAOutputinfo("Nothing suspicious found.")
            EndIf
        Next
    Next

    ACRACloseLog()
EndFunc   ;==>ExceptionWindow

Func Test_ExceptionWindow()
    Return ExceptionWindow()
EndFunc   ;==>Test_ExceptionWindow
Edit: Added indents.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Here is your fixed code, please read my comments :

;~ #include-once ;only required if this script is an UDF itself

#include <WinAPI.au3>
#include <Array.au3>

#include ".\backup_100\Library\Error_log.au3" ;use "" for local scripts and <> for internal includes

Global $aWindows = _WinAPI_EnumWindowsTop()

; Very handy debugging function, but not left it in the framework code live
;_ArrayDisplay($aWindows)

Global Const $iContents = 15

; & _ is used to continue a string, not an array
Global $aErrors[$iContents] = ["EDivByZero", "EExternal", "EOutOfMemory", "EOutOfResources", "EAccessViolation", _
        "EIntError", "EMathError", "EPrivilege", "EStackOverflow", "EInvalidArgument", "EInvalidOp", "EOverflow", _
        "EUnderflow", "EZeroDivide", "Exception"]

ExceptionWindow()

Func ExceptionWindow()
    Local $sClip = "" ;never declare a variable in a loop

    $gInteractiveLog = 0

    ACRAOpenLog("FUUUUUUU.log", 2)

    For $i = 1 To $aWindows[0][0]
        For $i2 = 0 To $iContents - 1
            If WinGetText($aWindows[$i][0]) = $aErrors[$i2] Then
                Sleep(500) ;what is this for?

                Send("^c") ;better than: {CTRLDOWN}c{CTRLUP}

                $sClip = ClipGet()

                ACRAOutputerror($olo)

                ;Send("{ENTER}")

                Return 1
            Else
                ACRAOutputinfo("Nothing suspicious found.")
            EndIf
        Next
    Next

    ACRACloseLog()
EndFunc   ;==>ExceptionWindow

Func Test_ExceptionWindow()
    Return ExceptionWindow()
EndFunc   ;==>Test_ExceptionWindow
Edit: Added indents.

Br, FireFox.

 

Yea fixed the code around a bit thought still the same outcome. forgot to include a screenshot.

post-79352-0-07129100-1370596728_thumb.j

Link to comment
Share on other sites

What if one of your top windows contains the EDivByZero text?

Check the console debug:

#include <WinAPI.au3>
#include <Array.au3>

#include ".\backup_100\Library\Error_log.au3"

Global $aWindows = _WinAPI_EnumWindowsTop()

Global Const $iContents = 15

Global $aErrors[$iContents] = ["EDivByZero", "EExternal", "EOutOfMemory", "EOutOfResources", "EAccessViolation", _
        "EIntError", "EMathError", "EPrivilege", "EStackOverflow", "EInvalidArgument", "EInvalidOp", "EOverflow", _
        "EUnderflow", "EZeroDivide", "Exception"]

ExceptionWindow()

Func ExceptionWindow()
    Local $sClip = "", $sText = ""

    $gInteractiveLog = 0

    ACRAOpenLog("FUUUUUUU.log", 2)

    For $i = 1 To $aWindows[0][0]
        For $i2 = 0 To $iContents - 1
            $sText = WinGetText($aWindows[$i][0])

            ConsoleWrite(WinGetTitle($aWindows[$i][0]) & ": " & $sText & @CrLf)

            If $sText = $aErrors[$i2] Then
                Send("^c")

                $sClip = ClipGet()

                ACRAOutputerror($sClip)

                ;Send("{ENTER}")

                Return 1
            Else
                ACRAOutputinfo("Nothing suspicious found.")
            EndIf
        Next
    Next

    ACRACloseLog()
EndFunc   ;==>ExceptionWindow

Func Test_ExceptionWindow()
    Return ExceptionWindow()
EndFunc   ;==>Test_ExceptionWindow
Edit: Added indents.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Yea still no luck. Still getting rubbish results in the log like the screenshot.

The point of the Send("^c") is if you got the message box in front of you, use that command to capture the data inside the message box. Then output that into the log.

Edited by olo
Link to comment
Share on other sites

So you made a text search in the console and you are telling me that you have not found "EDivByZero" but you have the error log of it?

opposite way around. I see it in the console, but not in the error log.

Link to comment
Share on other sites

  • Solution

Solved it! :D few tweaks such as StringInstr and this statement was nested in the 2nd for loop when it should have been stated immediately after the first for loop and before the second one.. ---"$sText = WinGetText($aWindows[$i][0])"

 
#include-once

#include <WinAPI.au3>
#include <Array.au3>

#include <.\backup_100\Library\Error_log.au3>


; Very handy debugging function, but not left it in the framework code live
;_ArrayDisplay($aWindows)

Local $Errors[15] = ["EDivByZero", "EExternal", "EOutOfMemory", "EOutOfResources", "EAccessViolation" & _
        "EIntError", "EMathError", "EPrivilege", "EStackOverflow", "EInvalidArgument", "EInvalidOp", "EOverflow" & _
        "EUnderflow", "EZeroDivide", "Exception"]

Func ExceptionWindow()

    Local $aWindows = _WinAPI_EnumWindowsTop()

    Local Const $contentCounter = UBound($Errors)

    $gInteractiveLog = 0

    ACRAOpenLog("FUUUUUUU.log", 2)

    Local $olo = "", $sText = ""

    For $i = 1 To $aWindows[0][0]

        $sText = WinGetText($aWindows[$i][0])

        For $j = 0 To $contentCounter - 1

            If StringInStr ( $sText, $Errors[$j] ) Then

                ACRAOutputerror($sText)

                Return 1
            Else
                ACRAOutputinfo("Nothing suspicious found.")
            EndIf
        Next
    Next

    ACRACloseLog()

EndFunc   ;==>ExceptionWindow

Func Test_ExceptionWindow()
    Return ExceptionWindow()
EndFunc   ;==>Test_ExceptionWindow

ExceptionWindow()
 
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...