Jump to content

Get window(s) handle if you have PID


Recommended Posts

Func _WinGetHandleByPID($iPID)
    If Not IsNumber($iPID) Then
        SetError(1)
        Return 0
    EndIf
    If $iPID < 0 Then
        SetError(2)
        Return 0
    EndIf
    Local $wList = WinList()
    Local $hWnd[1][2] = [[0], [0]]
    For $i = 1 To $wList[0][0]
        If WinGetProcess($wList[$i][1]) = $iPID Then
            $hWnd[0][0] = UBound($hWnd, 1)
            ReDim $hWnd[UBound($hWnd, 1)+1][2]
            $hWnd[UBound($hWnd, 1)-1][0] = $wList[$i][0]
            $hWnd[UBound($hWnd, 1)-1][1] = $wList[$i][1]
        EndIf
    Next
    Return $hWnd
EndFunc

Is it best way to obtain window(s) handle if you just have PID?

I usually use Run() and then want to ControlSend() it, so I use my function.

Any other ideas?

Edited by fastnick1oo
SciTE - much better than notepad. ; ]
Link to comment
Share on other sites

This script will popup a msgbox with the Handle of the current active window.

While 1
$handle = WinGetHandle ( WinGetTitle ( "" ) )
MsgBox (4096, "handle is", $handle , 1 )
WEnd
Edited by random667

It is really sad to see a family torn apart by something as simple as a pack of wolves.

Link to comment
Share on other sites

Func _WinGetHandleByPID($iPID)
    If Not IsNumber($iPID) Then
        SetError(1)
        Return 0
    EndIf
    If $iPID < 0 Then
        SetError(2)
        Return 0
    EndIf
    Local $wList = WinList()
    Local $hWnd[1][2] = [[0], [0]]
    For $i = 1 To $wList[0][0]
        If WinGetProcess($wList[$i][1]) = $iPID Then
            $hWnd[0][0] = UBound($hWnd, 1)
            ReDim $hWnd[UBound($hWnd, 1)+1][2]
            $hWnd[UBound($hWnd, 1)-1][0] = $wList[$i][0]
            $hWnd[UBound($hWnd, 1)-1][1] = $wList[$i][1]
        EndIf
    Next
    Return $hWnd
EndFunc

Is it best way to obtain window(s) handle if you just have PID?

I usually use Run() and then want to ControlSend() it, so I use my function.

Any other ideas?

This is best. No more ideas needed.

Lar.

f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Link to comment
Share on other sites

  • Moderators

There is an error here: Local $hWnd[1][2] = [[0], [0]]

This is nice if you know the PID... a couple of things.

Redimming constantly if there are a large amount of windows for that PID open, can be a bit slow.

If you take a look at this way (a bit longer), but you can throw in the name of the executable (mind you, this one will get z order, I usually use ProcessList and go through 2 loops (1 for the win's and 1 for the processes)), or the PID.

You have a few options as well, if want all windows visible or not... -1 if you want just non visible ones 0, or if you want only visible ones (default) 1.

#include <array.au3>
$a1 = _WinGetHandleByPID(232)
$a2 = _WinGetHandleByPID("notepad.exe", -1)
$a3 = _WinGetHandleByPID("notepad.exe", 0)
$a4 = _WinGetHandleByPID("notepad.exe", 1)
_ArrayDisplay($a1, "1")
_ArrayDisplay($a2, "2")
_ArrayDisplay($a3, "3")
_ArrayDisplay($a4, "4")

;$nVisible = -1 "All (Visble or not)", $nVisible = 0 "Not Visible Only", $nVisible = 1 "Visible Only"
Func _WinGetHandleByPID($vProc, $nVisible = 1)
    $vProc = ProcessExists($vProc);
    If Not $vProc Then Return SetError(1, 0, 0)
    Local $aWL = WinList()
    Local $aTemp[UBound($aWL)][2], $nAdd = 0
    For $iCC = 1 To $aWL[0][0]
        If $nVisible = -1 And WinGetProcess($aWL[$iCC][1]) = $vProc Then
            $nAdd += 1
            $aTemp[$nAdd][0] = $aWL[$iCC][0]
            $aTemp[$nAdd][1] = $aWL[$iCC][1]
        ElseIf $nVisible = 0 And WinGetProcess($aWL[$iCC][1]) = $vProc And _
                BitAND(WinGetState($aWL[$iCC][1]), 2) = 0 Then
            $nAdd += 1
            $aTemp[$nAdd][0] = $aWL[$iCC][0]
            $aTemp[$nAdd][1] = $aWL[$iCC][1]
        ElseIf $nVisible > 0 And WinGetProcess($aWL[$iCC][1]) = $vProc And _
                BitAND(WinGetState($aWL[$iCC][1]), 2) Then
            $nAdd += 1
            $aTemp[$nAdd][0] = $aWL[$iCC][0]
            $aTemp[$nAdd][1] = $aWL[$iCC][1]
        EndIf
    Next
    If $nAdd = 0 Then Return SetError(2, 0, 0);No windows found
    ReDim $aTemp[$nAdd + 1][2]
    $aTemp[0][0] = $nAdd
    Return $aTemp
EndFunc
A bit longer, but more options, and possibly faster if there are a lot of windows open for that PID

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...