How can I get EnumWindows to list all windows?
It's (as I assumed) not a problem with EnumWindows
at all. The problem is with the output stream.
While debugging, I noticed that enumWindowsProc
is called just fine for every window, but that some iterations are simply not generating output.
For the time being, I switched to using _tprintf
, but I don't understand what the problem with the original code is. Calling wcout.flush()
had no desirable effect either.
Well, wcout.flush()
never works, however wcout.clear()
fixes your code, at least for me.
wcout << hWnd << TEXT( ": " ) << windowTitle << std::endl;
wcout.clear();
return TRUE;
And I know that this question is already one year old, however it's never too late to answer.
Here is a callback function that lists all open windows:
#include <string>
#include <iostream>
#include <Windows.h>
static BOOL CALLBACK enumWindowCallback(HWND hWnd, LPARAM lparam) {
int length = GetWindowTextLength(hWnd);
char* buffer = new char[length + 1];
GetWindowText(hWnd, buffer, length + 1);
std::string windowTitle(buffer);
// List visible windows with a non-empty title
if (IsWindowVisible(hWnd) && length != 0) {
std::cout << hWnd << ": " << windowTitle << std::endl;
}
return TRUE;
}
int main() {
std::cout << "Enmumerating windows..." << std::endl;
EnumWindows(enumWindowCallback, NULL);
std::cin.ignore();
return 0;
}
If you want to check if the window is minimized, you can use IsIconic()
.
See Also:
- Microsoft: EnumWindows function
- Stack Overflow: Getting a list of all open windows in c++ and storing them