4

In Windows, how can I view a list of all the titles of all Windows that are open.

(Preferably with some unique identifier like handle)

1
  • 2
    There's a Windows API function to do that called EnumWindows() that has the core functionality required. I know of no existing program to do that, but you could probably write one in your favorite scripting language -- I personally prefer Python, which could certainly do something like this. VisualBasic probably could, too, as well as a number of others.
    – martineau
    Commented Nov 17, 2013 at 11:10

2 Answers 2

5

Use the EnumWindows() Windows API function. It will invoke the application-defined callback function argument passed to it for all the top-level windows on the screen, passing it the handle of each one, until the callback returns FALSE.

Here's a simple Python 2.x console program I wrote that uses that function (and a few others to determine which windows might actually be visible on the desktop -- many of the "top-level" windows thatEnumWindows()enumerates over are invisible -- to accomplish what you want. It makes use of the win32guimodule included in the PyWin32 extension package to gain access to the Windows API. This can also be done at a lower, more direct, level using the built-inctypesmodule, but PyWin32 is a more advanced and convenient way to go about it, IMO.

You could redirect the output to a text file or the program could be modified to display the list in a window or dialog box using several different GUI modules available, including the tk/tcl interface module calledTkinter, which comes standard with the language.

import sys
import win32gui

def callback(hwnd, strings):
    if win32gui.IsWindowVisible(hwnd):
        window_title = win32gui.GetWindowText(hwnd)
        left, top, right, bottom = win32gui.GetWindowRect(hwnd)
        if window_title and right-left and bottom-top:
            strings.append('0x{:08x}: "{}"'.format(hwnd, window_title))
    return True

def main():
    win_list = []  # list of strings containing win handles and window titles
    win32gui.EnumWindows(callback, win_list)  # populate list

    for window in win_list:  # print results
        print window

    sys.exit(0)

if __name__ == '__main__':
    main()
0
3

I took the PowerShell script from Enumerating all Windows-windows with a PowerShell function and a callback function and modified it slightly.

It print the window name and handle.

<#
 .Synopsis
 Enumerieren der vorhandenen Fenster
#>

$TypeDef = @"

using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Api
{

 public class WinStruct
 {
   public string WinTitle {get; set; }
   public int WinHwnd { get; set; }
 }

 public class ApiDef
 {
   private delegate bool CallBackPtr(int hwnd, int lParam);
   private static CallBackPtr callBackPtr = Callback;
   private static List<WinStruct> _WinStructList = new List<WinStruct>();

   [DllImport("User32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   private static extern bool EnumWindows(CallBackPtr lpEnumFunc, IntPtr lParam);

   [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
   static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

   private static bool Callback(int hWnd, int lparam)
   {
       StringBuilder sb = new StringBuilder(256);
       int res = GetWindowText((IntPtr)hWnd, sb, 256);
      _WinStructList.Add(new WinStruct { WinHwnd = hWnd, WinTitle = sb.ToString() });
       return true;
   }   

   public static List<WinStruct> GetWindows()
   {
      _WinStructList = new List<WinStruct>();
      EnumWindows(callBackPtr, IntPtr.Zero);
      return _WinStructList;
   }

 }
}
"@

Add-Type -TypeDefinition $TypeDef -Language CSharpVersion3

[Api.Apidef]::GetWindows() | Where-Object { $_.WinTitle -ne "" } | Sort-Object -Property WinTitle | Select-Object WinTitle,@{Name="Handle"; Expression={"{0:X0}" -f $_.WinHwnd}}
2
  • Have you read the section about copyright on the site's imprint? It forbids copying, modifying and distributing all contents. You should at least consider giving fair attribution to the author.
    – ComFreek
    Commented Nov 29, 2013 at 16:25
  • @ComFreek: More fair than linking to the authors site? Commented Nov 29, 2013 at 17:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .