mirror of
https://github.com/yaakov-h/Notepad.Extensions.Logging.git
synced 2024-11-25 04:24:49 +00:00
Use EnumWindows instead of FindWindow
This commit is contained in:
parent
e512889b17
commit
fa24ae3022
3 changed files with 70 additions and 15 deletions
|
@ -18,9 +18,15 @@ namespace Notepad.Extensions.Logging.FunctionalTest
|
||||||
var sp = sc.BuildServiceProvider();
|
var sp = sc.BuildServiceProvider();
|
||||||
var logger = sp.GetRequiredService<ILogger<Program>>();
|
var logger = sp.GetRequiredService<ILogger<Program>>();
|
||||||
|
|
||||||
logger.LogWarning("Here is a warning.");
|
//logger.LogWarning("Here is a warning.");
|
||||||
logger.LogError(GetException(), "oh no!.");
|
//logger.LogError(GetException(), "oh no!.");
|
||||||
logger.LogInformation("Here is some info.");
|
//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?.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static Exception GetException()
|
static Exception GetException()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace Microsoft.Extensions.Logging
|
namespace Microsoft.Extensions.Logging
|
||||||
{
|
{
|
||||||
|
@ -15,5 +16,13 @@ namespace Microsoft.Extensions.Logging
|
||||||
|
|
||||||
[DllImport("User32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
[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 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace Microsoft.Extensions.Logging
|
namespace Microsoft.Extensions.Logging
|
||||||
{
|
{
|
||||||
|
@ -6,28 +8,66 @@ namespace Microsoft.Extensions.Logging
|
||||||
{
|
{
|
||||||
public static IntPtr FindNotepadWindow()
|
public static IntPtr FindNotepadWindow()
|
||||||
{
|
{
|
||||||
var hwnd = FindMainWindow();
|
sb ??= new StringBuilder(4096);
|
||||||
IntPtr edit = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);
|
|
||||||
return edit;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static IntPtr FindMainWindow()
|
static IntPtr FindMainWindow()
|
||||||
{
|
{
|
||||||
IntPtr hwnd;
|
NativeMethods.EnumWindows(enumWindowsDelegate, IntPtr.Zero);
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
hwnd = NativeMethods.FindWindow(null, "Untitled - Notepad");
|
static NativeMethods.EnumWindowsDelegate enumWindowsDelegate = new NativeMethods.EnumWindowsDelegate(EnumWindowsCallback);
|
||||||
if (hwnd != IntPtr.Zero)
|
|
||||||
|
static bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam)
|
||||||
|
{
|
||||||
|
var result = NativeMethods.GetWindowText(hWnd, sb, sb.Capacity);
|
||||||
|
if (result < 0)
|
||||||
{
|
{
|
||||||
return hwnd;
|
throw new Win32Exception(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
hwnd = NativeMethods.FindWindow(null, "*Untitled - Notepad");
|
if (IsKnownNotepadWindow(sb.ToString()))
|
||||||
if (hwnd != IntPtr.Zero)
|
|
||||||
{
|
{
|
||||||
return hwnd;
|
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 IntPtr.Zero;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue