Re: OpenProcess Identify Handle

From: Tom Esh (tjeshGibberish_at_earthlink.net)
Date: 04/21/04


Date: Wed, 21 Apr 2004 11:23:14 -0400

See inline.

On Tue, 20 Apr 2004 23:06:02 -0700, "Keith"
<anonymous@discussions.microsoft.com> wrote:

>No confusion about this, I'm getting the correct PID, and I know the difference. The closed remard is when I was trying to find the hwnd from a call to OpenProcess, where all the ample code comes with big warnings about closing the process later.

Then I guess you know that OpenProcess returns a ~process~ handle for
a given PID, and not a hwnd (?) If it helps, here's a snip that finds
any MediaPlayer (ver 8) top-level window handle regardless of its
caption / titlebar text:

        Dim hwndMP As Long
        hwndMP = FindWindow("WMPlayerApp", vbNullString)

Then to determine if that particular instance belongs to a specific
process PID, you can use GetWindowThreadProcessId:

        Dim pidThis As Long
        GetWindowThreadProcessId hwndMP, pidThis
        If pidThis = PID Then 'it matches
                SetWindowPos hwndMP, ...whatever
        End If

>Though as a confessed newbie, there's a lot about API that I'm confused about. It's getting the hwnd from this PID value that I'm having trouble with.
There is no Api function to enumerate windows for a given process, so
you have to work through the hwnds and look for a PID match.
GetWindowThreadProcessId is useful here.
        Public Declare Function GetWindowThreadProcessId Lib "user32" _
                (ByVal hwnd As Long, lpdwProcessId As Any) As Long

>That I'm using the correct PID is confirmed by looking at the Task Manager and Spy++. Generally today I found it to have a value between 600 and 2500. It's the return value from the Shell function I'm using to open the mplayer2 Windows.
>
>The problem is when I use this value either though the OpenProcess or FindWindow method, as per the 2 samples I looked at, the values I get are wrong. ( I assume with OpenProcess I'm getting a new handle that references the base one)
Yes, each call to OpenProcess returns a proc handle unique to the
calling context. However typically you don't need a proc handle unless
you intend to monitor or wait for the process to finish.

>Björn Holmgren posted some code in the ShellExecute thread of 4/13/2004 that I though would do the trick, using FindWindow and then looping though the PIDs - but no such luck - It may be the parent/child problem, but - when I plug the hwnd value from the rpiWinSpy - which uses the same trick as Spy++ of drag drop a marker to identify the window then uses WindowFromPoint to get the hwnd - then I can do the moving and positioning. I was using Shell, not ShellExecuteEx, which also has a function call in Björn's sample, so I'll try that next.
>
>Is is possible to set where a window will open using an API calls? This would also be a solution.
Yes, but only if the app cooperates. You could use CreateProcess to
start the app and specify the startup location in the STARTUPINFO
structure and including the STARTF_USEPOSITION flag. However this will
only work if the app actually uses it and does not for example stash &
restore the last positon. Since using the last position is common,
you're better off finding and moving the window after it starts.

-Tom
MVP - Visual Basic
(please post replies to the newsgroup)



Relevant Pages

  • Re: OpenProcess Identify Handle
    ... >This is probably a pretty newbie question - I'm having some trouble with getting the handle to the media player windows using OpenProcess. ... I have the correct PID, ... SetWindowPos requires a hwnd. ...
    (microsoft.public.vb.winapi)
  • Re: Window -> PID
    ... the X server knows the pid for each client in order to do the ... |I am familiar with the most kernel structures (actually i am digging ... |pid> and moving mouse over that window produce a massive ... no way to get it out other than via poking in the X server process ...
    (comp.unix.solaris)
  • Re: Window -> PID
    ... Is there a way I can get it somehow digging into ... the X server knows the pid for each client in order to do the ... with adb into crash files many times), ... pid> and moving mouse over that window produce a massive ...
    (comp.unix.solaris)
  • Re: Fenster mit Fokus finden
    ... brauche ich die PID des Prozesses. ... static Window Select_Window; ...
    (de.comp.os.unix.x11)
  • Re: OpenProcess Identify Handle
    ... SetWindowPos requires a hwnd. ... Sounds like you're confusing the process ... handle (PID) with the window handle. ... The problem is when I use this value either though the OpenProcess or FindWindow method, as per the 2 samples I looked at, the values I get are wrong. ...
    (microsoft.public.vb.winapi)

Loading