From 1d1f92c608d39e2aea5bfd47f6e49a5e2557062c Mon Sep 17 00:00:00 2001 From: Yaakov Date: Thu, 21 May 2020 17:52:07 +1000 Subject: [PATCH] Revert "Use EnumWindows instead of FindWindow" This reverts commit fa24ae3022fdd83b51e3707b31ecd462549c842b. --- .../Program.cs | 12 +--- Notepad.Extensions.Logging/NativeMethods.cs | 9 --- Notepad.Extensions.Logging/WindowFinder.cs | 64 ++++--------------- 3 files changed, 15 insertions(+), 70 deletions(-) diff --git a/Notepad.Extensions.Logging.FunctionalTest/Program.cs b/Notepad.Extensions.Logging.FunctionalTest/Program.cs index 886a3f2..b8deb88 100644 --- a/Notepad.Extensions.Logging.FunctionalTest/Program.cs +++ b/Notepad.Extensions.Logging.FunctionalTest/Program.cs @@ -18,15 +18,9 @@ namespace Notepad.Extensions.Logging.FunctionalTest var sp = sc.BuildServiceProvider(); var logger = sp.GetRequiredService>(); - //logger.LogWarning("Here is a warning."); - //logger.LogError(GetException(), "oh no!."); - //logger.LogInformation("Here is some info."); - //logger.LogInformation("here, have a nice 😋 emoji."); - logger.LogInformation(@"💧 Paul -@paws101 -Replying to -@yaakov_h -But does it handle emoji injection?."); + logger.LogWarning("Here is a warning."); + logger.LogError(GetException(), "oh no!."); + logger.LogInformation("Here is some info."); } static Exception GetException() diff --git a/Notepad.Extensions.Logging/NativeMethods.cs b/Notepad.Extensions.Logging/NativeMethods.cs index 9426460..6de089f 100644 --- a/Notepad.Extensions.Logging/NativeMethods.cs +++ b/Notepad.Extensions.Logging/NativeMethods.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.InteropServices; -using System.Text; namespace Microsoft.Extensions.Logging { @@ -16,13 +15,5 @@ namespace Microsoft.Extensions.Logging [DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam); - - public delegate bool EnumWindowsDelegate(IntPtr hWnd, IntPtr lParam); - - [DllImport("user32.dll")] - public static extern bool EnumWindows(EnumWindowsDelegate lpEnumFunc, IntPtr lParam); - - [DllImport("User32.dll")] - public static extern int GetWindowText(IntPtr hWndParent, StringBuilder sb, int maxCount); } } diff --git a/Notepad.Extensions.Logging/WindowFinder.cs b/Notepad.Extensions.Logging/WindowFinder.cs index 19faae1..a033593 100644 --- a/Notepad.Extensions.Logging/WindowFinder.cs +++ b/Notepad.Extensions.Logging/WindowFinder.cs @@ -1,6 +1,4 @@ using System; -using System.ComponentModel; -using System.Text; namespace Microsoft.Extensions.Logging { @@ -8,66 +6,28 @@ namespace Microsoft.Extensions.Logging { public static IntPtr FindNotepadWindow() { - sb ??= new StringBuilder(4096); - - try - { - FindMainWindow(); - if (handle == IntPtr.Zero) - { - return handle; - } - - IntPtr edit = NativeMethods.FindWindowEx(handle, IntPtr.Zero, "EDIT", null); - return edit; - } - finally - { - handle = IntPtr.Zero; - sb.Clear(); - } + var hwnd = FindMainWindow(); + IntPtr edit = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null); + return edit; } static IntPtr FindMainWindow() { - NativeMethods.EnumWindows(enumWindowsDelegate, IntPtr.Zero); - return handle; - } - - static NativeMethods.EnumWindowsDelegate enumWindowsDelegate = new NativeMethods.EnumWindowsDelegate(EnumWindowsCallback); - - static bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam) - { - var result = NativeMethods.GetWindowText(hWnd, sb, sb.Capacity); - if (result < 0) + IntPtr hwnd; + + hwnd = NativeMethods.FindWindow(null, "Untitled - Notepad"); + if (hwnd != IntPtr.Zero) { - throw new Win32Exception(result); + return hwnd; } - if (IsKnownNotepadWindow(sb.ToString())) + hwnd = NativeMethods.FindWindow(null, "*Untitled - Notepad"); + if (hwnd != IntPtr.Zero) { - WindowFinder.handle = hWnd; - return false; - } - return true; - } - - [ThreadStatic] - static IntPtr handle; - - [ThreadStatic] - static StringBuilder sb; - - static bool IsKnownNotepadWindow(string titleText) - { - switch (titleText) - { - case "Untitled - Notepad": - case "*Untitled - Notepad": - return true; + return hwnd; } - return false; + return IntPtr.Zero; } } }