Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions WindowsInput/WindowsInputMessageDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using WindowsInput.Native;

Expand All @@ -15,14 +16,27 @@ internal class WindowsInputMessageDispatcher : IInputMessageDispatcher
/// <param name="inputs">The list of <see cref="INPUT"/> messages to be dispatched.</param>
/// <exception cref="ArgumentException">If the <paramref name="inputs"/> array is empty.</exception>
/// <exception cref="ArgumentNullException">If the <paramref name="inputs"/> array is null.</exception>
/// <exception cref="Exception">If the any of the commands in the <paramref name="inputs"/> array could not be sent successfully.</exception>
/// <exception cref="Win32Exception">If the any of the commands in the <paramref name="inputs"/> array could not be sent successfully.</exception>
/// <exception cref="Exception">If the any of the commands in the <paramref name="inputs"/> array could not be sent successfully and <see
/// cref="Marshal.GetLastWin32Error"></see> returns zero.</exception>
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);
}
}
}
}
}