且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过Powershell中的进程获取所有窗口句柄?

更新时间:2021-12-23 07:30:10

首先,您应该检查一下 WASP,看看它是否适合您的需求:http://wasp.codeplex.com/

First, you should check out WASP and see if it suits your needs: http://wasp.codeplex.com/

其次,我修改了此处找到的代码 http://social.technet.microsoft.com/Forums/windowsserver/en-US/c3cd3982-ffc5-4c17-98fc-a09c555e121c/get-all-child-window-titles?forum=winserverpowershell

Secondly, I 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

创建一个函数,它将 MainWindowHandle 作为输入,并返回一个带有子句柄 ID 的对象(它还将列出任何窗口标题,如果有).

to create a function that will take a MainWindowHandle as input, and will give you an object with child handle IDs back (It will also list any window titles, if any).

我希望其中一种方法可以满足您的需求:)

I hope one of these methods will give you what you 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));
               }
               finally
               {
                   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 as List<IntPtr>;
               if (list == null)
               {
                   throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
               }
               list.Add(handle);
               //  You can modify this to check to see if you want to cancel the operation, then return a 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))
        }))
    }
}
}

您可以直接从 Get-Process 的结果中通过管道传输它,如下所示:

You can pipe it directly from the result of Get-Process, like this:

Get-Process | Where-Object {$_.ProcessName -eq 'OUTLOOK'} | Get-ChildWindow