Sign in to follow this  
Followers 0
BPM

Find window handle with pid?

6 posts in this topic

I was wondering if there was a way to find a window's handle by using the process ID.

I know I can find it with the windows name, but my problem is I have multipul windows with the same name.

I want to be abel to activate a window by using its process ID

For example,

If Not WinActive($Handle) Then WinActivate($Handle)

How could I get $Handle from just the process ID? ($pid)

So far the only way Ive seen in the documentation to find a windows handle involves the windows name.

WinGetHandle("Title")

Some help would be greatly appriciated. Thank you

Share this post


Link to post
Share on other sites



well try if is detected someting with that name to rename it as "the name+1" and if it detects othere to rename it as "the name+2" i made something like that but have deleated it and i can't think now how it was cuz i need to go to sleep soon :D hope i helped you alitle bit :D


Have Questions About GUI (Graphical User Interface) ? Post Them Here :GUI Help And Support ForumHave Questions About General AutoIt ? Post Them Here : General Help And Support ForumNew To AutoIt ? Be Shure To Check Out The FaQ's (Frequently Asked Questions) Or FaQ ¹ There You May Find Great Help That Will Guide You True The Wonderful Programming Language AutoItOthere Good Place To Get Some Knolage Of AutoIt Is The Example Script ForumNotice A Bug ? Please Go And Report it At Bug Report Section And Help The Devolepers Of AutoIt Update And Fix The Programming LanguageWant To Thank The People For This Great Forum And Programming Language ? Then DonateWhen You Found The Answer Your Looking For Please Add [Resolved] To The Thread's Name That Will Show Otheres That You Have Found What Your Looking For And They Whount Have To Enter The Thread.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Similar question in MSDN magazine a while ago deal with this generically-

http://msdn2.microsoft.com/en-us/magazine/cc301495.aspx.

It would probably be possible to use the dll functions mentioned there to do this.

A native autoit solution might be to use WinGetProcess() and then using advanced window descriptions to enumerate though each iteration of the window until you get one that matches the PID you already have.

Edited by evilertoaster

Share this post


Link to post
Share on other sites

There's got to be an easy way to do this.

I think the Dll call function has something to do with it, I saw this code within Nomad's memory UDF's:

Func _MemoryOpen($iv_Pid, $iv_DesiredAccess = 0x1F0FFF, $iv_InheritHandle = 1)
    
    Local $ah_Handle[2] = [DllOpen('kernel32.dll')]
    
    Local $av_OpenProcess = DllCall($ah_Handle[0], 'int', 'OpenProcess', 'int', $iv_DesiredAccess, 'int', $iv_InheritHandle, 'int', $iv_Pid)
    
    $ah_Handle[1] = $av_OpenProcess[0]
    
    Return $ah_Handle
    
EndFunc

This function returns "an array containing the Dll handle and an open handle to the specified process."

But it seems to return the dll handle aswell, and I just want the handle of the process. I haven't ventured into dll functions yet, so I wouldnt know how to modify this code. Maybe this helps though?

Share this post


Link to post
Share on other sites

Here's an example, in this case i have 5 notepads open, they all have the same title. I know that the PID of one is 4072-

$handle=_GetPIDWindows(4072)
if IsHWnd($handle) then
    WinKill($handle)
EndIf

func _GetPIDWindows($PID)
    $WinList=WinList()
    for $i=1 to $WinList[0][0]
        if WinGetProcess($WinList[$i][1])=$PID then 
            Return $WinList[$i][1]
        EndIf
    Next
    Return 0
EndFunc

Share this post


Link to post
Share on other sites

Awesome, I got it to work!

Looks like the window list function was the ticket

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