Finding the first HWND belonging to ProcessID

This is a discussion on Finding the first HWND belonging to ProcessID within the Windows Programming forums, part of the Platform Specific Boards category; I want to get the first HWND owned by a ProcessID. Everything runs but it always shows the dialog box ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    15

    Finding the first HWND belonging to ProcessID

    I want to get the first HWND owned by a ProcessID. Everything runs but it always shows the dialog box saying "No windows found" but i know it is there since i can see on on the screen.
    (I find it hard to debug in windows programs since i cant put printf everywhere, how is printf debugging done while writing windows programs?)

    Code:
    //Global variables
    DWORD processID = NULL;
    DWORD tempID = NULL;
    HWND myHWND = NULL;
    
    
    BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lparam)
    {
    	//Getting the ProcessID for that the HWND belongs to
    	tempID=GetWindowThreadProcessId(hwnd,NULL);
            //If the ID is the same as my process id
    	if (tempID == processID){
                    //Take this hwnd as the one i want
    		myHWND = hwnd;
    		//Quit the Enum when the first window is found
    		return (FALSE);
    	}
            //Otherwise run again
    	return (TRUE);
    }
    
    --------------------------------------------------------
    This code runs when i hit a button
    //Returns processID from functions that first runs CreateProcess and then GetProcessID and returns it
    processID = startup();
    //Iterate every windows to find the first one that belongs to my process
    EnumWindows(&EnumWindowsProc,NULL);
    //If the global variabel have recived a value the IF expression got executed in EnumWindowProc
    if (myHWND != NULL)
          //Visual confirmation of my the HWND
          SetDlgItemInt(hwnd, IDC_NUMBER, (DWORD)myHWND, FALSE);
    else
          //Found no windows for this process
           MessageBox(hwnd, "No windows found!", "Warning", MB_OK);

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    15
    Is my code understandable?

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Why do you need to do this?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    15
    What my program does it that it starts an external exe and gets the hwnd for the window, then im going to get the DC for this window so that i can do stuff with it.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Two problems:

    1. GetWindowThreadProcessID returns the thread ID. To get the process ID you need to use the second parameter. GetWindowThreadProcessID(hwnd, &tempID)

    2. If you enumerate the windows right after creating the process, the window may not have been created yet. Try Sleep(100) before enumerating.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    15
    That did the trick! Ty mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get an unkown hWnd
    By Xzyx987X in forum Windows Programming
    Replies: 1
    Last Post: 11-26-2003, 02:29 PM
  2. hWnd in MFC
    By WebmasterMattD in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2003, 07:06 AM
  3. HDC and HWND
    By conright in forum Windows Programming
    Replies: 1
    Last Post: 01-04-2003, 04:36 PM
  4. Getting a HWND
    By bonkey in forum Windows Programming
    Replies: 2
    Last Post: 11-01-2002, 09:18 AM
  5. Hwnd
    By _BiG_HeaD_ in forum Windows Programming
    Replies: 1
    Last Post: 06-25-2002, 03:03 PM

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