Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C++
Article

Utility to display information on all windows in z-order and their children.

Rate me:
Please Sign up or sign in to vote.
4.58/5 (13 votes)
6 Jan 20031 min read 77.9K   2.9K   39   5
Prints a text tree showing all windows in system, along with information about each window.

Sample Image - windump.gif

Introduction

This console program uses the GetTopWindow and GetNextWindow calls recursively to iterate through the entire tree of windows in the system. The program outputs information on these windows in a text based tree view. For each window, it shows the title, the registered class, the handle of the owner process, the coordinates, and whether or not the window is visible.

Using the code

The main work of the program is done in the PrintChildrenWindows function. This steps through the z-order, listing all windows, and recursively steps into the list of child windows for each window it visits. The output is a text tree that looks something like this:

133114 "windump - Microsoft Visual C++ - [windump.cpp]" 
                                 "Afx:400000:8:10011:0:10505" 892
|  (-4, -4)-(1028, 744)  VISIBLE
|
+-133110 "" "Afx:400000:0:10011:10:0" 892
|    (0, 723)-(1024, 740)  VISIBLE
|
+-133118 "" "Afx:400000:8" 892
| |  (0, 19)-(1024, 106)  VISIBLE
| |
| +-67628 "" "Afx:400000:b:10011:10:0" 892
| |    (367, 77)-(655, 107)  VISIBLE
| |
| +-67626 "" "Afx:400000:b:10011:10:0" 892
| |    (607, 47)-(733, 77)  VISIBLE
| |
| +-67614 "" "Afx:400000:b:10011:10:0" 892
| | |  (-1, 47)-(607, 77)  VISIBLE
| | |
| | +-67616 "" "AfxWnd42" 892
| | |    (193, 51)-(230, 73)  VISIBLE
| | |
...

The first line displays the handle, the title, the class name, and the handle of the owner process. The second line displays the coordinates (left, top)-(right, bottom), and whether or not the window is currently visible.

// recursive algorithm
// prints top level window, then tree of children windows
// calling with hwnd == NULL, level == 0 starts from top of z-order
void PrintChildrenWindows(HWND hwnd, int level)
{
    hwnd = GetTopWindow(hwnd);

    if(!hwnd)
        return;

    while(hwnd)
    {
        char levelSpace[1024];
        char nameBuffer[WINBUFF_SIZE];
        char typeBuffer[WINBUFF_SIZE];

        GetWindowText(hwnd, nameBuffer, WINBUFF_SIZE);
        RealGetWindowClassA (hwnd, typeBuffer, WINBUFF_SIZE);

        GenerateTreeString(levelSpace,    // buffer
            level,    // depth
            // are there additional siblings
            GetNextWindow(hwnd, GW_HWNDNEXT) ? true : false,
            GetTopWindow(hwnd) ? true : false,    // are there children?
            false);    // no fall through

        printf("%s%d \"%s\" \"%s\" %d\n",levelSpace, hwnd, 
          nameBuffer, typeBuffer, GetWindowThreadProcessId(hwnd, NULL));

        GenerateTreeString(levelSpace,    // buffer
            level,    // depth
            // are there more siblnigs
            GetNextWindow(hwnd, GW_HWNDNEXT) ? true : false, 
            GetTopWindow(hwnd) ? true : false, // are there chidren
            true);    // fall through

        RECT rect;
        GetWindowRect(hwnd, &rect);
        printf("%s (%d, %d)-(%d, %d)", levelSpace, 
           rect.left, rect.top, rect.right, rect.bottom);

        if(IsWindowVisible(hwnd))
            printf("  VISIBLE\n");
        else
            printf("  HIDDEN\n");

        printf("%s\n", levelSpace);

        PrintChildrenWindows(hwnd, level + 1);
        hwnd = GetNextWindow(hwnd, GW_HWNDNEXT);
    }
}

This is the recursive function which prints the window information. At the top level, hwnd is NULL. Calling GetTopWindow(NULL) gets the top window in the z-order. It then loops, calling hwnd = GetNextWindow(hwnd), to retrieve each successive window in the z-order. Also, it calls itself for each window, with hwnd equal to the window just printed. This causes the loop to process all child windows in the same way as GetTopWindow(hwnd /*!= NULL*/) gets the first child window of hwnd. If there are no child windows, GetTopWindow returns NULL, and we return, unwinding the recursion.

Points of interest

This program also contains a function called isFlagInArgs which could easily be extracted and used in other command line utilities to interpret command line arguments.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I was born and raised in Dayton, Ohio. I acquired an NCR PC Model 4 when I was 8, and was writing fairly complex GW-BASIC programs by the time I was ten. Educated formally at Wright State University, I now work for an undisclosed company doing platform level work with Linux, Windows, and, sadly still, DOS (Why the &%^& won't [name withheld] %^&%$%# upgrade already?!).


Comments and Discussions

 
QuestionThank you very much!!! Pin
comminche2-Nov-11 21:35
comminche2-Nov-11 21:35 
GeneralThanks! Pin
Rydi12-May-09 2:00
Rydi12-May-09 2:00 
GeneralConsole app to list hidden processes$ Pin
jwburton21-Apr-06 15:50
jwburton21-Apr-06 15:50 
GeneralTHANK YOU Pin
Axel__4-Apr-06 1:17
Axel__4-Apr-06 1:17 
GeneralVery nice! Pin
Anonymous7-Jan-03 7:01
Anonymous7-Jan-03 7:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.