MoShe
MoShe

Reputation: 6427

get window title from pid

I am looking for a way to get the window title from process id.

I would like to build function the get the pid of specific window and return its window title.

I try to use AutoIt but it didn't work.

Any Idea?

Upvotes: 2

Views: 8476

Answers (2)

Matt
Matt

Reputation: 7160

This has been done many times in AutoIt before, with the option to return all windows belonging to the process, instead of just the main one.

This post provides the standard solution. In case the rot spreads:

;0 will return 1 base array; leaving it 1 will return the first visible window it finds
Func _WinGetByPID($iPID, $nArray = 1)
    If IsString($iPID) Then $iPID = ProcessExists($iPID)
    Local $aWList = WinList(), $sHold

    For $iCC = 1 To $aWList[0][0]
        If WinGetProcess($aWList[$iCC][1]) = $iPID And _
            BitAND(WinGetState($aWList[$iCC][1]), 2) = 0 Then
            If $nArray Then Return $aWList[$iCC][0]
            $sHold &= $aWList[$iCC][0] & Chr(1)
        EndIf
    Next

    If $sHold Then Return StringSplit(StringTrimRight($sHold, 1), Chr(1))
    Return SetError(1, 0, 0)
EndFunc

Upvotes: 4

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

This should be quite simple:

Process.GetProcessById(processId).MainWindowTitle;

And if you like it as a function as you requested:

public string GetWindowTitle(int processId){
  return Process.GetProcessById(processId).MainWindowTitle;
}

Upvotes: 10

Related Questions