From 9806994399cc0da7cd0845b2c07588ef3d6b599f Mon Sep 17 00:00:00 2001 From: Giorgi Date: Tue, 18 Jun 2013 23:25:36 +0400 Subject: [PATCH] When Sendinput fails throw a Win32Exception with last error code --- WindowsInput/WindowsInputMessageDispatcher.cs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/WindowsInput/WindowsInputMessageDispatcher.cs b/WindowsInput/WindowsInputMessageDispatcher.cs index cac77c0..965fb26 100644 --- a/WindowsInput/WindowsInputMessageDispatcher.cs +++ b/WindowsInput/WindowsInputMessageDispatcher.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Runtime.InteropServices; using WindowsInput.Native; @@ -15,14 +16,27 @@ internal class WindowsInputMessageDispatcher : IInputMessageDispatcher /// The list of messages to be dispatched. /// If the array is empty. /// If the array is null. - /// If the any of the commands in the array could not be sent successfully. + /// If the any of the commands in the array could not be sent successfully. + /// If the any of the commands in the array could not be sent successfully and returns zero. public void DispatchInput(INPUT[] inputs) { if (inputs == null) throw new ArgumentNullException("inputs"); if (inputs.Length == 0) throw new ArgumentException("The input array was empty", "inputs"); - var successful = NativeMethods.SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof (INPUT))); + var successful = NativeMethods.SendInput((UInt32)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT))); if (successful != inputs.Length) - throw new Exception("Some simulated input commands were not sent successfully. The most common reason for this happening are the security features of Windows including User Interface Privacy Isolation (UIPI). Your application can only send commands to applications of the same or lower elevation. Similarly certain commands are restricted to Accessibility/UIAutomation applications. Refer to the project home page and the code samples for more information."); + { + //According to http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx SendInput failures caused by UIPI are not indicated by GetLastError calls. + var lastWin32Error = Marshal.GetLastWin32Error(); + if (lastWin32Error == 0) + { + throw new Exception("Some simulated input commands were not sent successfully. The most common reason for this happening are the security features of Windows including User Interface Privacy Isolation (UIPI). Your application can only send commands to applications of the same or lower elevation. Similarly certain commands are restricted to Accessibility/UIAutomation applications. Refer to the project home page and the code samples for more information."); + } + else + { + throw new Win32Exception(lastWin32Error); + } + } } } } \ No newline at end of file