Connecting Tech Pros Worldwide Forums | Help | Site Map

Finding all open windows

MrPickle's Avatar
Member
 
Join Date: Jul 2008
Posts: 98
#1: Jan 6 '09
I am trying to get all open windows on windows. I have looked around and seen that I should use EnumWindows and EnumWindowsProc so I whipped up this code to test it but it tells me there's 300-400 windows open, and I only have about 5?

Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. int i;
  5.  
  6. BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);
  7.  
  8. int main(int argc, char** argv) {
  9.     std::cout << "Finding all open windows\n";
  10.     if(EnumWindows(EnumWindowsProc, 0)) {
  11.         std::cout << i << " windows are open\nCall was successful...\n" << std::endl;
  12.         std::cin.get();
  13.     } else {
  14.         std::cout << "Call was unsuccessful...\n" << std::endl;
  15.         std::cin.get();
  16.     }
  17.  
  18.     return 0;
  19. }
  20.  
  21. BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
  22.     i++;
  23.     return true;
  24. }

Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#2: Jan 6 '09

re: Finding all open windows


Caveat: I'm not a Windows programmer; and I've never used EnumWindows.

1. I suggest setting variable "i" to zero just before calling EnumWindows. It won't fix your problem, but it is a good habit to get into.

2. Could EnumWindows be revisiting the same windows? The way to check that is to accumulate a list of the window handles passed to EnumWindowProc (hWnd) and check if there are any duplicates.
MrPickle's Avatar
Member
 
Join Date: Jul 2008
Posts: 98
#3: Jan 7 '09

re: Finding all open windows


After adding the check, I think it may be getting hidden programs too because outputting the program's text gives allot of blanks and things like my laptop's battery life.

Thanks :)
Reply