joseLB Posted December 26, 2008 Share Posted December 26, 2008 (edited) DearsI'm in a trouble. How to convert PID to window handle....Wen I run a program ->Run ( "filename" )<-, I receive a PID as result. I only have the "filename" , no known title, class, etc., as this "filename" is passed as the only parameter thru $commandline.Immediatedly I need to wait up to that program to be running... and later I need to use _ScreenCapture_CaptureWnd, where the window Handle is required.So, from PID I need to go to hwnd... Any suggestion? I searched all help, but was unable to find a way.Thanks in advanceJose Edited December 26, 2008 by joseLB Link to comment Share on other sites More sharing options...
monoceres Posted December 26, 2008 Share Posted December 26, 2008 Hi! Use WinList() to get all the windows, you can then use WinGetProcess() to compare the windows to your pid you see which windows your pid owns Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
BrettF Posted December 26, 2008 Share Posted December 26, 2008 I seem to answer this question every other day... Credit goes to the original author. Who I forget now... Func _GetHwndFromPID($PID) $hWnd = 0 $stPID = DllStructCreate("int") Do $winlist2 = WinList() For $i = 1 To $winlist2[0][0] If $winlist2[$i][0] <> "" Then DllCall("user32.dll", "int", "GetWindowThreadProcessId", "hwnd", $winlist2[$i][1], "ptr", DllStructGetPtr($stPID)) If DllStructGetData($stPID, 1) = $PID Then $hWnd = $winlist2[$i][1] ExitLoop EndIf EndIf Next Sleep(100) Until $hWnd <> 0 Return $hWnd EndFunc ;==>_GetHwndFromPID Cheers, Brett Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
joseLB Posted December 26, 2008 Author Share Posted December 26, 2008 Thanks Brett and monoceres, you did my day! but how complex, uh! I think that when you run a program, all that information is avaiable, without need for a loop. If yes, here is my suggestion: RUN function (and all related ones), to have 1 more parameter: how will be the return. Default (0) is like today, just pid. Value 1 would return an array with the most possible parameters of the created process. Or, value 1= array with a group of parameters, 2=array with another group of parameters, etc. Jose Link to comment Share on other sites More sharing options...
Subhobroto Posted December 26, 2008 Share Posted December 26, 2008 Hi, Here is a snippet from my program that helps me automate Telnet sessions using PuTTY. I would be releasing that app here, but for now, here is something useful from there: expandcollapse popup#cs Busy wait on window having windowTitle and belonging to process with PID processPID. Time out timeoutSeconds in seconds Returns -1 if timed out #ce Func BusyWaitOnWindow($processPID, $windowTitle, $timeoutSeconds) Local $multiplierValue = 1000; Local $timeOut = $timeoutSeconds; Local $puttyHWND = -1; While ((0 <> $timeOut) AND (-1 == $puttyHWND)) ; Find out the program by finding programs that have this title ; check if the PID of that window matches the PID of the spawned process ; if so, we have found our window ; NOTE: Winwait will return (prematurely) if a window with same title exists Winwait($windowTitle); If multiple instances of this script is spawned extremely fast (2 or more scripts per second), it gets hung here because the title changes to something unexpected (window title resembles log filename) ; I believe this has to do with the way ControlSendPlus.au3 modifies the global key state, thus effectively changing the sequence of commands sent versus what this script is supposed to generate Local $matchingWindows = WinList($windowTitle) If 0 == $matchingWindows[0][0] Then MsgBox(0, "Details", "NO matching window titles! Will continue busy wait for " & $timeOut & " more seconds..", 1) $timeOut -= 1; Sleep($multiplierValue); ContinueLoop EndIf For $dx = 1 to $matchingWindows[0][0] If WinGetProcess($matchingWindows[$dx][1]) == $processPID Then $puttyHWND = $matchingWindows[$dx][1]; ExitLoop EndIf Next WEnd If NOT IsHWnd($puttyHWND) Then MsgBox(4096, "", $PuttyHWND & " - It's not a HWND") $puttyHWND = -1; EndIf return $puttyHWND; EndFunc Link to comment Share on other sites More sharing options...
bobsyuruncle Posted February 11, 2010 Share Posted February 11, 2010 I seem to answer this question every other day... Credit goes to the original author. Who I forget now... Func _GetHwndFromPID($PID) $hWnd = 0 $stPID = DllStructCreate("int") Do $winlist2 = WinList() For $i = 1 To $winlist2[0][0] If $winlist2[$i][0] <> "" Then DllCall("user32.dll", "int", "GetWindowThreadProcessId", "hwnd", $winlist2[$i][1], "ptr", DllStructGetPtr($stPID)) If DllStructGetData($stPID, 1) = $PID Then $hWnd = $winlist2[$i][1] ExitLoop EndIf EndIf Next Sleep(100) Until $hWnd <> 0 Return $hWnd EndFunc ;==>_GetHwndFromPID Cheers, Brett I vote for a function like this or this functionality to be added to one of the current autoit functions... or enable the other functions to work off of PID as well as the hwnd for finding windows. It makes sense to be able to simply run $pid = run(bla app) and then winactivate($pid) Link to comment Share on other sites More sharing options...
Yashied Posted February 11, 2010 Share Posted February 11, 2010 #Include <Array.au3> #Include <WinAPIEx.au3> Opt('MustDeclareVars', 1) Global $Data = _WinAPI_EnumProcessWindows(ProcessExists('SciTE.exe')) If IsArray($Data) Then _ArrayDisplay($Data, '_WinAPI_EnumProcessWindows') EndIfWinAPIEx.au3 Decibel and MONaH-Rasta 2 My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
BrettF Posted February 11, 2010 Share Posted February 11, 2010 #Include <Array.au3> #Include <WinAPIEx.au3> Opt('MustDeclareVars', 1) Global $Data = _WinAPI_EnumProcessWindows(ProcessExists('SciTE.exe')) If IsArray($Data) Then _ArrayDisplay($Data, '_WinAPI_EnumProcessWindows') EndIf WinAPIEx.au3 Ooo dats just lovely! Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
jvanegmond Posted February 11, 2010 Share Posted February 11, 2010 Brett, also instead of using GetWindowThreadProcessID we now have WinGetProcess. github.com/jvanegmond Link to comment Share on other sites More sharing options...
BrettF Posted February 12, 2010 Share Posted February 12, 2010 Oh no way. I should really read the change log Apart from that it was posted late 2008... Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Cyri Posted April 20, 2011 Share Posted April 20, 2011 #Include <Array.au3> #Include <WinAPIEx.au3> Opt('MustDeclareVars', 1) Global $Data = _WinAPI_EnumProcessWindows(ProcessExists('SciTE.exe')) If IsArray($Data) Then _ArrayDisplay($Data, '_WinAPI_EnumProcessWindows') EndIf WinAPIEx.au3 Will this method work for child process windows? So in your example, If SciTE.exe spawned a child process that also has windows...would this method enum the windows from the child process? I'm thinking that it wouldn't and I would have to create a loop to enum the child process windows, but I haven't had a chance to play with it yet. Note that I'm talking about a child process and not a child window. Link to comment Share on other sites More sharing options...
jaylweb Posted August 19, 2011 Share Posted August 19, 2011 I spent a lot of time testing this. I have a command line tool that when executed, opens a command line window. I cannot get the handle using the pid for my executable. I can get the pid with the WinGetHandle function. I was hoping I could get the handle using the pid, this way I could run multiple instances of the executable and control the one I want specifically. For now, I launch it using Run, then get the handle using the title, then rename the title to something completely different. _WinAPI_EnumProcessWindows definitely does not work for a non-window process. That being said, I don't think it would work for a child process either. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now