Announcement

Collapse
No announcement yet.

How to find top-level window if I only have the Process ID?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to find top-level window if I only have the Process ID?

    I need to launch a program, look through the process list to find its Process ID, and then - somehow - get a handle to it's parent window... does anybody know which APIs to call to do this?


    ------------------
    -

  • #2
    If the program is a windows program i would expect it
    to take the foreground. Couldnt you just use GetForegroundWindow?

    Regards
    Peter

    ------------------
    [email protected]
    www.dreammodel.dk

    Comment


    • #3
      Wayne --

      I think you want GetWindowThreadProcessId.

      -- Eric


      ------------------
      Perfect Sync Development Tools
      Perfect Sync Web Site
      Contact Us: mailto:[email protected][email protected]</A>
      "Not my circus, not my monkeys."

      Comment


      • #4
        JFYI, the SHELL function returns a Process ID, but in rare cases, this may be zero even when the application is successfully launched. In this case, you would need to enumerate the process list.

        Note that SHELL always sets ERR/ERRCLEAR is the SHELL operation fails.


        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Peter, you could be right but that sounds a bit risky, and may not work under all circumstances

          Eric, I thought GetWindowThreadProcessID was for determining the ProcessID of a window handle? I need to determine the window handle of a Process ID, so the opposite of the GetWindowThreadProcessID

          For example, just say I launch c:\winnt\system32\calc.exe, and my program looks through and finds that calc.exe is ProcessID 200. I then need to be able to do things with that window such as change the title of the window with SetWindowText, but I need a handle on the window first


          ------------------
          -

          Comment


          • #6
            Wayne --

            I don't believe a "backwards" API exists. But...

            If you have the Process ID, you could use the EnumWindows API to enumerate all of the top-level windows that currently exist. As they are enumerated, use GetWindowThreadProcessId to find the process ID of each window. If it matches the process ID that you are looking for, you have your answer. In spite of what the API docs say, however, EnumWindows returns more than just top-level windows, so you may want to use the GetParent API to "climb the tree" until you find a top-level window.

            However, all of that assumes that the target application has a single top-level window. If it has more than one you may have to play games with GetWindowText to figure out which window is which.

            -- Eric


            ------------------
            Perfect Sync Development Tools
            Perfect Sync Web Site
            Contact Us: mailto:[email protected][email protected]</A>



            [This message has been edited by Eric Pearson (edited June 06, 2001).]
            "Not my circus, not my monkeys."

            Comment


            • #7
              wayne

              i think we discussed something quite similar recently. try http://www.powerbasic.com/support/pb...ead.php?t=3742

              hope this helps
              keith

              ------------------

              Comment


              • #8
                Woohoo! Perfect
                Keith, May 15 - no wonder it wasnt in POFFS2
                This code from the very end of your aforementioned thread solves my problem superbly! (although I had to put in a delay to ensure the process had time to load - no dramas):
                Code:
                GLOBAL g_tempDword AS DWORD 
                 
                FUNCTION CheckForProcess (BYVAL hWnd AS LONG, BYVAL procID AS LONG) AS LONG
                  LOCAL wProcID AS LONG 
                  CALL GetWindowThreadProcessId (hWnd, wProcID) 
                  IF wProcID = procID THEN           'If window of process
                    IF %NULL = GetParent(hWnd) THEN  'If has no parent
                      g_tempDword = hWnd             'Note window handle
                      FUNCTION = %FALSE              'Stop enumerating
                      EXIT FUNCTION
                    END IF
                  END IF
                  FUNCTION = %TRUE                   'Continue enumerating
                END FUNCTION 
                 
                FUNCTION ProcessMainWindow (BYVAL procId AS LONG) AS LONG
                  g_tempDword = %NULL
                  CALL EnumWindows (CODEPTR(CheckForProcess), procId)
                  FUNCTION = g_tempDword
                END FUNCTION
                This will come in very handy for me, Ill be using it time and time again now
                I can soldier on with my program now, thanks very much everyone, special thanks to Semen and Keith for their hard work cracking this problem



                [This message has been edited by Wayne Diamond (edited June 06, 2001).]
                -

                Comment

                Working...
                X