Results 1 to 16 of 16

Thread: Get All Open windows

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    19

    Get All Open windows

    Is it possible to use the windows api to get a list of all windows that are currently open ? I want to be able to run a function that gets a list of all open windows and depending on which are open I then call seperate routines. I would need to be able to check if there were windows or popups. Any help would be appreciated.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Get All Open windows

    Are you talking about a Task Manager type of list?

    What development language are you using?

  3. #3
    Lively Member Spetnik's Avatar
    Join Date
    Jan 2002
    Posts
    121

    Re: Get All Open windows

    See "Creating a task list" here: http://vb.mvps.org/articles/ap199902.pdf
    After that see "Obtaining the alt-tab order" here: http://vb.mvps.org/articles/ap200003.asp


  4. #4
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Get All Open windows

    For just the windows, and not the process itself, you could use the GetWindow function with the constants that have the prefix GW_.

    Use AnyPopup to determine if a popup window exists anywhere on the screen.

    Use GetLastActivePopup to obtain the handle of the most recently used popup window for a given parent window. returns a handle to most recently used popup window.

    Use ShowOwnedPopups to show or hide all popup windows owned by the specified window.

    If you need a demo, I can whip one up for ya.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    19

    Re: Get All Open windows

    I am using VBScript. I don't need the list of processes from the task manager just what windows are currently open. I am very new to using winapi and would appreciate it if anyone could point me to some code examples. Thanks for the suggestions so far.

  6. #6
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Get All Open windows

    I wrote up a demo in VB.NET 2005 as close to vbscriptious as I could get.
    This makes a list of the open programs that have a title.
    You can check for popups in here if you want.

    Code:
        Const GW_HWNDNEXT As Integer = 2
        Private Declare Function apiGetWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Integer, ByVal wCmd As Integer) As Integer
        Private Declare Function apiGetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
        Private Declare Function apiGetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Integer) As Integer
        Private Declare Function apiGetTopWindow Lib "user32" Alias "GetTopWindow" (ByVal hwnd As Integer) As Integer
        Private Declare Function apiGetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Integer
        Private Declare Function apiShowOwnedPopups Lib "user32" Alias "ShowOwnedPopups" (ByVal hWnd As Integer, ByVal fShow As Integer) As Integer
        Private Declare Function apiAnyPopup Lib "user32" Alias "AnyPopup" () As Integer
        Private Declare Function apiGetLastActivePopup Lib "user32" Alias "GetLastActivePopup" (ByVal hWndOwnder As Integer) As Integer 'Obtains the handle of the most recently used popup window for a given parent window.  returns a Handle to most recently used popup window. hWndOwner if no popup was found.
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show(GetWindows())
        End Sub
    
        Private Function GetWindows() As String
            On Error Resume Next
            Dim hwnd As Integer
            Dim s1 As String
            Dim s2 As String
            hwnd = apiGetTopWindow(apiGetDesktopWindow)
            s1 = ""
            s2 = ""
            Do
                s2 = GetWindowName(hwnd)
    
               If s2 = "AppTitle" Then
                  'Do Something here
               End If
    
                s1 = s1 & " - " & s2
                hwnd = apiGetWindow(hwnd, GW_HWNDNEXT)
                If hwnd = 0 Then
                    GetWindows = s1
                    Exit Do
                End If
            Loop
        End Function
        Public Function GetWindowName(ByVal hWnd As Integer) As String
            On Error Resume Next
            Dim tLength As Integer
            Dim rValue As Integer
            Dim wName As String
            tLength = apiGetWindowTextLength(hWnd) + 4
            wName = ""
            wName = wName.PadLeft(tLength) 'add buffer
            rValue = apiGetWindowText(hWnd, wName, tLength)
            wName = wName.Substring(0, rValue) 'strip buffer
            GetWindowName = wName
        End Function

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Get All Open windows

    You can't call native API functions from VBScript.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get All Open windows

    You could have your vbscript run a VB 6 executable that would call the API calls like TTn posted.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Get All Open windows

    Or a COM DLL, but both would dramatically reduce the portability of the script, which is the main reason one would use VBScript in the first place.

    The language and the task are simply incompatible. Use something else if at all possible.

  10. #10
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Get All Open windows

    I know but he said he's very new to the windows API, so I figured I'd write it as vbscriptious as possible so he'd be able to read it.
    Maybe he is using vbscript.

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Get All Open windows

    From post #5:
    Quote Originally Posted by Railay29
    I am using VBScript.

  12. #12
    Fanatic Member TTn's Avatar
    Join Date
    Jul 2004
    Posts
    708

    Re: Get All Open windows

    Yes but,

    In post one:
    Is it possible to use the windows api..
    In post 5:
    I am very new to using winapi...
    Either way it should help him

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Get All Open windows

    Not if he is using VBScript.

    Would you be trying to run this from a web page?

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get All Open windows

    Either running vbscript in a webpage or some other source, the best solution would be to use VB 6 code in a dll or exe file and call from vbscript.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    19

    Re: Get All Open windows

    Hi all and thanks for all the help. Just to add some detail to what I am doing, I am using an automation tool called Quick Test Professional which uses VBScript as its coding language. I working on automating the actions of a browser based application and unfortunately the tool doesn't pick up which browsers are open particularly well. Thats why I thought I could use a winapi call to get the open windows so that I can then interact with them.

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get All Open windows

    Maybe you could use VB 6 executable to find the browser you need and set it as the active window. Then in vbscript maybe their function will pickup the active browser window first?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width