Skip to content

Commit 7ab2698

Browse files
PR rebase & minor tweaks
1 parent 6db95e1 commit 7ab2698

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

sample/ConsoleDemo/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ private static void Main(string[] args)
3333

3434
try
3535
{
36-
//Console.WriteLine("Open a `notepad.exe` instance and press <enter> to continue...");
37-
//Console.ReadLine();
36+
Console.WriteLine("Open a `notepad.exe` instance and press <enter> to continue...");
37+
Console.ReadLine();
3838

3939
Console.WriteLine("Writing messages to the most recent Notepad you opened...");
4040

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ public override void Flush()
7474
}
7575

7676
// Get how many characters are in the Notepad editor already
77-
var textLength = User32.SendMessage(_currentNotepadEditorHandle, User32.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
77+
var textLengthBefore = User32.SendMessage(_currentNotepadEditorHandle, User32.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
7878

7979
// Set the caret position to the end of the text
80-
User32.SendMessage(_currentNotepadEditorHandle, User32.EM_SETSEL, (IntPtr)textLength, (IntPtr)textLength);
80+
User32.SendMessage(_currentNotepadEditorHandle, User32.EM_SETSEL, (IntPtr)textLengthBefore, (IntPtr)textLengthBefore);
8181

8282
var buffer = base.GetStringBuilder();
8383
var message = buffer.ToString();
@@ -88,13 +88,17 @@ public override void Flush()
8888
// Get how many characters are in the Notepad editor after putting in new text
8989
var textLengthAfter = User32.SendMessage(_currentNotepadEditorHandle, User32.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);
9090

91-
// If no change in textLength, reset editor handle to try to find it again.
92-
if (textLengthAfter == textLength)
91+
// If no change in the text length, reset editor handle to try to find it again.
92+
if (textLengthAfter == textLengthBefore)
93+
{
9394
_currentNotepadEditorHandle = IntPtr.Zero;
94-
95-
// Otherwise, we clear the buffer
95+
}
9696
else
97+
{
98+
// Otherwise, we clear the buffer
99+
97100
buffer.Clear();
101+
}
98102
}
99103

100104
protected override void Dispose(bool disposing)
@@ -161,9 +165,10 @@ private static string GetClassNameFromWindow(IntPtr handle)
161165

162166
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
163167
{
164-
GCHandle gch = GCHandle.FromIntPtr(pointer);
165-
List<IntPtr> list = gch.Target as List<IntPtr>;
166-
if (list == null)
168+
var gch = GCHandle.FromIntPtr(pointer);
169+
var list = gch.Target as List<IntPtr>;
170+
171+
if (list is null)
167172
{
168173
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
169174
}
@@ -181,8 +186,9 @@ private static bool EnumWindow(IntPtr handle, IntPtr pointer)
181186

182187
private static IntPtr FindEditorHandleThroughChildWindows(IntPtr notepadWindowHandle)
183188
{
184-
List<IntPtr> result = new List<IntPtr>(1);
185-
GCHandle listHandle = GCHandle.Alloc(result);
189+
var result = new List<IntPtr>(1);
190+
var listHandle = GCHandle.Alloc(result);
191+
186192
try
187193
{
188194
User32.Win32Callback childProc = new User32.Win32Callback(EnumWindow);
@@ -191,8 +197,11 @@ private static IntPtr FindEditorHandleThroughChildWindows(IntPtr notepadWindowHa
191197
finally
192198
{
193199
if (listHandle.IsAllocated)
200+
{
194201
listHandle.Free();
202+
}
195203
}
204+
196205
return result.FirstOrDefault();
197206
}
198207
}

0 commit comments

Comments
 (0)