Monday 6 May 2019

WinList.exe list the open windows and their child windows' Window Title, Window Class, and the EXE file

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
Window Manipulation Posts

REM WinList.bat
REM This file compiles WinList.vb to WinList.exe
REM WinList.exe list the open windows and their child windows' Window Title, Window Class, and the EXE file that created the window. 
REM To use type WinList in a command prompt
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\WinList.vb" /out:"%~dp0\WinList.exe" /target:exe
Pause
---------------------------------------------------------------------------------
'WinList.vb
imports System.Runtime.InteropServices 
Public Module WinList  

Public Declare Function GetTopWindow Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal wCmd As Integer) As IntPtr
Public Declare UNICODE Function GetWindowModuleFileNameW Lib "user32" (ByVal hwnd As IntPtr, ByVal WinModule As String, StringLength As Integer) As Integer
Public Declare UNICODE Function GetWindowTextW Lib "user32" (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Integer) As Integer
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As IntPtr, ByRef lpdwProcessId As IntPtr) As IntPtr
Public Declare UNICODE Function GetClassNameW Lib "user32" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Public Declare Function IsWindowUnicode Lib "user32" (ByVal hwnd As IntPtr) As Boolean
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
  
Public Sub Main ()
 Dim WindowChain as Integer
     WindowChain = 0 
 Dim hwnd As IntPtr
 hwnd = GetTopWindow(0)
 If hwnd <> 0 Then
              AddChildWindows(hwnd, 0)
         End If
End Sub


Private Sub AddChildWindows(ByVal hwndParent As IntPtr, ByVal Level As Integer) 
 Dim objWMIService As Object
 Dim colItems As Object
 Dim TempStr As String 
 Dim WT As String, CN As String, Length As Integer, hwnd As IntPtr, TID As IntPtr, PID As IntPtr, MN As String, Parenthwnd As IntPtr
        Static Order As Integer
        Static FirstTime As Integer
        Parenthwnd = hwndParent
        If Level = 0 Then
                        hwnd = hwndParent
        Else
            hwnd = GetWindow(hwndParent, GW_CHILD)
        End If
        Do While hwnd <> 0
                 WT = Space(512)
                  Length = GetWindowTextW(hwnd, WT, 508)
                  WT = Left$(WT, Length)
                  If WT = "" Then WT = Chr(171) & "No Window Text" & Chr(187)
                  CN = Space(512)
                  Length = GetClassNameW(hwnd, CN, 508)
                  CN = Left$(CN, Length)
                  If CN = "" Then CN = "Error=" & Err.LastDllError
                  MN = ""
                  
                  TID = GetWindowThreadProcessId(hwnd, PID)
                  
 objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 colItems = objWMIService.ExecQuery("Select * From Win32_Process where ProcessID=" & CStr(PID))
 For Each objItem in colItems 
  MN = objItem.name 
 Next                    
                Dim Unicode  as Boolean
 Unicode = IsWindowUnicode(hwnd)

                  Order = Order + 1
                If FirstTime = 0 Then
                    Console.writeline("Window Text                             " & "Class Name                       " & vbTab & "Unicode" & vbtab & "HWnd" & vbTab & "ParentHWnd" & vbTab & "ProcessID" & vbTab & "ThreadID" & vbTab & "Process Name" )
                    FirstTime = 1
                End If
 TempStr = vbCrLf & Space(Level * 3) & WT 
 If 30 - len(TempStr) > -1 then
  TempStr = TempStr & space(40 - len(TempStr))
 End If
 TempStr = TempStr & " " & CN 
 If 55 - len(TempStr) > -1 then
  TempStr = TempStr & space(75 - len(TempStr))
 End If
                Console.write(TempStr  & vbtab & Unicode & vbTab & CStr(hwnd) & vbTab & CStr(Parenthwnd) & vbTab & CStr(PID) & vbTab & CStr(TID) & vbTab & MN )
                 
                  AddChildWindows(hwnd, Level + 1)
                  hwnd = GetWindow(hwnd, GW_HWNDNEXT)
        Loop
      End Sub

End Module 

No comments:

Post a Comment