Jump to content

Winhandle from PID


malu05
 Share

Recommended Posts

I used to use this function to get the Winhandle from the PID:

Func _WinGetByPID($iPID)
    Local $aWList = WinList()
    For $iCC = 1 To $aWList[0][0]
        If WinGetProcess($aWList[$iCC][1]) = $iPID And _
                BitAND(WinGetState($aWList[$iCC][1]), 2) Then
            Return $aWList[$iCC][0]
        EndIf
    Next
    Return SetError(1, 0, 0)
EndFunc   ;==>_WinGetByPID

However this function does not support if we have 2 of the same process running at the same time.

So is there a better way to get the Winhandle from the ProcessID?

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

Link to comment
Share on other sites

Your function worked perfectly for me, but I think it all depends on how you're getting the PID to start with.

$aPList = ProcessList("<procname>")
For $i = 1 To $aPList[0][0]
    ConsoleWrite(_WinGetByPid($aPList[$i][1])&@CRLF)
Next

Is outputting the right window handles even when I use on it multiple instances of the same process with same title.

To get the handle you need to change

Return $aWList[$iCC][0]

to

Return $aWList[$iCC][1]

of course, but you're probably already doing that.

So question is, how are you getting the PID's in the first place?

Link to comment
Share on other sites

Your function worked perfectly for me, but I think it all depends on how you're getting the PID to start with.

$aPList = ProcessList("<procname>")
For $i = 1 To $aPList[0][0]
    ConsoleWrite(_WinGetByPid($aPList[$i][1])&@CRLF)
