Thread: Need Help With EnumDesktopWindows And More..

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Need Help With EnumDesktopWindows And More..

    I'm trying to get a list of all the handles of all the windows that are running on my computer. It's giving an "access violation" error and I'm not surprised since I'm in way over my head. Not like that's stopped me before.

    Here's the code:

    Code:
    #include <string> 
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <cstdlib>
    #include <windows.h>
    
    
    using namespace std;
    
    int main(){
    
    	HWND hwnd = 0; // probably initializing this wrong
    	DWORD dwThreadId = GetCurrentThreadId(); // Get the current thread id
    	HDESK hDesktop = GetThreadDesktop(dwThreadId); // Assign the desktop thread id to hDesktop
    	LPARAM lParam(0); // almost definitely initializing this wrong
    	WNDENUMPROC lpfn(0); // same for this
    	string windowName = " ";
    
    
    	cout << "--------------\n";
    	cout << "Swarm 1.0.0\n";
    	cout << "by Nemesis\n";
    	cout << "--------------\n";
    	cout << "--------------\n";
    
    	cout << "Searching For Windows... ";
    	
    
    	EnumDesktopWindows(hDesktop, EnumWindowsProc(), lParam);
    
    	/*if(){ // wanted some way to check or list enumeration, but ended up commenting this out for now
    	cout << "Enumeration successful.";
    	}else{ cout << "Enumeration unsuccessful.";}*/
    	
    	system("PAUSE");
            return 0;
    
    }
    Last edited by ArchNemesis; 04-27-2010 at 02:45 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Code:
    EnumDesktopWindows(hDesktop, EnumWindowsProc(), lParam);
    EnumDesktopWindows takes a pointer to a function as its second argument. The template (not an actual C++ template) function for this is:

    Code:
    BOOL CALLBACK EnumWindowsProc(
      __in  HWND hwnd,
      __in  LPARAM lParam
    );
    (Source: EnumWindowsProc Function (Windows))

    In other words, you must write a function (with whatever name you want) that returns type BOOL and takes a HWND and an LPARAM as arguments. I'm pretty sure that CALLBACK is just a define/typedef (can calling conventions be typedef'd?) for the calling convention, you're going to want it in there anyway. You then pass this function to EnumDesktopWindows.

    For example:
    Code:
    BOOL CALLBACK MyEnumProc( HWND wnd, LPARAM lParam )
    {
            //do something with window here
    }
    
    int main( void )
    {
            EnumDesktopWindows( GetDesktopWindow(), MyEnumProc, 0 );
    
            return 0;
    }
    Note that in my experience with EnumDesktopWindows, it only returns windows that are direct descendants of the desktop window.

    This should probably be in the Windows Programming section.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    Ok, so are you saying there's a better method to use if I want to get all the handles of the windows on my computer? And thanks for the reply, btw.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I needed to populate a treeview with all the window handles once and using GetWindow in a recursive function fit the bill perfectly. It may be somewhat harder to do without a treeview, but maybe you could use a simple linked list or something.

    For the recursive function, I wrote it so that if the window had a child, it would 'recurse' with the child window as the new parameter. If it didn't have children (or the child window had returned) then it would 'recurse' with the sybling window as the new parameter.

    It can probably be done other ways, but if you can't think of one, this way worked nicely.

Popular pages Recent additions subscribe to a feed