I am trying to print a list of running applications like alt-tab would give me. Here are what I have done so far:
1.In the beginning I tried EnumWindows, but I got hundreds of entries.
2.I found some similar questions and they led me to the blog of Raymond Chen.
However it still shows more than 100 windows(window_num1 being 158 and window_num2 being 329), while alt-tab would give me only 4. What did I do wrong? Here is my code:
using namespace std;
#pragma comment(lib, "user32.lib")
BOOL IsAltTabWindow(HWND hwnd)
if (hwnd == GetShellWindow()) //Desktop
// Start at the root owner
HWND hwndWalk = GetAncestor(hwnd, GA_ROOTOWNER);
// See if we are the last active visible popup
while ((hwndTry = GetLastActivePopup(hwndWalk)) != hwndTry)
hwndWalk = hwndTry;
return hwndWalk == hwnd;
BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
GetWindowText(hWnd, title, sizeof(title)/sizeof(title));
_tprintf(_T("Value is %s\n"), title);
//strTitle += title; // Convert to std::string
windowHandle = hWnd;
void MyFunc(void) //(called by main)
You failure is, that you should walk only visible windows... read the blog again.
For each visible window, walk up its owner chain until you find the root owner. Then walk back down the visible last active popup chain until you find a visible window. If you're back to where you're started, then put the window in the Alt+↹Tab list.
Your code walks over every window!