|
2 | 2 | using System.ComponentModel;
|
3 | 3 | using System.Diagnostics;
|
4 | 4 | using System.IO;
|
5 |
| -using System.Runtime.InteropServices; |
6 |
| -using System.Text; |
7 | 5 | using System.Threading;
|
| 6 | +using Windows.Win32; |
| 7 | +using Windows.Win32.Foundation; |
8 | 8 |
|
9 | 9 | namespace Flow.Launcher.Plugin.SharedCommands
|
10 | 10 | {
|
11 | 11 | public static class ShellCommand
|
12 | 12 | {
|
13 | 13 | public delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);
|
14 |
| - [DllImport("user32.dll")] static extern bool EnumThreadWindows(uint threadId, EnumThreadDelegate lpfn, IntPtr lParam); |
15 |
| - [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount); |
16 |
| - [DllImport("user32.dll")] static extern int GetWindowTextLength(IntPtr hwnd); |
17 | 14 |
|
18 | 15 | private static bool containsSecurityWindow;
|
19 | 16 |
|
@@ -42,21 +39,33 @@ private static void CheckSecurityWindow()
|
42 | 39 | {
|
43 | 40 | ProcessThreadCollection ptc = Process.GetCurrentProcess().Threads;
|
44 | 41 | for (int i = 0; i < ptc.Count; i++)
|
45 |
| - EnumThreadWindows((uint)ptc[i].Id, CheckSecurityThread, IntPtr.Zero); |
| 42 | + PInvoke.EnumThreadWindows((uint)ptc[i].Id, CheckSecurityThread, IntPtr.Zero); |
46 | 43 | }
|
47 | 44 |
|
48 |
| - private static bool CheckSecurityThread(IntPtr hwnd, IntPtr lParam) |
| 45 | + private static BOOL CheckSecurityThread(HWND hwnd, LPARAM lParam) |
49 | 46 | {
|
50 | 47 | if (GetWindowTitle(hwnd) == "Windows Security")
|
51 | 48 | containsSecurityWindow = true;
|
52 | 49 | return true;
|
53 | 50 | }
|
54 | 51 |
|
55 |
| - private static string GetWindowTitle(IntPtr hwnd) |
| 52 | + private static unsafe string GetWindowTitle(HWND hwnd) |
56 | 53 | {
|
57 |
| - StringBuilder sb = new StringBuilder(GetWindowTextLength(hwnd) + 1); |
58 |
| - GetWindowText(hwnd, sb, sb.Capacity); |
59 |
| - return sb.ToString(); |
| 54 | + var capacity = PInvoke.GetWindowTextLength(hwnd) + 1; |
| 55 | + char[] buffer = new char[capacity]; |
| 56 | + fixed (char* pBuffer = buffer) |
| 57 | + { |
| 58 | + // If the window has no title bar or text, if the title bar is empty, |
| 59 | + // or if the window or control handle is invalid, the return value is zero. |
| 60 | + if (PInvoke.GetWindowText(hwnd, (PWSTR)pBuffer, capacity) == 0) |
| 61 | + { |
| 62 | + return string.Empty; |
| 63 | + } |
| 64 | + |
| 65 | + int validLength = Array.IndexOf(buffer, '\0'); |
| 66 | + if (validLength < 0) validLength = capacity; |
| 67 | + return new string(buffer, 0, validLength); |
| 68 | + } |
60 | 69 | }
|
61 | 70 |
|
62 | 71 | public static ProcessStartInfo SetProcessStartInfo(this string fileName, string workingDirectory = "", string arguments = "", string verb = "", bool createNoWindow = false)
|
|
0 commit comments