How can I get all window handles by a process in Powershell? -
i have script
get-process | where-object {$_.mainwindowtitle -like "*total*"}
which yield information
handles npm(k) pm(k) ws(k) vm(m) cpu(s) id processname ------- ------ ----- ----- ----- ------ -- ----------- 362 23 19432 32744 324 3.86 6880 totalcmd64
so have process id.
the *total*
application has many open windows of own.
question
how can iterate ( using powershell) through windows ( can window handle) ?
nb : goal ?
:
looking (for example ) @ visual studio : have application running.
application has own open window .
i want sub window topmost. already have script make window topmost. need handle number.
first, should check out wasp , see if suits needs: http://wasp.codeplex.com/
secondly, have modified code found here http://social.technet.microsoft.com/forums/windowsserver/en-us/c3cd3982-ffc5-4c17-98fc-a09c555e121c/get-all-child-window-titles?forum=winserverpowershell
to create function take mainwindowhandle input, , give object child handle ids (it list window titles, if any).
i hope 1 of these methods give need :)
function get-childwindow{ [cmdletbinding()] param ( [parameter(valuefrompipeline = $true, valuefrompipelinebypropertyname = $true)] [validatenotnullorempty()] [system.intptr]$mainwindowhandle ) begin{ function get-windowname($hwnd) { $len = [apifuncs]::getwindowtextlength($hwnd) if($len -gt 0){ $sb = new-object text.stringbuilder -argumentlist ($len + 1) $rtnlen = [apifuncs]::getwindowtext($hwnd,$sb,$sb.capacity) $sb.tostring() } } if (("apifuncs" -as [type]) -eq $null){ add-type @" using system; using system.runtime.interopservices; using system.collections.generic; using system.text; public class apifuncs { [dllimport("user32.dll", charset = charset.auto, setlasterror = true)] public static extern int getwindowtext(intptr hwnd,stringbuilder lpstring, int cch); [dllimport("user32.dll", setlasterror=true, charset=charset.auto)] public static extern intptr getforegroundwindow(); [dllimport("user32.dll", setlasterror=true, charset=charset.auto)] public static extern int32 getwindowthreadprocessid(intptr hwnd,out int32 lpdwprocessid); [dllimport("user32.dll", setlasterror=true, charset=charset.auto)] public static extern int32 getwindowtextlength(intptr hwnd); [dllimport("user32")] [return: marshalas(unmanagedtype.bool)] public static extern bool enumchildwindows(intptr window, enumwindowproc callback, intptr i); public static list<intptr> getchildwindows(intptr parent) { list<intptr> result = new list<intptr>(); gchandle listhandle = gchandle.alloc(result); try { enumwindowproc childproc = new enumwindowproc(enumwindow); enumchildwindows(parent, childproc,gchandle.tointptr(listhandle)); } { if (listhandle.isallocated) listhandle.free(); } return result; } private static bool enumwindow(intptr handle, intptr pointer) { gchandle gch = gchandle.fromintptr(pointer); list<intptr> list = gch.target list<intptr>; if (list == null) { throw new invalidcastexception("gchandle target not cast list<intptr>"); } list.add(handle); // can modify check see if want cancel operation, return null here return true; } public delegate bool enumwindowproc(intptr hwnd, intptr parameter); } "@ } } process{ foreach ($child in ([apifuncs]::getchildwindows($mainwindowhandle))){ write-output (,([pscustomobject] @{ mainwindowhandle = $mainwindowhandle childid = $child childtitle = (get-windowname($child)) })) } } }
you can pipe directly result of get-process, this:
get-process | where-object {$_.processname -eq 'outlook'} | get-childwindow
Comments
Post a Comment