dotnet/wpf · error · Win32Exception
Win32Exception(Marshal.GetLastWin32Error())
Error message
Win32Exception(Marshal.GetLastWin32Error())
What it means
SendInput injected the mouse input event but returned 0, meaning Windows rejected the injection. The library throws Win32Exception carrying Marshal.GetLastWin32Error() to expose the underlying OS error (commonly ERROR_ACCESS_DENIED).
Solutions
- Run the automation client at the same or higher integrity level than the target app, or lower the target app's elevation
- Ensure the test runs in an interactive desktop session (not a service/session 0); keep the machine unlocked during automation
- Check the Win32Exception.NativeErrorCode: 5 (access denied) indicates UIPI/integrity issues; 0 or unexpected codes indicate a marshaling/INPUT structure problem
- Use alternative patterns (InvokePattern/ValuePattern) instead of raw mouse injection where possible
Example fix
// before
Input.SendMouseInput(x, y, data, flags);
// after
try
{
Input.SendMouseInput(x, y, data, flags);
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 5)
{
// target is elevated/UIPI-blocked; use UIA patterns instead of raw input
} Defensive patterns
Strategy: try-catch
Validate before calling
bool canInject = (bool)target.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) && Environment.UserInteractive;
Try / catch
try { Input.SendMouseInput(x, y, 0, flags); }
catch (Win32Exception ex) when (ex.NativeErrorCode == 5) { /* UIPI blocked: use UIA patterns */ } Prevention
- Run client and target at matching integrity levels
- Keep automation in an interactive, unlocked session
- Prefer UIA patterns over synthetic input
When it happens
Trigger: Input.SendMouseInput calls UnsafeNativeMethods.SendInput while input injection is blocked: a UAP/elevated process owns the foreground window, the session is not the interactive one (service/remote session), or the desktop is locked / a screensaver or UAC prompt is active.
Common situations: Running UI tests from a Windows service or CI agent without an interactive desktop; automating elevated apps from a standard user process; UIPI blocking injection into higher-integrity windows; test runs on locked build machines.
Understand the failure class
Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.
Related errors
- ElementNotAvailableException
- ElementNotEnabledException
- InvalidOperationException(SR.OperationCannotBePerformed, e)
- Win32Exception
- ArgumentNullException
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6e2821b139fb897d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Input.cs:120
// a particular pixel, target its rectangle's midpoint, not its edge.
x = ( ( x - vscreenLeft ) * 65536 ) / vscreenWidth + 65536 / ( vscreenWidth * 2 );
y = ( ( y - vscreenTop ) * 65536 ) / vscreenHeight + 65536 / ( vscreenHeight * 2 );
intflags |= NativeMethods.MOUSEEVENTF_VIRTUALDESK;
}
NativeMethods.INPUT mi = new NativeMethods.INPUT
{
type = NativeMethods.INPUT_MOUSE
};
mi.union.mouseInput.dx = (int) x;
mi.union.mouseInput.dy = (int)y;
mi.union.mouseInput.mouseData = data;
mi.union.mouseInput.dwFlags = intflags;
mi.union.mouseInput.time = 0;
mi.union.mouseInput.dwExtraInfo = new IntPtr( 0 );
if( UnsafeNativeMethods.SendInput( 1, ref mi, Marshal.SizeOf(mi) ) == 0 )
throw new Win32Exception(Marshal.GetLastWin32Error());
}
// Inject keyboard input into the system
internal static void SendKeyboardInput( Key key, bool press )
{
NativeMethods.INPUT ki = new NativeMethods.INPUT
{
type = NativeMethods.INPUT_KEYBOARD
};
ki.union.keyboardInput.wVk = (short) KeyInterop.VirtualKeyFromKey( key );
ki.union.keyboardInput.wScan = (short)SafeNativeMethods.MapVirtualKey(ki.union.keyboardInput.wVk, 0);
int dwFlags = 0;
if( ki.union.keyboardInput.wScan > 0 )
dwFlags |= NativeMethods.KEYEVENTF_SCANCODE;
if( !press )
dwFlags |= NativeMethods.KEYEVENTF_KEYUP;
ki.union.keyboardInput.dwFlags = dwFlags;
if (IsExtendedKey(ki.union.keyboardInput.wVk))View on GitHub (pinned to 81131a70a4)