-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_mute.ps1
More file actions
799 lines (679 loc) · 21.2 KB
/
window_mute.ps1
File metadata and controls
799 lines (679 loc) · 21.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
param (
[Parameter(Mandatory = $true)]
[Alias("ProcessName")]
[string]$p,
[Parameter(Mandatory = $false)]
[Alias("LogPath")]
[string]$l = "D:\mute.txt",
[Parameter(Mandatory = $false)]
[ValidateSet("Transparent", "Hide", "Move")]
[string]$Mode = "Transparent",
[Parameter(Mandatory = $false)]
[string]$ClassPattern,
[Parameter(Mandatory = $false)]
[string]$TitlePattern,
[Parameter(Mandatory = $false)]
[ValidateRange(200, 60000)]
[int]$SweepIntervalMs = 2000
)
Add-Type -AssemblyName System.Windows.Forms
$win32Code = @"
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
public static class NativeMethods
{
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
public delegate void WinEventDelegate(
IntPtr hWinEventHook,
uint eventType,
IntPtr hWnd,
int idObject,
int idChild,
uint dwEventThread,
uint dwmsEventTime
);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumChildWindows(IntPtr hWndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
uint uFlags
);
[DllImport("user32.dll")]
public static extern IntPtr SetWinEventHook(
uint eventMin,
uint eventMax,
IntPtr hmodWinEventProc,
WinEventDelegate lpfnWinEventProc,
uint idProcess,
uint idThread,
uint dwFlags
);
[DllImport("user32.dll")]
public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "GetWindowLong")]
private static extern int GetWindowLong32(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "GetWindowLongPtr")]
private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "SetWindowLong")]
private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true, EntryPoint = "SetWindowLongPtr")]
private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetLayeredWindowAttributes(IntPtr hwnd, out uint pcrKey, out byte pbAlpha, out uint pdwFlags);
public const int SW_HIDE = 0;
public const int GWL_EXSTYLE = -20;
public const uint SWP_NOSIZE = 0x0001;
public const uint SWP_NOMOVE = 0x0002;
public const uint SWP_NOZORDER = 0x0004;
public const uint SWP_NOACTIVATE = 0x0010;
public const uint SWP_FRAMECHANGED = 0x0020;
public const uint SWP_HIDEWINDOW = 0x0080;
public const long WS_EX_LAYERED = 0x00080000L;
public const long WS_EX_TRANSPARENT = 0x00000020L;
public const uint LWA_ALPHA = 0x00000002;
public const uint EVENT_OBJECT_CREATE = 0x8000;
public const uint EVENT_OBJECT_SHOW = 0x8002;
public const int OBJID_WINDOW = 0;
public const uint WINEVENT_OUTOFCONTEXT = 0x0000;
public const uint WINEVENT_SKIPOWNPROCESS = 0x0002;
public static int GetProcessId(IntPtr hWnd)
{
uint pid;
GetWindowThreadProcessId(hWnd, out pid);
return unchecked((int)pid);
}
public static string GetClassNameSafe(IntPtr hWnd)
{
var builder = new StringBuilder(256);
return GetClassName(hWnd, builder, builder.Capacity) > 0 ? builder.ToString() : string.Empty;
}
public static string GetWindowTextSafe(IntPtr hWnd)
{
int length = GetWindowTextLength(hWnd);
if (length <= 0)
{
return string.Empty;
}
var builder = new StringBuilder(length + 1);
return GetWindowText(hWnd, builder, builder.Capacity) > 0 ? builder.ToString() : string.Empty;
}
public static IntPtr GetWindowLongPtrCompat(IntPtr hWnd, int nIndex)
{
return IntPtr.Size == 8
? GetWindowLongPtr64(hWnd, nIndex)
: new IntPtr(GetWindowLong32(hWnd, nIndex));
}
public static IntPtr SetWindowLongPtrCompat(IntPtr hWnd, int nIndex, IntPtr newValue)
{
return IntPtr.Size == 8
? SetWindowLongPtr64(hWnd, nIndex, newValue)
: new IntPtr(SetWindowLong32(hWnd, nIndex, unchecked((int)newValue.ToInt64())));
}
public static bool IsTransparentWindow(IntPtr hWnd)
{
long exStyle = GetWindowLongPtrCompat(hWnd, GWL_EXSTYLE).ToInt64();
if ((exStyle & (WS_EX_LAYERED | WS_EX_TRANSPARENT)) != (WS_EX_LAYERED | WS_EX_TRANSPARENT))
{
return false;
}
uint colorKey;
byte alpha;
uint flags;
if (!GetLayeredWindowAttributes(hWnd, out colorKey, out alpha, out flags))
{
return false;
}
return (flags & LWA_ALPHA) != 0 && alpha == 0;
}
public static void MakeTransparent(IntPtr hWnd)
{
long exStyle = GetWindowLongPtrCompat(hWnd, GWL_EXSTYLE).ToInt64();
long newExStyle = exStyle | WS_EX_LAYERED | WS_EX_TRANSPARENT;
if (newExStyle != exStyle)
{
SetWindowLongPtrCompat(hWnd, GWL_EXSTYLE, new IntPtr(newExStyle));
}
SetLayeredWindowAttributes(hWnd, 0, 0, LWA_ALPHA);
SetWindowPos(
hWnd,
IntPtr.Zero,
0,
0,
0,
0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED
);
}
}
public sealed class WindowMuteSession : ApplicationContext
{
private readonly string _processName;
private readonly string _logPath;
private readonly string _mode;
private readonly Regex _classRegex;
private readonly Regex _titleRegex;
private readonly Timer _timer;
private readonly Control _shutdownInvoker;
private readonly NativeMethods.EnumWindowsProc _enumTopLevelCallback;
private readonly NativeMethods.EnumWindowsProc _enumChildCallback;
private readonly NativeMethods.WinEventDelegate _winEventCallback;
private readonly Dictionary<int, IntPtr[]> _hooksByPid = new Dictionary<int, IntPtr[]>();
private readonly Dictionary<IntPtr, DateTime> _lastLogByWindow = new Dictionary<IntPtr, DateTime>();
private HashSet<int> _targetPids = new HashSet<int>();
private bool _shutdownRequested;
public WindowMuteSession(
string processName,
string logPath,
string mode,
string classPattern,
string titlePattern,
int sweepIntervalMs
)
{
_processName = NormalizeProcessName(processName);
_logPath = logPath;
_mode = mode;
_classRegex = string.IsNullOrWhiteSpace(classPattern)
? null
: new Regex(classPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
_titleRegex = string.IsNullOrWhiteSpace(titlePattern)
? null
: new Regex(titlePattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
_enumTopLevelCallback = EnumTopLevelWindow;
_enumChildCallback = EnumChildWindow;
_winEventCallback = HandleWinEvent;
_shutdownInvoker = new Control();
_shutdownInvoker.CreateControl();
_timer = new Timer();
_timer.Interval = Math.Max(200, sweepIntervalMs);
_timer.Tick += TimerTick;
}
public void Start()
{
EnsureLogDirectory();
RefreshTargetPids();
SynchronizeHooks();
SweepExistingWindows("startup");
_timer.Start();
Log(string.Format(
"[{0:yyyy-MM-dd HH:mm:ss.fff}] watching process={1}, mode={2}, class={3}, title={4}, sweep={5}ms",
DateTime.Now,
Safe(_processName),
Safe(_mode),
_classRegex == null ? "*" : Safe(_classRegex.ToString()),
_titleRegex == null ? "*" : Safe(_titleRegex.ToString()),
_timer.Interval
));
}
public void Shutdown()
{
if (_shutdownRequested)
{
return;
}
_shutdownRequested = true;
if (_shutdownInvoker.IsHandleCreated && _shutdownInvoker.InvokeRequired)
{
_shutdownInvoker.BeginInvoke((MethodInvoker)ShutdownCore);
return;
}
ShutdownCore();
}
protected override void ExitThreadCore()
{
Cleanup();
base.ExitThreadCore();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
Cleanup();
}
base.Dispose(disposing);
}
private void Cleanup()
{
StopLiveWork();
_timer.Tick -= TimerTick;
_hooksByPid.Clear();
_shutdownInvoker.Dispose();
}
private void SynchronizeHooks()
{
if (_shutdownRequested)
{
return;
}
var stalePids = new List<int>();
foreach (KeyValuePair<int, IntPtr[]> entry in _hooksByPid)
{
if (!_targetPids.Contains(entry.Key))
{
stalePids.Add(entry.Key);
}
}
foreach (int pid in stalePids)
{
RemoveHooks(pid);
}
foreach (int pid in _targetPids)
{
if (_hooksByPid.ContainsKey(pid))
{
continue;
}
IntPtr createHook = NativeMethods.SetWinEventHook(
NativeMethods.EVENT_OBJECT_CREATE,
NativeMethods.EVENT_OBJECT_CREATE,
IntPtr.Zero,
_winEventCallback,
unchecked((uint)pid),
0,
NativeMethods.WINEVENT_OUTOFCONTEXT | NativeMethods.WINEVENT_SKIPOWNPROCESS
);
IntPtr showHook = NativeMethods.SetWinEventHook(
NativeMethods.EVENT_OBJECT_SHOW,
NativeMethods.EVENT_OBJECT_SHOW,
IntPtr.Zero,
_winEventCallback,
unchecked((uint)pid),
0,
NativeMethods.WINEVENT_OUTOFCONTEXT | NativeMethods.WINEVENT_SKIPOWNPROCESS
);
if (createHook != IntPtr.Zero || showHook != IntPtr.Zero)
{
_hooksByPid[pid] = new[] { createHook, showHook };
}
}
}
private void RemoveHooks(int pid)
{
IntPtr[] hooks;
if (!_hooksByPid.TryGetValue(pid, out hooks))
{
return;
}
for (int i = 0; i < hooks.Length; i++)
{
IntPtr hook = hooks[i];
if (hook != IntPtr.Zero)
{
NativeMethods.UnhookWinEvent(hook);
}
}
_hooksByPid.Remove(pid);
}
private void TimerTick(object sender, EventArgs e)
{
if (_shutdownRequested)
{
return;
}
RefreshTargetPids();
SynchronizeHooks();
SweepExistingWindows("timer");
}
private void HandleWinEvent(
IntPtr hWinEventHook,
uint eventType,
IntPtr hWnd,
int idObject,
int idChild,
uint dwEventThread,
uint dwmsEventTime
)
{
if (_shutdownRequested)
{
return;
}
if (hWnd == IntPtr.Zero || idObject != NativeMethods.OBJID_WINDOW || idChild != 0)
{
return;
}
TryNeutralize(hWnd, "event:0x" + eventType.ToString("X"));
}
private void RefreshTargetPids()
{
if (_shutdownRequested)
{
return;
}
var latest = new HashSet<int>();
foreach (Process process in Process.GetProcessesByName(_processName))
{
try
{
latest.Add(process.Id);
}
catch
{
}
finally
{
process.Dispose();
}
}
_targetPids = latest;
}
private void SweepExistingWindows(string source)
{
if (_shutdownRequested)
{
return;
}
if (_targetPids.Count == 0)
{
return;
}
NativeMethods.EnumWindows(_enumTopLevelCallback, new IntPtr(source == "startup" ? 1 : 0));
}
private bool EnumTopLevelWindow(IntPtr hWnd, IntPtr lParam)
{
if (_shutdownRequested)
{
return false;
}
string source = lParam == new IntPtr(1) ? "startup" : "timer";
int pid = NativeMethods.GetProcessId(hWnd);
if (!IsTargetProcessId(pid))
{
return true;
}
TryNeutralize(hWnd, source);
NativeMethods.EnumChildWindows(hWnd, _enumChildCallback, IntPtr.Zero);
return true;
}
private bool EnumChildWindow(IntPtr hWnd, IntPtr lParam)
{
if (_shutdownRequested)
{
return false;
}
TryNeutralize(hWnd, "child");
return true;
}
private bool TryNeutralize(IntPtr hWnd, string source)
{
if (_shutdownRequested)
{
return false;
}
if (hWnd == IntPtr.Zero || !NativeMethods.IsWindow(hWnd))
{
return false;
}
int pid = NativeMethods.GetProcessId(hWnd);
if (!IsTargetProcessId(pid))
{
return false;
}
NativeMethods.RECT rect = default(NativeMethods.RECT);
bool hasRect = false;
if (_mode == "Move")
{
NativeMethods.GetWindowRect(hWnd, out rect);
hasRect = true;
}
if (AlreadyNeutralized(hWnd, rect))
{
return false;
}
string className = string.Empty;
if (_classRegex != null)
{
className = NativeMethods.GetClassNameSafe(hWnd);
if (!_classRegex.IsMatch(className))
{
return false;
}
}
string title = string.Empty;
if (_titleRegex != null)
{
title = NativeMethods.GetWindowTextSafe(hWnd);
if (!_titleRegex.IsMatch(title))
{
return false;
}
}
switch (_mode)
{
case "Hide":
NativeMethods.ShowWindowAsync(hWnd, NativeMethods.SW_HIDE);
NativeMethods.SetWindowPos(
hWnd,
IntPtr.Zero,
0,
0,
0,
0,
NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOZORDER |
NativeMethods.SWP_NOACTIVATE | NativeMethods.SWP_HIDEWINDOW
);
break;
case "Move":
NativeMethods.SetWindowPos(
hWnd,
IntPtr.Zero,
9999,
9999,
0,
0,
NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE
);
break;
default:
NativeMethods.MakeTransparent(hWnd);
break;
}
if (!hasRect)
{
NativeMethods.GetWindowRect(hWnd, out rect);
}
if (_classRegex == null)
{
className = NativeMethods.GetClassNameSafe(hWnd);
}
if (_titleRegex == null)
{
title = NativeMethods.GetWindowTextSafe(hWnd);
}
LogWindowAction(hWnd, pid, className, title, rect, source);
return true;
}
private bool AlreadyNeutralized(IntPtr hWnd, NativeMethods.RECT rect)
{
switch (_mode)
{
case "Hide":
return !NativeMethods.IsWindowVisible(hWnd);
case "Move":
return rect.Left == 9999 && rect.Top == 9999;
default:
return NativeMethods.IsTransparentWindow(hWnd);
}
}
private void LogWindowAction(
IntPtr hWnd,
int pid,
string className,
string title,
NativeMethods.RECT rect,
string source
)
{
DateTime now = DateTime.Now;
DateTime lastLog;
if (_lastLogByWindow.TryGetValue(hWnd, out lastLog) && (now - lastLog).TotalSeconds < 5)
{
return;
}
_lastLogByWindow[hWnd] = now;
Log(string.Format(
"[{0:yyyy-MM-dd HH:mm:ss.fff}] source={1} mode={2} pid={3} hwnd=0x{4:X} class={5} title={6} rect=({7},{8})-({9},{10})",
now,
Safe(source),
Safe(_mode),
pid,
hWnd.ToInt64(),
Safe(className),
Safe(title),
rect.Left,
rect.Top,
rect.Right,
rect.Bottom
));
}
private void EnsureLogDirectory()
{
string directory = Path.GetDirectoryName(_logPath);
if (!string.IsNullOrWhiteSpace(directory))
{
Directory.CreateDirectory(directory);
}
}
private void Log(string message)
{
File.AppendAllText(_logPath, message + Environment.NewLine, Encoding.UTF8);
try
{
Console.WriteLine(message);
}
catch
{
}
}
private void ShutdownCore()
{
StopLiveWork();
Log(string.Format(
"[{0:yyyy-MM-dd HH:mm:ss.fff}] shutdown requested",
DateTime.Now
));
ExitThread();
}
private bool IsTargetProcessId(int pid)
{
if (_shutdownRequested)
{
return false;
}
return _targetPids.Contains(pid);
}
private void StopLiveWork()
{
_timer.Stop();
var hookedPids = new List<int>(_hooksByPid.Keys);
for (int i = 0; i < hookedPids.Count; i++)
{
RemoveHooks(hookedPids[i]);
}
}
private static string NormalizeProcessName(string processName)
{
return Path.GetFileNameWithoutExtension(processName ?? string.Empty);
}
private static string Safe(string value)
{
if (string.IsNullOrEmpty(value))
{
return "(empty)";
}
return value.Replace("\r", " ").Replace("\n", " ");
}
}
"@
Add-Type -ReferencedAssemblies "System.Windows.Forms" -TypeDefinition $win32Code -ErrorAction Stop
$processName = [System.IO.Path]::GetFileNameWithoutExtension($p)
if ([string]::IsNullOrWhiteSpace($processName)) {
throw "Parameter -p must be a valid process name, for example: WeChat or WeChat.exe"
}
$session = [WindowMuteSession]::new($processName, $l, $Mode, $ClassPattern, $TitlePattern, $SweepIntervalMs)
$cancelHandler = $null
$shutdownNoticeShown = $false
Write-Host ">>> WindowMute started | Process=$processName | Mode=$Mode | Log=$l <<<" -ForegroundColor Cyan
if ($ClassPattern) {
Write-Host " Class filter: $ClassPattern" -ForegroundColor DarkCyan
}
if ($TitlePattern) {
Write-Host " Title filter: $TitlePattern" -ForegroundColor DarkCyan
}
if ($Host.Name -eq "ConsoleHost") {
Write-Host " Press Ctrl+C to stop." -ForegroundColor DarkGray
}
try {
$cancelHandler = [System.ConsoleCancelEventHandler]{
param($sender, $eventArgs)
$eventArgs.Cancel = $true
if (-not $script:shutdownNoticeShown) {
$script:shutdownNoticeShown = $true
try {
[System.Console]::WriteLine("Ctrl+C received, shutting down...")
}
catch {
}
}
if ($script:session) {
$script:session.Shutdown()
}
}
[System.Console]::add_CancelKeyPress($cancelHandler)
}
catch {
}
try {
$session.Start()
[System.Windows.Forms.Application]::Run($session)
}
finally {
if ($cancelHandler) {
try {
[System.Console]::remove_CancelKeyPress($cancelHandler)
}
catch {
}
}
$session.Dispose()
}