Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when EnumWindows finishes its listing of windows?

How to know when EnumWindows finishes its listing of windows? Because EnumWindows receives a callback function as parameter, and it keeps calling it until no more windows to be listed.

like image 559
jondinham Avatar asked Aug 30 '11 22:08

jondinham


1 Answers

EnumWindows() blocks while enumeration is taking place. When EnumWindows() finishes enumerating through the windows, it returns a BOOL.

The following code snippet:

#include <windows.h>
#include <cstdio>

BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam)
{
    int& i = *(reinterpret_cast<int*>(lparam));
    ++i;
    char title[256];
    ::GetWindowText(hwnd, title, sizeof(title));
    ::printf("Window #%d (%x): %s\n", i, hwnd, title);
    return TRUE;
}

int main()
{
    int i = 0;
    ::printf("Starting EnumWindows()\n");
    ::EnumWindows(&MyEnumWindowsProc, reinterpret_cast<LPARAM>(&i));
    ::printf("EnumWindows() ended\n");
    return 0;
}

gives me an output like this:

Starting EnumWindows()
Window #1 (<hwnd>): <title>
Window #2 (<hwnd>): <title>
Window #3 (<hwnd>): <title>
<and so on...>
EnumWindows() ended

So EnumWindows() definitely enumerates in a synchronous manner.

like image 166
In silico Avatar answered Sep 28 '22 03:09

In silico