Thread: EnumWindows?

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question EnumWindows?

    I want to get all the open windows on the desktop. How would I use EnumWindows()? I need the code that gets all open windows and sets them in a listbox. PLEASE HELP!
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You need an EnumWindowsProc that'll get called for every window. If your storing the window names in a listbox you'll want to do something like this (this is pretty basic - you'll need to add some error checking) -

    Code:
    BOOL CALLBACK EnumWinProc(HWND hWnd,LPARAM lParam)
    {
    	HWND hListBox= (HWND)lParam;
    	CHAR lpWinName[MAX_PATH];
    
    		GetWindowText(hWnd,lpWinName,MAX_PATH);
    		if(strlen(lpWinName))
    		SendMessage(hListBox,LB_ADDSTRING,
                                      0,(LPARAM)lpWinName);
    
    		return TRUE;
    	
    }
    Then call EnumWindows with your listbox handle as the extra data from whatever event you want to trigger it -

    hListBox=GetDlgItem(hWnd,IDC_LIST1);
    EnumWindows(EnumWinProc,(LPARAM)hListBox);

    and your list box should contain the window titles. If you want to store the handles for all the windows you'll have to allocate memory in your EnumWindowsProc each time it's called, copy the handles and store the pointers somewhere (you could use LB_SETITEMDATA).
    zen

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post Thanks!

    ThankS!
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  4. #4
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post Errors with your code..Oops..

    Well, I need the created ListBox, too. cause I haven't figured it out yet. I tried:

    lBox =CreateWindow(
    "LISTBOX",
    "",
    WS_CHILD | WS_VISIBLE,
    0,
    0,
    200,
    200,
    hwnd,
    (HMENU)IDC_LIST1,
    hInst,
    NULL
    );

    BOOL CALLBACK EnumWinProc(HWND hWnd,LPARAM lParam);
    {
    lBox= (HWND)lParam;
    CHAR lpWinName[MAX_PATH];

    GetWindowText(hwnd,lpWinName,MAX_PATH);
    if(strlen(lpWinName))
    SendMessage(lBox,LB_ADDSTRING,
    0,(LPARAM)lpWinName);

    return TRUE;

    }

    lBox=GetDlgItem(hwnd,IDC_LIST1);
    EnumWindows(EnumWindowsProc,(LPARAM)lBox);

    but i get a linker error:

    c:\my documents\enmwnd.o(.text+0x236):enmwnd.cpp: undefined reference to `EnumWindowsProc__FP6HWND__l@8'
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    EnumWindows() takes the pointer to your proc function (it's name), your proc function is called 'EnumWinProc' but your EnumWindows() call contains 'EnumWindowsProc'. You'll either have to change the callback functions name or put the correct name in the EnumWindows() call.

    Also since you're creating the Listbox rather than putting it on a dialog with a resource editor, you already have it's handle from the CreateWindow() call so you shouldn't need to get it using GetDlgItem().
    zen

  6. #6
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post I still got..

    The linker error is:

    c:\my documents\enmwnd.o(.text+0x21e):enmwnd.cpp: undefined reference to `EnumWinProc__FP6HWND__l@8'

    Code:

    lBox =CreateWindow(
    "LISTBOX",
    "",
    WS_CHILD | WS_VISIBLE,
    0,
    0,
    200,
    200,
    hwnd,
    (HMENU)IDC_LIST1,
    hInst,
    NULL
    );

    BOOL CALLBACK EnumWinProc(HWND hWnd,LPARAM lParam);
    {
    lBox= (HWND)lParam;
    CHAR lpWinName[MAX_PATH];

    GetWindowText(hwnd,lpWinName,MAX_PATH);
    if(strlen(lpWinName))
    SendMessage(lBox,LB_ADDSTRING,
    0,(LPARAM)lpWinName);

    return TRUE;

    }

    EnumWindows(EnumWinProc,(LPARAM)lBox);
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EnumWindows Problem
    By Cha0sBG in forum Windows Programming
    Replies: 7
    Last Post: 06-28-2009, 07:01 PM
  2. ShellExecute() & EnumWindows()?
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 09-19-2005, 09:41 PM
  3. Window Image Capture
    By Lazy Student in forum Windows Programming
    Replies: 1
    Last Post: 02-17-2005, 08:06 AM
  4. EnumWindows + XP
    By FillYourBrain in forum Windows Programming
    Replies: 9
    Last Post: 02-16-2003, 08:44 PM
  5. KEN Please answer Q about EnumWindows!
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 11-05-2001, 07:02 PM