spudw2k Posted September 21, 2009 Share Posted September 21, 2009 (edited) I started developing a cmdline tool to interact with and manipulate Window Forms Controls. I thought this was a useful func so I figured I share. expandcollapse popup$hwnd = _GetHwnd("explorer.exe") msgbox(0,"",$hwnd) Func _GetHwnd($id,$txt="") ;Retrieve Hwnd of process $proc = 0 If _IsPIDOrProc($id) Then $proc = _PIDOrProcToHwnd($id) ElseIf _IsWinTitle($id,$txt) Then $proc = _WinTitleToHwnd($id,$txt) EndIf Return $proc EndFunc Func _IsPIDOrProc(ByRef $id) ;Is running PID or Processname If Not ProcessExists($id) Then Return 0 Else Return $id EndIf EndFunc Func _IsWinTitle(ByRef $id,$txt="") ;Is running Window Title $win = WinGetTitle($id,$txt) If Not $win Then Return 0 $id = $win Return 1 EndFunc Func _IsWinVisible($handle) ;Is Window Visible If BitAnd( WinGetState($handle), 2 ) Then Return 1 Else Return 0 EndIf EndFunc Func _PIDOrProcToHwnd($proc) ;Convert PID or process to Hwnd If ProcessExists($proc) <> $proc Then $proclist = ProcessList($proc) $proc = $proclist[1][1] EndIf $var = WinList() For $i = 1 to $var[0][0] ;Pair PID/Process with Window Title If $var[$i][0] <> "" AND _IsWinVisible($var[$i][1]) Then If WinGetProcess($var[$i][0]) = $proc Then $proc = WinGetHandle($var[$i][0]) EndIf Next Return $proc EndFunc Func _WinTitleToHwnd($proc,$txt="") ;Convert Window title to Hwnd $winlist = WinList($proc,$txt) If Not $winlist[0][0] Then Return -1 Return $winlist[1][1] EndFunc edit: added txt param Edited September 21, 2009 by spudw2k Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX BuilderMisc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retreive SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose ArrayProjects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalcCool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
Yashied Posted September 21, 2009 Share Posted September 21, 2009 Nice, but what if the process has a few windows? 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...
spudw2k Posted September 21, 2009 Author Share Posted September 21, 2009 Nice, but what if the process has a few windows?Better hope you get the right one. Good question and point. I guess specifying the window name would be the only way in that case. Spoiler Things I've Made: Always On Top Tool ◊ AU History ◊ Deck of Cards ◊ HideIt ◊ ICU ◊ Icon Freezer ◊ Ipod Ejector ◊ Junos Configuration Explorer ◊ Link Downloader ◊ MD5 Folder Enumerator ◊ PassGen ◊ Ping Tool ◊ Quick NIC ◊ Read OCR ◊ RemoteIT ◊ SchTasksGui ◊ SpyCam ◊ System Scan Report Tool ◊ System UpTime ◊ Transparency Machine ◊ VMWare ESX BuilderMisc Code Snippets: ADODB Example ◊ CheckHover ◊ Detect SafeMode ◊ DynEnumArray ◊ GetNetStatData ◊ HashArray ◊ IsBetweenDates ◊ Local Admins ◊ Make Choice ◊ Recursive File List ◊ Remove Sizebox Style ◊ Retrieve PNPDeviceID ◊ Retreive SysListView32 Contents ◊ Set IE Homepage ◊ Tickle Expired Password ◊ Transpose ArrayProjects: Drive Space Usage GUI ◊ LEDkIT ◊ Plasma_kIt ◊ Scan Engine Builder ◊ SpeeDBurner ◊ SubnetCalcCool Stuff: AutoItObject UDF ◊ Extract Icon From Proc ◊ GuiCtrlFontRotate ◊ Hex Edit Funcs ◊ Run binary ◊ Service_UDF Link to comment Share on other sites More sharing options...
demandnothing Posted September 25, 2009 Share Posted September 25, 2009 Better hope you get the right one. Good question and point. I guess specifying the window name would be the only way in that case.thanks a lot man.. i just dont know if it returned properly.. i got 0x003900F8 << is that a correct response? Link to comment Share on other sites More sharing options...
bo8ster Posted September 25, 2009 Share Posted September 25, 2009 You have the handle there. PIDs are just numbers, Task Manager will give you pids, you just have to enable to column. Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic] Link to comment Share on other sites More sharing options...
Yashied Posted October 1, 2009 Share Posted October 1, 2009 (edited) Returns all windows (Handles) for the specified process. expandcollapse popup#Include <Array.au3> #Include <WinAPI.au3> Opt('MustDeclareVars', 1) Global $__Data, $aWnd = _WinAPI_EnumProcessWindows(ProcessExists('explorer.exe'), False) If IsArray($aWnd) Then _ArrayDisplay($aWnd, '_WinAPI_EnumProcessWindows') EndIf Func _WinAPI_EnumProcessThreads($PID) If Not $PID Then $PID = _WinAPI_GetCurrentProcessID() If Not $PID Then Return SetError(1, 0, 0) EndIf EndIf Local $hSnapshot = DllCall('kernel32.dll', 'ptr', 'CreateToolhelp32Snapshot', 'dword', 0x00000004, 'dword', 0) If (@error) Or ($hSnapshot[0] = 0) Then Return SetError(1, 0, 0) EndIf Local $tTHREADENTRY32 = DllStructCreate('dword Size;dword Usage;dword ThreadID;dword OwnerProcessID;long BasePri;long DeltaPri;dword Flags') Local $pTHREADENTRY32 = DllStructGetPtr($tTHREADENTRY32) Local $Ret, $Result[101] = [0] $hSnapshot = $hSnapshot[0] DllStructSetData($tTHREADENTRY32, 'Size', DllStructGetSize($tTHREADENTRY32)) $Ret = DllCall('kernel32.dll', 'int', 'Thread32First', 'ptr', $hSnapshot, 'ptr', $pTHREADENTRY32) While (Not @error) And ($Ret[0]) If DllStructGetData($tTHREADENTRY32, 'OwnerProcessID') = $PID Then $Result[0] += 1 If $Result[0] > UBound($Result) - 1 Then ReDim $Result[$Result[0] + 100] EndIf $Result[$Result[0]] = DllStructGetData($tTHREADENTRY32, 'ThreadID') EndIf $Ret = DllCall('kernel32.dll', 'int', 'Thread32Next', 'ptr', $hSnapshot, 'ptr', $pTHREADENTRY32) WEnd _WinAPI_CloseHandle($hSnapshot) If $Result[0] Then ReDim $Result[$Result[0] + 1] Else Return SetError(1, 0, 0) EndIf Return $Result EndFunc ;==>_WinAPI_EnumProcessThreads Func _WinAPI_EnumProcessWindows($PID, $fVisible = 1) Local $Threads = _WinAPI_EnumProcessThreads($PID) If @error Then Return SetError(1, 0, 0) EndIf Local $hEnumProc = DllCallbackRegister('__EnumWindowsProc','int','hwnd;int') Local $Error = 1 Dim $__Data[101][2] = [[0]] For $i = 1 To $Threads[0] DllCall('user32.dll', 'int', 'EnumThreadWindows', 'dword', $Threads[$i], 'ptr', DllCallbackGetPtr($hEnumProc), 'int', $fVisible) If @error Then $__Data = 0 ExitLoop EndIf Next If IsArray($__Data) Then $Error = 0 If $__Data[0][0] Then ReDim $__Data[$__Data[0][0] + 1][2] Else $__Data = 0 EndIf EndIf DllCallbackFree($hEnumProc) Return SetError($Error, 0, $__Data) EndFunc ;==>_WinAPI_EnumProcessWindows Func __EnumWindowsProc($hWnd, $fVisible) If ($fVisible) And (Not BitAND(WinGetState($hWnd), 2)) Then Return 1 EndIf $__Data[0][0] += 1 If $__Data[0][0] > UBound($__Data) - 1 Then ReDim $__Data[$__Data[0][0] + 100][2] EndIf $__Data[$__Data[0][0]][0] = $hWnd $__Data[$__Data[0][0]][1] = _WinAPI_GetClassName($hWnd) Return 1 EndFunc ;==>__EnumWindowsProc Edited October 1, 2009 by Yashied 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...
Shafayat Posted October 2, 2009 Share Posted October 2, 2009 Very nice. Thanks for sharing. [Not using this account any more. Using "iShafayet" instead] Link to comment Share on other sites More sharing options...
ddeerr Posted November 5, 2009 Share Posted November 5, 2009 (edited) Thanks, exactly what i need.The result look to list all handles for all objects inside the specified process, i have a question is it possible to isolate and get only the handle of the principal window ?e.g : with firefox.exe the class of the principal window is MozillaUIWindowClassMaybe it is already available in you library but i don't understand Dll* fonctions i simply use basics function.I make it work like that :#Include <Array.au3> #include "U:\_DSA\AutoIt\devs\zzzz_DEV_ssss cipe 18.08.09\zzz_shared_libs_sss\WinAPIEx\WinAPIEx.au3" Opt("WinTitleMatchMode", 4) Global $processName = "notepad.exe", $winClass = "Notepad", $result Global $aWnd = _WinAPI_EnumProcessWindows($processName) If IsArray($aWnd) Then $result = _ArraySearch($aWnd, $winClass, 0,0,0,1,0,1) $result = $aWnd[$result][0] MsgBox(0,"Text",WinGetText($result)) EndIf Edited November 5, 2009 by ddeerr 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