-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWindowCommands.ps1
More file actions
413 lines (337 loc) · 12.2 KB
/
WindowCommands.ps1
File metadata and controls
413 lines (337 loc) · 12.2 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# using namespace System
# using namespace System.Collections.Generic
# using namespace System.ComponentModel
# using namespace System.Runtime.InteropServices
# $user32 = lib user32
# [Flags()]
# enum WindowStationAccessRights {
# # Required to delete the object.
# Delete = 0x00010000
# # Required to read information in the security descriptor for the object, not including the information in the SACL.
# ReadControl = 0x00020000
# # Not supported for window station objects.
# Synchronize = 0x00100000
# # Required to modify the DACL in the security descriptor for the object.
# WriteDac = 0x00040000
# # Required to change the owner in the security descriptor for the object.
# WriteOwner = 0x00080000
# # All possible access rights for the window station.
# AllAccess = 0x37F
# # Required to use the clipboard.
# AccessClipboard = 0x0004
# # Required to manipulate global atoms.
# AccessGlobalAtoms = 0x0020
# # Required to create new desktop objects on the window station.
# CreateDesktop = 0x0008
# # Required to enumerate existing desktop objects.
# EnumDesktops = 0x0001
# # Required for the window station to be enumerated.
# Enumerate = 0x0100
# # Required to successfully call the ExitWindows or ExitWindowsEx function.
# ExitWindows = 0x0040
# # Required to read the attributes of a window station object.
# ReadAttributes = 0x0002
# # Required to access screen contents.
# ReadScreen = 0x0200
# # Required to modify the attributes of a window station object.
# WriteAttributes = 0x0010
# }
# class WindowStationHandle : IDisposable {
# hidden [bool] $_isDisposed
# hidden [bool] $_ownsHandle
# [IntPtr] $Value
# hidden WindowStationHandle([IntPtr] $value) {
# $this.Value = $value
# $this._ownsHandle = $true
# }
# hidden WindowStationHandle([IntPtr] $value, [bool] $ownsHandle) {
# $this.Value = $value
# $this._ownsHandle = $false
# }
# static [WindowStationHandle] GetNull() {
# return [WindowStationHandle]::new([IntPtr]::Zero, $false)
# }
# static [WindowStationHandle] GetCurrent() {
# $user32 = $script:user32
# $result = $user32.SetLastError().GetProcessWindowStation[IntPtr]()
# if ($result -eq [IntPtr]::Zero) {
# throw [Win32Exception]::new($user32.LastError)
# }
# return [WindowStationHandle]::new($result, $false)
# }
# static [WindowStationHandle] Open([string] $winsta) {
# return [WindowStationHandle]::Open(
# $winsta,
# $true,
# [WindowStationAccessRights]::EnumDesktops -bor 'Enumerate' -bor 'ReadAttributes' -bor 'ReadScreen')
# }
# static [WindowStationHandle] Open([string] $winsta, [bool] $inherit, [int] $desiredAccess) {
# $user32 = $script:user32
# $result = $user32.SetLastError().OpenWindowStationW[IntPtr](
# $user32.MarshalAs($winsta, [UnmanagedType]::LPWStr),
# $inherit,
# $desiredAccess)
# if ($result -eq [IntPtr]::Zero) {
# throw [Win32Exception]::new($user32.LastError)
# }
# return [WindowStationHandle]::new($result)
# }
# [void] Dispose() {
# if ($this._isDisposed) {
# return
# }
# if ($this._ownsHandle) {
# $user32 = $script:user32
# if ($user32.SetLastError().CloseWindowStation($this.Value) -eq 0) {
# throw [Win32Exception]::new($user32.LastError)
# }
# }
# $this._isDisposed = $true
# }
# }
# class ItemHandle : IDisposable {
# [bool] $_isDisposed
# [GCHandle] $Handle
# [IntPtr] $Value
# hidden ItemHandle([GCHandle] $handle) {
# $this.Handle = $handle
# $this.Value = [GCHandle]::ToIntPtr($handle)
# }
# static [ItemHandle] Alloc([object] $value) {
# return [ItemHandle]::new([GCHandle]::Alloc($value))
# }
# [void] Dispose() {
# if ($this._isDisposed) {
# return
# }
# $this.Handle.Free()
# $this.Value = [IntPtr]::Zero
# $this._isDisposed = $true
# }
# }
# class DisposalTarget : IDisposable {
# [IDisposable] $Value
# DisposalTarget() {
# }
# DisposalTarget([IDisposable] $value) {
# $this.Value = $value
# }
# [void] Dispose() {
# $this.Value?.Dispose()
# }
# }
# function Get-WindowStation {
# [CmdletBinding()]
# param()
# end {
# $users = [List[string]]::new()
# $pUsers = $null
# try {
# $pUsers = [GCHandle]::Alloc($users)
# $success = $user32.SetLastError().EnumWindowStationsW[int](
# {
# [OutputType([int])]
# param(
# [MarshalAs([UnmanagedType]::LPWStr)]
# [string] $lpszWindowStation,
# [intptr] $lParam
# )
# [GCHandle]::FromIntPtr($lParam).Target.Add($lpszWindowStation)
# return 1
# },
# [GCHandle]::ToIntPtr($pUsers))
# } finally {
# if ($pUsers -is [GCHandle]) {
# $pUsers.Free()
# }
# }
# if ($success -ne 1) {
# throw [Win32Exception]::new($user.LastError)
# }
# return $users
# }
# }
# function Get-WindowDesktop {
# [CmdletBinding()]
# param(
# [Parameter(ValueFromPipeline)]
# [string] $WindowStation
# )
# process {
# if ($MyInvocation.ExpectingInput -and -not $WindowStation) {
# return
# }
# $desktops = [List[string]]::new()
# use { $winsta = [DisposalTarget]::new() } {
# use { $pList = [ItemHandle]::Alloc($desktops) } {
# $winsta.Value = $WindowStation ? [WindowStationHandle]::Open($WindowStation) : [WindowStationHandle]::GetCurrent()
# $user32 = $script:user32
# $success = $user32.SetLastError().EnumDesktopsW(
# $winsta.Value.Value,
# {
# [OutputType([int])]
# param(
# [MarshalAs([UnmanagedType]::LPTStr)]
# [string] $lpszDesktop,
# [IntPtr] $lParam
# )
# end {
# [GCHandle]::FromIntPtr($lParam).Target.Add($lpszDesktop)
# return 1
# }
# },
# $pList.Value)
# if ($success -eq 0) {
# throw [Win32Exception]::new($user32.LastError)
# }
# return $desktops
# }}
# }
# }
# [Flags()]
# enum WindowStyle : uint {
# Tiled = 0u
# Overlapped = 0u
# MaximizeBox = 1u -shl 16
# TabStop = 1u -shl 16
# Group = 1u -shl 17
# MinimizeBox = 1u -shl 17
# SizeBox = 1u -shl 18
# ThickFrame = 1u -shl 18
# SysMenu = 1u -shl 19
# HScroll = 1u -shl 20
# VScroll = 1u -shl 21
# DlgFrame = 1u -shl 22
# Border = 1u -shl 23
# Caption = (1u -shl 22) -bor (1u -shl 23)
# TiledWindow = 0u -bor (1u -shl 17) -bor (1u -shl 17) -bor (1u -shl 18) -bor (1u -shl 19) -bor ((1u -shl 22) -bor (1u -shl 23))
# OverlappedWindow = 0u -bor (1u -shl 16) -bor (1u -shl 17) -bor (1u -shl 18) -bor (1u -shl 19) -bor ((1u -shl 22) -bor (1u -shl 23))
# Maximize = 1u -shl 24
# ClipChildren = 1u -shl 25
# ClipSiblings = 1u -shl 26
# Disabled = 1u -shl 27
# Visible = 1u -shl 28
# Minimize = 1u -shl 29
# Iconic = 1u -shl 29
# Child = 1u -shl 30
# ChildWindow = 1u -shl 30
# Popup = 1u -shl 31
# PopupWindow = (1u -shl 19) -bor (1u -shl 23) -bor (1u -shl 31)
# }
# [Flags()]
# enum WindowStyleEx : uint {
# Left = 0u
# LtrReading = 0u
# DialogModalFrame = 1u -shl 0
# Unused1 = 1u -shl 1
# NoParentNotify = 1u -shl 2
# TopMost = 1u -shl 3
# AcceptFiles = 1u -shl 4
# Transparent = 1u -shl 5
# MdiChild = 1u -shl 6
# ToolWindow = 1u -shl 7
# WindowEdge = 1u -shl 8
# PaletteWindow = (1u -shl 3) -bor (1u -shl 7) -bor (1u -shl 8)
# OverlappedWindow = (1u -shl 8) -bor (1u -shl 9)
# ClientEdge = 1u -shl 9
# ContextHelp = 1u -shl 10
# MakeVisibleWhenUnghosted = 1u -shl 11
# Right = 1u -shl 12
# RtlReading = 1u -shl 13
# LeftScrollBar = 1u -shl 14
# Unused2 = 1u -shl 15
# ControlParent = 1u -shl 16
# StaticEdge = 1u -shl 17
# AppWindow = 1u -shl 18
# Layered = 1u -shl 19
# NoInheritLayout = 1u -shl 20
# NoRedirectionBitmap = 1u -shl 21
# LayoutRtl = 1u -shl 22
# NoPaddedBorder = 1u -shl 23
# Unused4 = 1u -shl 24
# Composited = 1u -shl 25
# UIStateActive = 1u -shl 26
# NoActivate = 1u -shl 27
# CompositedCompositing = 1u -shl 28
# Redirected = 1u -shl 29
# UIStateKbdAccelHidden = 1u -shl 30
# UIStateFocusRectHidden = 1u -shl 31
# }
# struct RECT {
# [int] $left
# [int] $top
# [int] $right
# [int] $bottom
# }
# struct WINDOWINFO {
# [uint] $cbSize
# [RECT] $rcWindow
# [RECT] $rcClient
# [WindowStyle] $dwStyle
# [WindowStyleEx] $dwExStyle
# [uint] $dwWindowStatus
# [uint] $cxWindowBorders
# [uint] $cyWindowBorders
# [ushort] $atomWindowType
# [ushort] $wCreatorVersion
# }
# function Get-Window {
# [CmdletBinding()]
# param()
# end {
# $user32 = $script:user32
# $windows = [List[ptr]]::new()
# use { $pList = [ItemHandle]::Alloc($windows) } {
# $user32 = $script:user32
# $success = $user32.SetLastError().EnumWindows(
# {
# [OutputType([int])]
# param([ptr] $hwnd, [ptr] $lParam)
# end {
# [GCHandle]::FromIntPtr($lParam).Target.Add($hwnd)
# return 1
# }
# },
# $pList.Value)
# if ($success -eq 0) {
# throw [Win32Exception]::new($user32.LastError)
# }
# }
# foreach ($window in $windows) {
# $info = [ptr]::AllocZeroed[WINDOWINFO](1)
# $temp = $info[0]
# $temp.cbSize = size WINDOWINFO
# $info[0] = $temp
# $success = $user32.SetLastError().GetWindowInfo[int]($window, $info)
# if ($success -eq 0) {
# throw [Win32Exception]::new($user32.LastError)
# }
# $length = $user32.SetLastError().GetWindowTextLengthW($window)
# if ($length -eq 0 -and $user32.LastError -ne 0) {
# throw [Win32Exception]::new($user32.LastError)
# }
# $windowText = $null
# if ($length -gt 0) {
# $text = [ptr]::Alloc[char]($length + 1)
# $null = $user32.SetLastError().GetWindowTextW(
# $window,
# $text,
# $length + 1)
# $windowText = $text.ToString($length)
# } else {
# $windowText = ''
# }
# $processId = 0u
# if ($user32.SetLastError().GetWindowThreadProcessId($window, [ref] $processId) -eq 0) {
# throw [Win32Exception]::new($user32.LastError)
# }
# [pscustomobject]@{
# Process = Get-Process -Id $processId | Add-Member -Force -PassThru -MemberType ScriptMethod -Name ToString -Value {
# $this.MainModule.FileName | Split-Path -Leaf
# }
# Info = $info
# Title = $windowText
# }
# }
# }
# }