{"record":{"id":"6e2821b139fb897d","repo":"dotnet/wpf","slug":"win32exception-marshal-getlastwin32error-input","errorCode":null,"errorMessage":"Win32Exception(Marshal.GetLastWin32Error())","messagePattern":"Win32Exception\\(Marshal\\.GetLastWin32Error\\(\\)\\)","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Input.cs","lineNumber":120,"sourceCode":"                // a particular pixel, target its rectangle's midpoint, not its edge.\n                x = ( ( x - vscreenLeft ) * 65536 ) / vscreenWidth + 65536 / ( vscreenWidth * 2 );\n                y = ( ( y - vscreenTop ) * 65536 ) / vscreenHeight + 65536 / ( vscreenHeight * 2 );\n\n                intflags |= NativeMethods.MOUSEEVENTF_VIRTUALDESK;\n            }\n\n            NativeMethods.INPUT mi = new NativeMethods.INPUT\n            {\n                type = NativeMethods.INPUT_MOUSE\n            };\n            mi.union.mouseInput.dx = (int) x;\n            mi.union.mouseInput.dy = (int)y;\n            mi.union.mouseInput.mouseData = data;\n            mi.union.mouseInput.dwFlags = intflags;\n            mi.union.mouseInput.time = 0;\n            mi.union.mouseInput.dwExtraInfo = new IntPtr( 0 );\n            if( UnsafeNativeMethods.SendInput( 1, ref mi, Marshal.SizeOf(mi) ) == 0 )\n                throw new Win32Exception(Marshal.GetLastWin32Error());\n        }\n\n        // Inject keyboard input into the system\n        internal static void SendKeyboardInput( Key key, bool press )\n        {\n            NativeMethods.INPUT ki = new NativeMethods.INPUT\n            {\n                type = NativeMethods.INPUT_KEYBOARD\n            };\n            ki.union.keyboardInput.wVk = (short) KeyInterop.VirtualKeyFromKey( key );\n            ki.union.keyboardInput.wScan = (short)SafeNativeMethods.MapVirtualKey(ki.union.keyboardInput.wVk, 0);\n            int dwFlags = 0;\n            if( ki.union.keyboardInput.wScan > 0 )\n                dwFlags |= NativeMethods.KEYEVENTF_SCANCODE;\n            if( !press )\n                dwFlags |= NativeMethods.KEYEVENTF_KEYUP;\n            ki.union.keyboardInput.dwFlags = dwFlags;\n            if (IsExtendedKey(ki.union.keyboardInput.wVk))","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Input.cs#L102-L138","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nInput.SendMouseInput(x, y, data, flags);\n// after\ntry\n{\n    Input.SendMouseInput(x, y, data, flags);\n}\ncatch (Win32Exception ex) when (ex.NativeErrorCode == 5)\n{\n    // target is elevated/UIPI-blocked; use UIA patterns instead of raw input\n}","handlingStrategy":"try-catch","validationCode":"bool canInject = (bool)target.GetCurrentPropertyValue(AutomationElement.IsEnabledProperty) && Environment.UserInteractive;","typeGuard":null,"tryCatchPattern":"try { Input.SendMouseInput(x, y, 0, flags); }\ncatch (Win32Exception ex) when (ex.NativeErrorCode == 5) { /* UIPI blocked: use UIA patterns */ }","preventionTips":["Run client and target at matching integrity levels","Keep automation in an interactive, unlocked session","Prefer UIA patterns over synthetic input"],"tags":["win32","ui-automation","mouse-input","uipi"],"backgroundTag":"permission-denied","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}