Jump to content

Recommended Posts

Posted (edited)

So you want to get the window handle of a PID? You have a couple of options...

1. Use the following function below.

2. In WinAPIEx search for the function _WinAPI_EnumProcessWindows() (Recommended!)

This function is by SmOke_N but I've misplaced the forum post from which it came from.

Func _WinGetByPID($iPID, $iArray = 1) ; 0 Will Return 1 Base Array & 1 Will Return The First Window.
    Local $aError[1] = [0], $aWinList, $sReturn
    If IsString($iPID) Then
        $iPID = ProcessExists($iPID)
    EndIf
    $aWinList = WinList()
    For $A = 1 To $aWinList[0][0]
        If WinGetProcess($aWinList[$A][1]) = $iPID And BitAND(WinGetState($aWinList[$A][1]), 2) Then
            If $iArray Then
                Return $aWinList[$A][1]
            EndIf
            $sReturn &= $aWinList[$A][1] & Chr(1)
        EndIf
    Next
    If $sReturn Then
        Return StringSplit(StringTrimRight($sReturn, 1), Chr(1))
    EndIf
    Return SetError(1, 0, $aError)
EndFunc   ;==>_WinGetByPID

Edit: Found it >>

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

You're welcome. Which option did you go with?

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted (edited)

Sorry about that, here is an example for those that come across this thread in the future.

I also found a problem where the handle wasn't being returned but instead the title of the window.

Example()

Func Example()
    ; Run Notepad
    Local $iPID = Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    Local $hNotepad = WinWait("[CLASS:Notepad]", "", 10)

    ; Wait for 2 seconds to display the Notepad window.
    Sleep(2000)

    ; Retrieve the handle using the PID of returned by Run.
    Local $hWnd = _WinGetByPID($iPID, 1)
    MsgBox(4096, "", "_WinGetByPID hWnd: " & $hWnd & @CRLF & "WinWait hWnd: " & $hNotepad)

    ; Close the Notepad window using the classname of Notepad.
    WinClose("[CLASS:Notepad]")
EndFunc   ;==>Example

Func _WinGetByPID($iPID, $iArray = 1) ; 0 Will Return 1 Base Array & 1 Will Return The First Window.
    Local $aError[1] = [0], $aWinList, $sReturn
    If IsString($iPID) Then
        $iPID = ProcessExists($iPID)
    EndIf
    $aWinList = WinList()
    For $A = 1 To $aWinList[0][0]
        If WinGetProcess($aWinList[$A][1]) = $iPID And BitAND(WinGetState($aWinList[$A][1]), 2) Then
            If $iArray Then
                Return $aWinList[$A][1]
            EndIf
            $sReturn &= $aWinList[$A][1] & Chr(1)
        EndIf
    Next
    If $sReturn Then
        Return StringSplit(StringTrimRight($sReturn, 1), Chr(1))
    EndIf
    Return SetError(1, 0, $aError)
EndFunc   ;==>_WinGetByPID
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

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