NextoÝ÷ Ø(ºÚn¶Ø§Ø^®(!·§v!jwezǯz|!zr.±ê'ٮة觲֧që(~Ø^±©¦ºzË0Øljg­Ù^N¶Ø^©Ý쨺wvÚ©àyÊy­º¹ôߦ.+-÷]7ê ÷uÓÝÿr^¶(uä^¶êçÓ~X¸¬·ÝtߨÝý×_wýÊzÜ¢êìy»­Ê·ö·©®Ún·w'hx-«R¢«²Ø¨+!£«{*.ëmx-ãÈl{ayø«²ÚeiÇ¿ªê-y«­¢+Ù¥´ÀÌØíÁÁ¹ôÅÕ½Ðí¹½ÑÁ¹áÅÕ½Ðì((ììôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô(ìíPAɽÍÌíQ¡¥Ì¥ÌÑ¡U$½ÈAɽÍÌM±Ñ¥½¸(ììôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô)Õ¹ÑAɽÍÌ ¤($ÀÌØíͱ}ÁɽÍÌôU%
ÉÑ ÅÕ½ÐíM±ÐAɽÍÌÅÕ½Ðì°ÌÔÀ°ÄÀÀ¤($ÀÌØí ÕÑѽ¹|ÄôU%
Ñɱ
ÉÑ    ÕÑѽ¸ ÅÕ½Ðí½¬ÅÕ½Ðì°È°ÜÈ°ÌÐÜ°Èà¤($ÀÌØí±¥ÍÑÙ¥ÜôU%
Ñɱ
ÉÑ1¥ÍÐ ÅÕ½ÐìÅÕ½Ðì°À°À°ÌÔÀ°àÀ¤(%U%
ÑɱMÑMÑÑ ´Ä°ÀÌØíU%}=
UL¤(%AɽÍͱ½½­ÕÀ ¤(%U%MÑMÑÑ ¤(%}QÍ­É!¥  ÕÑѽ¸ ÅÕ½ÐíM±ÐAɽÍÌÅÕ½Ðì¤(%¥ÀÌØíÁɽÍÍ9HôÄQ¡¸($%U%
ÑɱMÑÑ ÀÌØí±¥ÍÑ٥ܰĤ($%}U%
Ñɱ1¥ÍÑ   ½á}MÑ
ÕÉM° ÀÌØí±¥ÍÑ٥ܰĤ($$ÀÌØíÉÉäôMÑÉ¥¹MÁ±¥Ð¡U%
ÑɱI ÀÌØí±¥ÍÑ٥ܤ°Ìäì±Ðì¨ÐìÌäì°Ä¤($$ÀÌØíAɽÍÍ%ôÀÌØíÉÉålÉt($%
½¹Í½±]É¥Ñ ÅÕ½ÐíAɽÍÌ%ôÅÕ½ÐìµÀìÀÌØíÉÉålÉt¤(%±Í($%]¡¥±Ä($$$ÀÌØíµÍôU%Ñ5Í ¤($$%M±Ð($$$%
ÍÀÌØíµÍôÀÌØíU%}Y9Q}
1=M($$$$%Ñɵ¥¹Ñ ¤($$$%
ÍÀÌØíµÍôÀÌØí    ÕÑѽ¹|Ä($$$$}U%
Ñɱ1¥ÍÑ   ½á}Ñ
½Õ¹Ð ÀÌØí±¥ÍÑ٥ܤôÀQ¡¸($$$$$$íU¹±Ñ¼±½ÑÑ¡AɽÍÌ($$$$$%Ñɵ¥¹Ñ ¤($$$$%¹%($$$$%¥U%
ÑɱI ÀÌØí±¥ÍÑ٥ܤôÅÕ½ÐìÅÕ½ÐìQ¡¸í%¹½Ñ¡¥¹¥ÌͱÑͱÐÑ¡¥ÉÍÐ¥¸Ñ¡±¥ÍÐ($$$$$%U%
ÑɱMÑÑ ÀÌØí±¥ÍÑ٥ܰĤ($$$$$%}U%
Ñɱ1¥ÍÑ   ½á}MÑ
ÕÉM° ÀÌØí±¥ÍÑ٥ܰĤ($$$$%¹%($$$$$ÀÌØíÉÉäôMÑÉ¥¹MÁ±¥Ð¡U%
ÑɱI ÀÌØí±¥ÍÑ٥ܤ°Ìäì±Ðì¨ÐìÌäì°Ä¤($$$$$ÀÌØíAɽÍÍ%ôÀÌØíÉÉålÉt($$$$%ÉÑÕɸÀ($$%¹M±Ð($$%AɽÍͱ½½­ÕÀ ¤($%]¹(%¹%)¹Õ¹ìôôÐíÑAɽÍÌ)U%±Ñ ¤((ììôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô(ìíAɽÍͱ½½­ÕÀ ¤íQ¡Õ¹¥Ñ½¸Ñ¼±½ÑÑ¡ÁɽÍÍÌÑ¡ÐµÑ Ñ¡ÀÌØíÁÁ¹(ììôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô)Õ¹AɽÍͱ½½­ÕÀ ¤($ÀÌØí±¥ÍÐôAɽÍÍ1¥ÍÐ ¤(%½ÈÀÌØí¤ôÄѼÀÌØí±¥ÍÑlÁulÁt($%¥ÀÌØí±¥ÍÑlÀÌØí¥ulÁtôÀÌØíÁÁ¹Q¡¸($$}ÉÉåMÉ  ÀÌØíÍÙ°ÀÌØí±¥ÍÑlÀÌØí¥ulÅt°À°À°À°QÉÕ¤ô´ÄQ¡¸($$$$ÀÌØíÍÙlÀÌØíMQ±½½Á½Õ¹ÑtôÀÌØí±¥ÍÑlÀÌØí¥ulÅt($$$$ÀÌØíÁ¹µô}AɽÍÍÑ]P ÀÌØí±¥ÍÑlÀÌØí¥ulÅt¤($$$%U%
ÑɱMÑÑ ÀÌØí±¥ÍÑ٥ܰ}AɽÍÍÑ1½Ñ¥½¸ ÀÌØí±¥ÍÑlÀÌØí¥ulÅt¤µÀìÅÕ½Ðì±ÐìÐìÅÕ½ÐìµÀìÀÌØíÁ¹µµÀìÅÕ½Ðì±Ðì¨ÐìÅÕ½ÐìµÀìÀÌØí±¥ÍÑlÀÌØí¥ulÅt¤($$$$ÀÌØíÁɽÍÍ9HôÀÌØíÁɽÍÍ9H¬Ä($$%¹%($%¹%(%¹áÐ(%
½¹Í½±]É¥Ñ ÅÕ½Ðí9ÕµÈ=AɽÍÍÌôÅÕ½ÐìµÀìÀÌØíÁɽÍÍ9HµÀì
I1¤($íU%
Ñɱ
ÉÑ1¥ÍÑY¥Ý%Ñ´ ÅÕ½ÐíAÑ ÅÕ½Ð찴Ĥ($ÀÌØíMQ±½½Á½Õ¹ÐôÀÌØíMQ±½½Á½Õ¹Ð¬Ä(%ͱÀ ÔÀ¤)¹Õ¹ìôôÐíAɽÍͱ½½­ÕÀ(ììôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô(ìí}AɽÍÍÑ¥ÉÍÑ]¥¹Q¥Ñ± ÀÌØíÁ¥¤(ììôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôôô)Õ¹}AɽÍÍÑ]P ÀÌØíÁ¥¤(%±½°ÀÌØíÑÍÑÁ¥(%±½°ÀÌØíÝ¥¹±¥ÍÐ($ìÐÝ¥¹½Ü±¥ÍÐ($ÀÌØíÝ¥¹±¥ÍÐô]¥¹1¥ÍÐ ¤($쥹ݡ¥ Ý¥¹½ÝÌ¼Ý¥Ñ Ñ¡¥ÌÁɽÍÌ(%½ÈÀÌØí¤ôÀQ¼ÀÌØíÝ¥¹±¥ÍÑlÁulÁt´Ä($$ÀÌØíÑÍÑÁ¥ô]¥¹ÑAɽÍÌ ÀÌØíÝ¥¹±¥ÍÑlÀÌØí¥ulÅt¤($%%ÀÌØíÑÍÑÁ¥ôÀÌØíÁ¥Q¡¸($$%IÑÕɸÀÌØíÝ¥¹±¥ÍÑlÀÌØí¥ulÁt($%¹%(%9áÐ(%IÑÕɸÀ)¹Õ¹ìôôÐí}AɽÍÍÑ]P

[center][u]WoW Machinima Tool[/u] (Tool for Machinima Artists) [/center]

Link to comment
Share on other sites

There cannot be duplicate PID numbers, only duplicate process names. And WinGetProcess() returns the numeric PID. So if you pass the integer PID to your function you cannot get the value back from another process of the same name.

There could, however, be more than one window from the same process.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

If it is the case that there are multiple windows from the same process and you wish to get them all you can do:

Func _WinGetByPid($iPID)
    Local $aWList = WinList()
    Local $aRet[1] = [0]

    For $iCC = 0 To $aWList[0][0]
        If WinGetProcess($aWList[$iCC][1]) = $iPID AND _
                BitAND(WinGetState($aWList[$iCC][1]), 2) Then
            ReDim $aRet[UBound($aRet)+1]
            $aRet[0] += 1
            $aRet[$aRet[0]] = $aWList[$iCC][0]
        EndIf
    Next
    If $aRet[0] = 0 Then
        Return SetError(1, 0, 0)
    Else
        Return $aRet
    EndIf
EndFunc

Remember to change $aRet[$aRet[0]] = $aWList[$iCC][0] to $aRet[$aRet[0]] = $aWList[$iCC][1] if you want the window handles.

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...