Jump to content

Difficulty copying 2DArray, returned from WinList()


Recommended Posts

Hi,

 

It's been a while since I've used AutoIt, but I'm having fun with it again.

I'm running on Windows 7 Pro, 64_bit. with AutoIt 3.3.14.2+ 

 

In my program, I've called Winlist(), so that I can get a list of all windows on the system.

I then iterate through the titles, and attempt to copy the title & handle of Windows I'm specifically interested in.

While this seems to be a very easy task, I'm having a strange problem copying the title & handle of the two windows that I'm interested in.

In the For loop:

For $i = 1 To $AllWindowsCount

ConsoleWrite() displays the expected Title & handle, from the WindowsDupe array, so it appears that the copy from the $AllWindows array

works as expected.

However, when

_ArrayDisplay($WindowsDupe)

is called after the for loop, $WindowsDupe is empty.

 

I wasn't able to find any functions in the help file, for copying entire arrays or certain elements, so I thought I would just copy them manually, as seen in the for loop.

What am I doing wrong with the WindowsDupe array?

 

Thanks

 

Thanks

 

My code is attached.

ListWindows2.au3

Edited by cappy2112
Added AutoIt version number, for clarity
Link to post
Share on other sites
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <Array.au3>

_Main()

; ******************************
; GLOBAL SETTINGS
;
AutoItSetOption("WinTitleMatchMode", 2) ; match ANY substring in the title
Break($BREAK_ENABLE)
;
; ******************************


Func FindAllWindows($titlematch)

    Local $NonHIWindowCount = 0
    Local $AllWindowsCount = 0
    Local $AllWindows
    Local $i
    Local $WindowsDupe[10][2] ; arbitrary size, will ReDim later
    Local $TitleMatchCount = 0
    Local $temp

    $AllWindows = WinList()


    $AllWindowsCount = $AllWindows[0][0]
    MsgBox($MB_OK, "WINDOW COUNT", "There are " & $AllWindowsCount & " Windows")

    ReDim $WindowsDupe[(UBound($AllWindows)) - 1][2]
    ;_ArrayColInsert($WindowsDupe, 1) ; add the second dimension
    ;$count = 0
    $TitleMatchCount = 0
    For $i = 0 To $AllWindowsCount

        If StringInStr($AllWindows[$i][0], $titlematch) Then

            $WindowsDupe[$TitleMatchCount][0] = $AllWindows[$i][0] ; copy this HI window's title
            $WindowsDupe[$TitleMatchCount][1] = $AllWindows[$i][1] ; copy this HI window's handle

            ConsoleWrite("Idx = " & $i & "  Title: " & $WindowsDupe[$i][0] & "Hndl: " & $WindowsDupe[$i][1] & @CRLF)
            $TitleMatchCount += 1

        EndIf
    Next
    ReDim $WindowsDupe[$TitleMatchCount][2]
    ;$WindowsDupe[0][0] = $TitleMatchCount
    ;_ArrayDisplay($WindowsDupe, $TitleMatchCount + 1)

    Return $WindowsDupe ; return a list of all windows

EndFunc   ;==>FindAllWindows


Func _Main()


    Local $wHnd
    Local $hiWindowList
    Local $WindowCount
    Local $WindowList

    $WindowList = FindAllWindows("Untitled - Notepad")
    _ArrayDisplay($WindowList)

    MsgBox($MB_OK, "AFTER RETURN", "RETURNED FROM FINDALLWINDOWS()")


EndFunc   ;==>_Main

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to post
Share on other sites
  • Moderators

cappy2112,

The information is there, but because you make the Dupe array as big as the WinList return, the found data is hidden somewhere within it. Best to do something like this:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
    #AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <Array.au3>

_Main()

; ******************************
; GLOBAL SETTINGS
;
AutoItSetOption("WinTitleMatchMode", 2) ; match ANY substring in the title
Break($BREAK_ENABLE)
;
; ******************************


