Skip to content

Commit 65f5399

Browse files
committed
Changes to handle changes in Notepad that allows multiple documents.
1 parent 1728be2 commit 65f5399

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/Serilog.Sinks.Notepad/Sinks/Notepad/Interop/NotepadTextWriter.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ private static IntPtr FindNotepadEditorHandle(IntPtr notepadWindowHandle)
123123
}
124124

125125
// Issue #59 - Alternate way of finding the RichEditD2DPT class:
126-
if (FindEditorHandleThroughChildWindows(notepadWindowHandle) is var childRichEditHandle
127-
&& childRichEditHandle != IntPtr.Zero)
126+
if (FindEditorHandleThroughChildWindows(notepadWindowHandle) is var richEditHandleFromChildren
127+
&& richEditHandleFromChildren != IntPtr.Zero)
128128
{
129-
return childRichEditHandle;
129+
return richEditHandleFromChildren;
130130
}
131131

132132
return User32.FindWindowEx(notepadWindowHandle, IntPtr.Zero, "Edit", null);
@@ -140,6 +140,13 @@ private void EnsureNotDisposed()
140140
}
141141
}
142142

143+
private static string GetClassNameFromWindow(IntPtr handle)
144+
{
145+
StringBuilder sb = new StringBuilder(256);
146+
var ret = User32.GetClassName(handle, sb, sb.Capacity);
147+
return ret != 0 ? sb.ToString() : string.Empty;
148+
}
149+
143150
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
144151
{
145152
GCHandle gch = GCHandle.FromIntPtr(pointer);
@@ -149,18 +156,20 @@ private static bool EnumWindow(IntPtr handle, IntPtr pointer)
149156
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
150157
}
151158

152-
// We only want windows of class RichEditD2DPT.
153-
if (User32.FindWindowEx(handle, IntPtr.Zero, "RichEditD2DPT", null) != IntPtr.Zero)
159+
if (string.Equals(GetClassNameFromWindow(handle), "RichEditD2DPT", StringComparison.OrdinalIgnoreCase))
154160
{
155161
list.Add(handle);
162+
163+
// Stop enumerating - we found the one.
164+
return false;
156165
}
157166

158167
return true;
159168
}
160169

161170
private static IntPtr FindEditorHandleThroughChildWindows(IntPtr notepadWindowHandle)
162171
{
163-
List<IntPtr> result = new List<IntPtr>();
172+
List<IntPtr> result = new List<IntPtr>(1);
164173
GCHandle listHandle = GCHandle.Alloc(result);
165174
try
166175
{

src/Serilog.Sinks.Notepad/Sinks/Notepad/Interop/User32.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ internal class User32
3737
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
3838

3939

40-
[DllImport("user32.dll")]
41-
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
42-
4340
// Needed for EnumChildWindows for registering a call back function.
4441
public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
4542

46-
[DllImport("user32.Dll")]
43+
[DllImport("user32.Dll", CharSet = CharSet.Unicode, SetLastError = true)]
4744
[return: MarshalAs(UnmanagedType.Bool)]
4845
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);
46+
47+
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
48+
public static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);
49+
4950
}
5051
}

0 commit comments

Comments
 (0)