Sign in to follow this  
Followers 0

[Solved] WinGetHandle from WinGetProcess

6 posts in this topic

Posted (edited)

I need to get handle information i can use for a window, but i only have the process id to work with. Any ideas? :)

Run("notepad.exe")
sleep(1000)
$pid = WinGetProcess("[CLASS:Notepad]")
$handle = WinGetHandle($pid, "")
ConsoleWrite($pid & @CRLF)
ConsoleWrite($handle & @CRLF)

Is an example that doesn't work but should show what i am after.

Edited by IanN1990

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

Posted

Thanks :)

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

The first, though i had to read though the forum you linked before i understood it enough to get it working ^^

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.