Func FindAllWindows($titlematch)

    Local $NonHIWindowCount = 0
    Local $AllWindowsCount = 0
    Local $AllWindows
    Local $i
    Local $WindowsDupe[1][2] = [[0]]
    Local $TitleMatchCount = 0
    Local $temp

    $AllWindows = WinList()

    _ArrayDisplay(WinList)

    $AllWindowsCount = $AllWindows[0][0]
    MsgBox($MB_OK, "WINDOW COUNT", "There are " & $AllWindowsCount & " Windows")

    $TitleMatchCount = 0
    For $i = 1 To $AllWindowsCount

        If StringInStr($AllWindows[$i][0], $titlematch) <> 0 Then
            $TitleMatchCount += 1
            ReDim $WindowsDupe[$TitleMatchCount + 1][2]
            $WindowsDupe[$TitleMatchCount][0] = $AllWindows[$i][0] ; copy this HI window's title
            $WindowsDupe[$TitleMatchCount][1] = $AllWindows[$i][1] ; copy this HI window's handle
        EndIf
    Next

    ;$WindowsDupe[0][0] = $TitleMatchCount
    _ArrayDisplay($WindowsDupe, "Dupe array", Default, 8)

    Return $WindowsDupe ; return a list of all windows

EndFunc   ;==>FindAllWindows


Func _Main()


    Local $wHnd
    Local $hiWindowList
    Local $WindowCount
    Local $WindowList

    $WindowList = FindAllWindows("Untitled - Notepad")
    _ArrayDisplay($WindowList, "Returned", Default, 8)

    MsgBox($MB_OK, "AFTER RETURN", "RETURNED FROM FINDALLWINDOWS()")


EndFunc   ;==>_Main

Now the Dupe array is only as large as it needs to be.

M23

Edit: I see JohnOne had the same idea.

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to post
Share on other sites
11 minutes ago, JohnOne said:
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <Array.au3>

_Main()

; ******************************
; GLOBAL SETTINGS
;
AutoItSetOption("WinTitleMatchMode", 2) ; match ANY substring in the title
Break($BREAK_ENABLE)
;
; ******************************


Func FindAllWindows($titlematch)

    Local $NonHIWindowCount = 0
    Local $AllWindowsCount = 0
    Local $AllWindows
    Local $i
    Local $WindowsDupe[10][2] ; arbitrary size, will ReDim later
    Local $TitleMatchCount = 0
    Local $temp

    $AllWindows = WinList()


    $AllWindowsCount = $AllWindows[0][0]
    MsgBox($MB_OK, "WINDOW COUNT", "There are " & $AllWindowsCount & " Windows")

    ReDim $WindowsDupe[(UBound($AllWindows)) - 1][2]
    ;_ArrayColInsert($WindowsDupe, 1) ; add the second dimension
    ;$count = 0
    $TitleMatchCount = 0
    For $i = 0 To $AllWindowsCount

        If StringInStr($AllWindows[$i][0], $titlematch) Then

            $WindowsDupe[$TitleMatchCount][0] = $AllWindows[$i][0] ; copy this HI window's title
            $WindowsDupe[$TitleMatchCount][1] = $AllWindows[$i][1] ; copy this HI window's handle

            ConsoleWrite("Idx = " & $i & "  Title: " & $WindowsDupe[$i][0] & "Hndl: " & $WindowsDupe[$i][1] & @CRLF)
            $TitleMatchCount += 1

        EndIf
    Next
    ReDim $WindowsDupe[$TitleMatchCount][2]
    ;$WindowsDupe[0][0] = $TitleMatchCount
    ;_ArrayDisplay($WindowsDupe, $TitleMatchCount + 1)

    Return $WindowsDupe ; return a list of all windows

EndFunc   ;==>FindAllWindows


Func _Main()


    Local $wHnd
    Local $hiWindowList
    Local $WindowCount
    Local $WindowList

    $WindowList = FindAllWindows("Untitled - Notepad")
    _ArrayDisplay($WindowList)

    MsgBox($MB_OK, "AFTER RETURN", "RETURNED FROM FINDALLWINDOWS()")


EndFunc   ;==>_Main

 

 

ReDim $WindowsDupe[(UBound($AllWindows))  -1][2]

Why? All of the examples I've seen- including in the help file,
always show UBound(Array) +1.

Isn't dimension [2] out of bounds, for a 2D array??

 
 ReDim $WindowsDupe[$TitleMatchCount + 1][2]
 Please explain dimension [2], since this is a 2D array. Do array indices start at 0 or 1, in AutoIT?


     
     

 

 

Link to post
Share on other sites

in its creation you are saying it has 2 columns, 0 and 1

 

if you reference 2, you're out of bounds.

