Skip to content

Commit ce5023a

Browse files
authored
Logs result of SendInput and PostMessage calls (#23)
* Logs result of SendInput calls Adds logging to track the number of input events successfully sent by SendInput. This helps diagnose potential issues where not all input events are processed correctly. It logs an error if the number of sent events doesn't match the expected count and a trace if all events are successfully sent. * Logs PostMessage results Adds logging to record the success or failure of PostMessage calls. Logs the last Win32 error when PostMessage fails, aiding in debugging. Logs successful message posts for tracing purposes.
1 parent d7b693f commit ce5023a

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

WinSyncScroll/ViewModels/MainViewModel.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,20 @@ private async Task RunMouseEventProcessingLoopAsync(
343343
if (!_options.Value.IsLegacyModeEnabled)
344344
{
345345
LogSendInput(inputs);
346-
PInvoke.SendInput(inputs.AsSpan(), SizeOfInput);
346+
var sentCount = PInvoke.SendInput(inputs.AsSpan(), SizeOfInput);
347+
348+
if (sentCount != inputs.Length)
349+
{
350+
var lastError = Marshal.GetLastWin32Error();
351+
_logger.LogError("SendInput failed: expected to send {ExpectedCount} events, but only {SentCount} were sent. Last Win32 error: {LastError}",
352+
inputs.Length,
353+
sentCount,
354+
lastError);
355+
}
356+
else
357+
{
358+
_logger.LogTrace("Successfully sent {SentCount} input events", sentCount);
359+
}
347360
}
348361
else
349362
{
@@ -369,7 +382,20 @@ private async Task RunMouseEventProcessingLoopAsync(
369382
lParam,
370383
targetX,
371384
targetY);
372-
PInvoke.PostMessage(windowHandle, (uint)buffer.MouseMessageId, (nuint)buffer.MouseMessageData.mouseData, lParam);
385+
386+
var result = PInvoke.PostMessage(windowHandle, (uint)buffer.MouseMessageId, (nuint)buffer.MouseMessageData.mouseData, lParam);
387+
388+
if (!result)
389+
{
390+
var lastError = Marshal.GetLastWin32Error();
391+
_logger.LogError("PostMessage failed for window {WindowHandle}: Last Win32 error: {LastError}",
392+
windowHandle,
393+
lastError);
394+
}
395+
else
396+
{
397+
_logger.LogTrace("Successfully posted message to window {WindowHandle}", windowHandle);
398+
}
373399
}
374400
}
375401
}

0 commit comments

Comments
 (0)