IMPORTANT NOTICE: As of 21 February, anything posted in this community WILL NOT BE MIGRATED to our new community site. We have pulled all user information and data from this site and are now in an approximately week long process of importing users, roles, and data to our new site. We are leaving this site open and active so you can post and hopefully get a response until the migration is complete. Once complete, the URL that currently takes you to this site will take you to our new site so your bookmarks will work as always. Please read the information on a New Login Process
There is no inbuilt function for this. You will need to enumerate all of the running processes and then ascertain which of those processes has an open GUI. The following code module will do this for you:
Module modGetAllOpenWindows ' Unmanaged API calls Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32 Public Declare Function IsWindowVisible Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32 Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hwnd As IntPtr) As Int32 Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Int32) As Int32 Public Declare Function GetParent Lib "user32.dll" (ByVal intptr As IntPtr) As IntPtr Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean ' Public constants Public Const GWL_HWNDPARENT As Int32 = -8 ' Public function Public newWindowList As List(Of String) ' Private function Private newHandleList As List(Of IntPtr) ' Structure - checks for a GUI Private Structure RECT Public left As Integer Public top As Integer Public right As Integer Public bottom As Integer Public Sub New(ByVal _left As Integer, ByVal _top As Integer, ByVal _right As Integer, ByVal _bottom As Integer) left = _left top = _top right = _right bottom = _bottom End Sub End Structure ' ----------------------------------------------------------- ' Enumerate the running processes - discard any that does not have a GUI ' ----------------------------------------------------------- Private Function EnumWinProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean Dim sWindow As String = Nothing If IsWindowVisible(hwnd) Then If GetParent(hwnd) = IntPtr.Zero Then If GetWindowLong(hwnd, GWL_HWNDPARENT) = 0 Then sWindow = String.Empty.PadLeft(GetWindowTextLength(hwnd) + 1) GetWindowText(hwnd, sWindow, sWindow.Length) If Not String.IsNullOrEmpty(sWindow.Substring(0, sWindow.Length - 1)) Then newWindowList.Add(sWindow.Substring(0, sWindow.Length - 1)) End If End If End If End If EnumWinProc = True End Function ' ----------------------------------------------------------- ' Start the enumeration process ' ----------------------------------------------------------- Public Sub RefreshWindowList() newWindowList = New List(Of String) EnumWindows(AddressOf EnumWinProc, CInt(True)) End Sub End Module
To call the module add the following code to your testcase:
RefreshWindowList()
The list of open windows is written to newWindowList. You can loop through the array and recover the window names as required. For example, the following code will call the enumeration process and then prints the open windows to the results file:
RefreshWindowList() For Each item As String In newWindowList Console.WriteLine(item) Next
The results output will depend on your system but as an example it will look something like this:
getOpenWindows (Running) - Microsoft Visual Studio (Administrator)Q-Dir 5.04Inbox - Mailbox - [redacted] - Microsoft OutlookProgram Manager