and ubound -1 is the actual amount of elements, ubound is the highest element

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By XGamerGuide
      I'm trying to call WinList() with a handle. That should be possible:
      But I only get back an 2d array that looks like this:
      ---------
      0 |
      (That means no window was found.)
      It's not the window or the handle. When I call other functions like WinExists() or WinGetHandle() it works.
      Is it because of me, the description in the reference or because of Autoit who makes a mistake?
      No, because of the rest of my program I don't want to use WinGetTitle() because the format of WinList() has to be kept. Alternatively, I could create a 2d array myself, but it should also work with WinList() ... I hope.
    • By nooneclose
      I need to dynamically resize my 2d array while looping. 
      I know this code:
      ReDim $rArray[UBound($rArray) + 1] works for the rows, however, I also need to increase the columns. How would i go about increasing both rows and columns while looping? 
    • By D2thunder
      Hi all, I wasnt active in this Forum forum for Years. 😅
      Since I need something with Json in AutoIt i write a little UDF to save a variable to file to read it with another language - and back. After a while until my other script grows i thought - lets integrate all variables wich is possible, because i want have arrays to.
      This is my first public UDF in Autoit - normaly my AutoIt scripts are quick an dirty.... - like the examples for this udf. - someone want to write nice examples?
      I tried to write this UDF properly, cared for processing speed, low mem consumption as possible, a short code, and a high usability without beeing very familiar with arrays.
      What is it for? - convert nearly all types of variables to a json string for saving it in a file or to an ini. Read it back in to a same formated variable and preserv variable types. All array types are supported with nested arrays, the only restriction is ram and array/stringsize - and time if the array is very big. You can use Filters for variable types and some other options.
      If anyone found a bug or any suggestions, please post it.
       
      get Json.au3 from here Json.au3
       
       
      JsonVar 2019.01.25.1.zip
    • By AndyK70
      I'm trying to fill a ListView with all normal viewable windows to act with them.
      First I tried with WinList:
      Local $aWinList = WinList("[REGEXPTITLE:(?i)(.+)]") Local $aTmp, $iID ;~ _ArrayDisplay($aWinList) For $i = $aWinList[0][0] To 1 Step -1 ; going backwards not disturbing the index while cycling through and deleting some If StringStripWS( $aWinList[$i][0], 3) == "" Or _ Not BitAND(WinGetState($aWinList[$i][1]), $WIN_STATE_VISIBLE) Or _ BitAND(WinGetState($aWinList[$i][1]), $WIN_STATE_MINIMIZED ) Then _ArrayDelete($aWinList, $i) Else ; Window has a Title and is "visible" $aTmp = WinGetPos($aWinList[$i][1]) If $aTmp[0] < -1000 Or $aTmp[1] < -1000 Then ; Window is minimized or tray icon _ArrayDelete($aWinList, $i) EndIf EndIf Next $aWinList[0][0] = UBound($aWinList)-1 ; getting actual # of windows ; Each row is now [ID]=> [Title], [hWnd] But it keeps getting Windows which are definitely not there at least not visible:

      Those windows "Rechner", "Einstellungen", "Netflix", "Microsoft Store", ... are not there!?! 
      It should list only the first three windows, which are real.
      I even tried it with _WinAPI_ UDF:
      $hWnd = _WinAPI_GetForegroundWindow() ; Add items _GUICtrlListView_BeginUpdate($idListview) If $hWnd <> 0 Then $iI = 0 Do If _WinAPI_IsWindow($hWnd) And _WinAPI_IsWindowVisible Then _GUICtrlListView_AddItem($idListview, WinGetTitle($hWnd)) _GUICtrlListView_AddSubItem($idListview, $iI, $hWnd, 1) $iI += 1 $hWnd = _WinAPI_GetWindow($hWnd, $GW_HWNDNEXT) EndIf Until $hWnd = 0 EndIf But it is the same...
       
      How can i distinguish those invisible windows from normal ones?
      PS: I'm using Windows 10, maybe it is important to know?
    • By AutoitMike
      I am currently using Autoit's "WinList" function in an Autoit script, I fully understand it.
      Autoit provides a nice Library for Word VBA so that Some of Autoit's functions can be used within a MSWord VBA script, some of which I am using.
      I have a use for Autoit's "WinList" function which has been provided by AutoIt in their Library for MSWord VBA scripting. The WinList function Creates an array when run. MSWord VBA does not seem to allow this. To create an array in VBA you have to first DIM it, and this does not seem to work for the WinList function.
      For example:
      Dim AutoIt As New AutoItX3Lib.AutoItX3
      MyArray= Autoit(WinList)  produces an error, whether I dim the array or not. 
      It seems that AutoIt has provided a function for VBA that cant be used ??
      Can someone figure out a way to use WinList within Word VBA?
      Thanks
       
×
×
  • Create New...