Hung up on GetWindowText

This is a discussion on Hung up on GetWindowText within the Windows Programming forums, part of the Platform Specific Boards category; I've got it setup to just grab the process list without worrying about window captions. I think this will suit ...

  1. #16
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    I've got it setup to just grab the process list without worrying about window captions. I think this will suit my purpose, at least for now.

    Thanks

  2. #17
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by Viper187 View Post
    Can't I do this somehow with EnumProcesses? I can't find any decent examples of EnumWindows/EnumWindowsProc that just list them all. Do you simply check if the HWND != NULL in order to determine when to return TRUE/FALSE or what?
    Give this a try...

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    BOOL CALLBACK GetWindowFromProcessID(HWND hWnd, LPARAM lParam)
    {
        static HWND hWnd2Find;
        static DWORD dwMatchProcessID;
    
        if(lParam != 0){
            dwMatchProcessID = (DWORD)hWnd;
            if(EnumWindows(GetWindowFromProcessID, 0))
                return FALSE;
            *(HWND *)lParam = hWnd2Find;
        }
        else{
            DWORD dwPID = 0;
            GetWindowThreadProcessId(hWnd, &dwPID);
            if(dwMatchProcessID == dwPID)
            {
                hWnd2Find = hWnd;
                return FALSE;
            }
        }
        return TRUE;
    }
    
    int main(void)
    {
        HWND hWnd;
        DWORD dwPID = 3268;
        char szWindowTitle[500] = {0};
    
        if(GetWindowFromProcessID((HWND)dwPID, (DWORD)&hWnd))
        {
            GetWindowText(hWnd, szWindowTitle, sizeof(szWindowTitle)-1);
            printf("Window title is %s\n", szWindowTitle );   
        }
        else printf("No Window found\n");
        return 0;
    }

  3. #18
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Wow, That's pretty slick. However, it seems to be grabbing a random window instead of the parent/root window of the process. Any ideas for isolating it with this method? Is it just a matter of using/looping GetParent() or something?

  4. #19
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    However, it seems to be grabbing a random window instead of the parent/root window of the process. Any ideas for isolating it with this method? Is it just a matter of using/looping GetParent() or something?
    The original code sample assumes that the first window found is the window of interest. This works fine with apps such as notepad. But it isn't appropriate for MDI apps such as MS Word etc. So, a modified solution would be to enumerate all windows for a particular process ID as follows:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    void ListChildWindows(HWND hWnd, DWORD dwPIDCheck)
    {
        DWORD dwPID;    
        char szBuffer[1000] = {0};
        GetWindowThreadProcessId(hWnd, &dwPID);
        GetWindowText(hWnd, szBuffer, 1000);
        if(dwPID == dwPIDCheck)
            printf("%-8X %-40s %-8d\n", hWnd, szBuffer,dwPID);
        for(hWnd = GetWindow(hWnd, GW_CHILD); hWnd != NULL; hWnd = GetWindow(hWnd, GW_HWNDNEXT))
            ListChildWindows(hWnd, dwPIDCheck);
    }
    
    int main(int argc, char **argv)
    {
        HWND hDesktopWindow = NULL;
        if(argc == 1)
        {
            printf("Invalid  arguments\n");
            return 0;
        }
        hDesktopWindow = GetDesktopWindow();
        ListChildWindows(hDesktopWindow, atoi(argv[1]));
        return 0;
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetWindowText() not working with Vista???
    By Abda92 in forum Windows Programming
    Replies: 10
    Last Post: 04-07-2008, 02:29 PM
  2. GetwindowText problem
    By spanker in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2008, 11:25 AM
  3. GetWindowText doesn't return the text
    By Rune Hunter in forum Windows Programming
    Replies: 16
    Last Post: 09-23-2005, 05:49 PM
  4. GetWindowText -> string?
    By XSquared in forum Windows Programming
    Replies: 3
    Last Post: 08-27-2003, 04:58 PM
  5. Using GetWindowText
    By ColdFire in forum Windows Programming
    Replies: 9
    Last Post: 05-26-2002, 11:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21