You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
Announcement
Collapse
No announcement yet.
How to find top-level window if I only have the Process ID?
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?
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.
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
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.
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).]
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment