Resolved No Windows Explorer in Enumerated Window List in Listbox

Pixel-Ink

Member
Joined
Jun 12, 2023
Messages
13
Programming Experience
5-10
I have this working code (below) that gets a list of all opened windows.
However, it doesn't show 2 Windows Explorer windows I have open on the desktop.
What do I use to make those to show up in the list??
Thanks in advance. I would really appreciate your input.

VB.NET:
Imports System.IO
Imports System.Runtime.InteropServices

<DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As Boolean
    End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer
    End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Integer
    End Function
<DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function GetWindowPlacement(ByVal hWnd As IntPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Boolean
    End Function
<DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
    End Function

    Public Const SW_SHOWMINIMIZED As Integer = 2
    Public Const SW_HIDE As Integer = 0

Private Sub frmScriptEditor_Load(sender As Object, e As EventArgs) Handles Me.Load

        'API
        lstActiveWindows.Items.Clear()

        ' Get the current process ID
        Dim currentProcessId As Integer = Process.GetCurrentProcess().Id

        ' Get all running processes
        Dim processes As Process() = Process.GetProcesses()

        ' Iterate through each process
        For Each process As Process In processes
            If process.Id = currentProcessId Then Continue For

            ' Check if the process has a main window handle
            If process.MainWindowHandle <> IntPtr.Zero Then
                ' Check if the main window is visible, not minimized, and not an ApplicationFrameHost process
                ' Get the title of the main window
                Dim titleLength As Integer = GetWindowTextLength(process.MainWindowHandle)
                Dim titleBuilder As New System.Text.StringBuilder(titleLength + 1)
                GetWindowText(process.MainWindowHandle, titleBuilder, titleBuilder.Capacity)
                Dim title As String = titleBuilder.ToString()

                If String.IsNullOrEmpty(title) Then Continue For

                ' Print the title to the console or display it in a ListBox or any other control
                If title = "NVIDIA GeForce Overlay" Or title = "Microsoft Text Input Application" Or title = "Program Manager" Then
                    'skip
                Else
                    lstActiveWindows.Items.Add(title)
                End If

           End If

        Next

    End Sub

Private Structure WINDOWPLACEMENT

        Public Length As Integer
        Public Flags As Integer
        Public ShowCmd As Integer
        Public MinPosition As System.Drawing.Point
        Public MaxPosition As System.Drawing.Point
        Public NormalPosition As System.Drawing.Rectangle

    End Structure

 Private Function IsMinimized(ByVal hWnd As IntPtr) As Boolean

        Dim wp As New WINDOWPLACEMENT()
        wp.Length = Marshal.SizeOf(wp)
        If GetWindowPlacement(hWnd, wp) Then
            Return wp.ShowCmd = SW_SHOWMINIMIZED OrElse wp.ShowCmd = SW_HIDE
        End If

        Return False

End Function

Private Function IsSpecialProcess(ByVal process As Process) As Boolean

        Dim processName As String = process.ProcessName.ToLowerInvariant()
        Return processName = "applicationframehost" OrElse processName = "textinputhost" OrElse processName = "systemsettings"
End Function
 
Last edited:
Try this, for me this lists all open File Explorer folders, both separate windows and multiple tabs on Windows 11:
VB.NET:
'Add COM reference "Microsoft Internet Controls"

Dim shell_windows As New SHDocVw.ShellWindows
For Each ie As SHDocVw.InternetExplorer In shell_windows
    If Path.GetFileNameWithoutExtension(ie.FullName) = "explorer" Then
        Debug.WriteLine(ie.LocationName)
        Debug.WriteLine(ie.LocationURL)
    End If
Next
Marshal.ReleaseComObject(shell_windows)
 
Well, nothing showed up and I have 3 Windows Explorer windows open.
Just to be sure... I am wanting to add to my listbox Windows Explorer, not IE.
I am using Windows 10

VB.NET:
Dim shell_windows As New SHDocVw.ShellWindows
    For Each ie As SHDocVw.InternetExplorer In shell_windows
        If Path.GetFileNameWithoutExtension(ie.FullName) = "explorer" Then
            ListBox1.Items.Add(ie.LocationName & "-" & ie.LocationURL)
        End If
    Next
    Marshal.ReleaseComObject(shell_windows)
 
Last edited:
The code is ancient and works for all Windows versions, I was a little surprised it worked for Windows 11 and tabs also. Yes, it is for File Explorer.
What does ie.FullName read for you? I'm thinking it is possible it could be uppercase.
 
Give me a path to FileExplorer

1686594239495.png
 
So in Windows 10 it is "Explorer", not "explorer".
 
Back
Top