Files
ClassHomeworkBoard/test-ppt.ps1
T
Henry_Du c7fdc11dbd
Azure Static Web Apps CI/CD / Close Pull Request Job (push) Skipped
Build and Deploy / build (push) Failing after 9m48s
Azure Static Web Apps CI/CD / Build and Deploy Job (push) Failing after 9m50s
首次提交
2026-10-24 01:29:24 +08:00

56 lines
1.9 KiB
PowerShell

Add-Type @"
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
public class Win32Helper {
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount);
public static List<IntPtr> GetProcessWindows(uint processId) {
List<IntPtr> windows = new List<IntPtr>();
EnumWindows(delegate(IntPtr hWnd, IntPtr lParam) {
uint id;
GetWindowThreadProcessId(hWnd, out id);
if (id == processId && IsWindowVisible(hWnd) && GetWindowTextLength(hWnd) > 0) {
windows.Add(hWnd);
}
return true;
}, IntPtr.Zero);
return windows;
}
public static string GetWindowTextStr(IntPtr hWnd) {
int length = GetWindowTextLength(hWnd);
if (length == 0) return "";
System.Text.StringBuilder builder = new System.Text.StringBuilder(length + 1);
GetWindowText(hWnd, builder, builder.Capacity);
return builder.ToString();
}
}
"@
$ppts = Get-Process -Name "wpp", "POWERPNT", "wps" -ErrorAction SilentlyContinue
foreach ($p in $ppts) {
Write-Host "Process $($p.Id) $($p.ProcessName)"
$hwnds = [Win32Helper]::GetProcessWindows($p.Id)
foreach ($hwnd in $hwnds) {
$title = [Win32Helper]::GetWindowTextStr($hwnd)
Write-Host " HWND: $hwnd, Title: $title"
}
}