Well, well try, If that API was supposed to work as per your understanding there are possible risks being caught in an infinite loop or referencing a destroyed window handle. Call Back functions of an Windows API are not that supposed to be called by you.
struct MYLISTVIEW
{
};
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
#define MAX_TEXT_LEN 256
MYLISTVIEW *plvData = (MYLISTVIEW *)lParam;
if (plvData)
{
TCHAR szWndText[MAX_TEXT_LEN] = _T("");
LONG lStyle = GetWindowLong(hwnd, GWL\_STYLE);
LONG lExStyle = GetWindowLong(hwnd, GWL\_EXSTYLE);
// filter the window handles depending on your need
if (IsWindowVisible(hwnd) && !GetParent(hwnd) && hwnd != GetDesktopWindow() && (
/\*lExStyle & WS\_EX\_APPWINDOW || \*/
!(lExStyle & WS\_EX\_NOACTIVATE || lExStyle & WS\_EX\_TOOLWINDOW)))
{
RECT rc = {0};
GetWindowRect(hwnd, &rc);
if (rc.bottom - rc.top && rc.right - rc.left)
{
GetWindowText(hwnd, szWndText, MAX\_TEXT\_LEN);
// add the text to your view
AddWndTextToListView(plvData, szWndText);
}
}
}
return TRUE;
}
MYLISTVIEW g_lvData;
void AddOpenedWindowsToListView()
{
EnumWindows(EnumWindowsProc, (LPARAM)&g_lvData);
}