1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
$MethodDefinition = @' [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern int GetWindowText(IntPtr hWnd, string strText, int maxCount); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern int GetClassName(IntPtr hWnd, string lpClassName, int nMaxCount); // Delegate to filter which windows to include. Required by EnumWindows() public delegate bool EnumWindowsProc(IntPtr hwnd, System.Collections.ArrayList lParam); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, System.Collections.ArrayList lParam); // These two functions are in C# because I can't figure out how to use the .NET API callback functionality via PowerShell public static System.Collections.ArrayList GetWindows() { System.Collections.ArrayList windowHandles = new System.Collections.ArrayList(); EnumWindowsProc callBackPtr = GetWindowHandle; EnumWindows(callBackPtr, windowHandles); return windowHandles; } private static bool GetWindowHandle(IntPtr hwnd, System.Collections.ArrayList windowHandles) { windowHandles.Add(hwnd); return true; } '@ Add-Type -MemberDefinition $MethodDefinition -Name 'ShowHideWindow' -Namespace 'User32' | Out-Null Start-Process notepad $hwnds = [User32.ShowHideWindow]::GetWindows() $ultimo = $hwnds | sort # Quick method to create a string of ~290 characters [String]$tempString = (0..100) $stringLength = $tempString.Length [System.Collections.ArrayList]$Windows = @() # Convertir hex to dec # [uint32]"0X002A00A8" foreach ($hwnd in 1..5752680) { $ClassNameLength = [User32.ShowHideWindow]::GetClassName($hwnd, $tempString, $stringLength) $ClassName = $tempString.Substring(0,$ClassNameLength) if($ClassName) { #$hwnd #$ClassName $WindowTextLength = [User32.ShowHideWindow]::GetWindowText($hWnd, $tempString, $stringLength) $WindowText = $tempString.Substring(0,$WindowTextLength) #$WindowText $Window = New-Object -TypeName psobject -Property @{hwnd = $hwnd; ClassName = $ClassName; WindowText = $WindowText} $Windows.Add($Window) | Out-Null } } $idcickdo = $Windows| where {$_.ClassName -eq "RichEditD2DPT"} $codigo2=' [DllImport("user32.dll", EntryPoint = "FindWindowEx")]public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("User32.dll")]public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam); ' $acciones = Add-Type -MemberDefinition $codigo2 -Name Acciones -PassThru $acciones::SendMessage($idcickdo.hwnd, 0x000C, 0, "Texto nuevo") |