{"record":{"id":"aa6068e8434f59a9","repo":"dotnet/wpf","slug":"error-noaccess-998-error-access-denied-5","errorCode":"ERROR_NOACCESS (998) / ERROR_ACCESS_DENIED (5)","errorMessage":"InvalidOperationException","messagePattern":"InvalidOperationException","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs","lineNumber":1695,"sourceCode":"                case 1404:  // 1404 ERROR_INVALID_HOOK_HANDLE       Invalid hook handle.\n                case 1405:  // 1405 ERROR_INVALID_DWP_HANDLE        Invalid handle to a multiple-window position structure.\n                case 1406:  // 1406 ERROR_TLW_WITH_WSCHILD          Cannot create a top-level child window.\n                case 1407:  // 1407 ERROR_CANNOT_FIND_WND_CLASS     Cannot find window class.\n                case 1408:  // 1408 ERROR_WINDOW_OF_OTHER_THREAD    Invalid window; it belongs to other thread.\n                    throw new ElementNotAvailableException();\n\n                // We're getting this in AMD64 when calling RealGetWindowClass; adding this code\n                // to allow the DRTs to pass while we continue investigation.\n                case 87:    //   87 ERROR_INVALID_PARAMETER\n                    throw new ElementNotAvailableException();\n\n                case 8:     //    8 ERROR_NOT_ENOUGH_MEMORY         Not enough storage is available to process this command.\n                case 14:    //   14 ERROR_OUTOFMEMORY               Not enough storage is available to complete this operation.\n                    throw new OutOfMemoryException();\n\n                case 998:   //  998 ERROR_NOACCESS                  Invalid access to memory location.\n                case 5:     //    5 ERROR_ACCESS_DENIED\n                    throw new InvalidOperationException();\n\n                case 121:   //  121 ERROR_SEM_TIMEOUT               The semaphore timeout period has expired.\n                case 258:   //  258 WAIT_TIMEOUT                    The wait operation timed out.\n                case 1053:  // 1053 ERROR_SERVICE_REQUEST_TIMEOUT   The service did not respond to the start or control request in a timely fashion.\n                case 1460:  // 1460 ERROR_TIMEOUT                   This operation returned because the timeout period expired.\n                    throw new TimeoutException();\n\n                default:\n                    // Not sure how to map the reset of the error codes so throw generic Win32Exception.\n                    throw new Win32Exception(errorCode);\n            }\n        }\n\n        internal static bool UnhookWinEvent(IntPtr winEventHook)\n        {\n            // There is no indication in the Windows SDK documentation that UnhookWinEvent()\n            // will set an error to be retrieved with GetLastError\n            return UnsafeNativeMethods.UnhookWinEvent(winEventHook);","sourceCodeStart":1677,"sourceCodeEnd":1713,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/Misc.cs#L1677-L1713","documentation":"The Win32 error mapper throws InvalidOperationException for ERROR_NOACCESS (998, invalid access to a memory location) and ERROR_ACCESS_DENIED (5). The native call was refused — either a bad memory access or insufficient rights — and the library maps this to the standard 'operation cannot be performed' exception rather than a UIA-specific one.","triggerScenarios":"Win32 calls made by UIA client proxies failing with last error 998 or 5 — accessing a window of an elevated/other-session process, or a native API writing to an invalid buffer.","commonSituations":"Automating an app running elevated (admin) from a non-elevated automation process; UIPI/mandatory-integrity-level blocks on UAC prompts; cross-session (RDP/service) access attempts.","solutions":["Run the automation client at the same or higher elevation/integrity level as the target app","Avoid automating secure UI (UAC consent, elevated windows) — UIPI blocks it by design","Catch InvalidOperationException and treat the target as blocked rather than broken","If 998 appears in your own P/Invoke, check buffer sizes and marshaling before blaming the target"],"exampleFix":"// before\nvar info = GetWindowInfo(hwnd); // error 5/998 -> InvalidOperationException\n// after\ntry\n{\n    var info = GetWindowInfo(hwnd);\n}\ncatch (InvalidOperationException)\n{\n    // access denied / invalid access: target likely elevated; require matching elevation\n    info = null;\n}","handlingStrategy":"validation","validationCode":"// Compare integrity levels before automating a window:\nbool canAccess = GetProcessIntegrityLevel(targetPid) <= GetProcessIntegrityLevel(currentPid);","typeGuard":null,"tryCatchPattern":"try { QueryWindow(hwnd); } catch (InvalidOperationException) { ElevateOrSkip(target); }","preventionTips":["Run the automation client with elevation matching or exceeding the target app","Never attempt to automate UAC consent or other secure desktop UI","Check process integrity levels before cross-process window queries","When running from a service/session, verify you target windows in your own session"],"tags":["ui-automation","access-denied","uac","win32-error"],"backgroundTag":"permission-denied","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}