Finding all open windows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrPickle
    New Member
    • Jul 2008
    • 100

    Finding all open windows

    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?

    Code:
    #include <windows.h>
    #include <iostream>
    
    int i;
    
    BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);
    
    int main(int argc, char** argv) {
    	std::cout << "Finding all open windows\n";
    	if(EnumWindows(EnumWindowsProc, 0)) {
    		std::cout << i << " windows are open\nCall was successful...\n" << std::endl;
    		std::cin.get();
    	} else {
    		std::cout << "Call was unsuccessful...\n" << std::endl;
    		std::cin.get();
    	}
    	
    	return 0;
    }
    
    BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) {
    	i++;
    	return true;
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    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.

    Comment

    • MrPickle
      New Member
      • Jul 2008
      • 100

      #3
      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 :)

      Comment

      Working...