{"record":{"id":"41e0262796a7974f","repo":"dotnet/wpf","slug":"win32exception-commonxsendmessage","errorCode":null,"errorMessage":"Win32Exception","messagePattern":"Win32Exception","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/CommonXSendMessage.cs","lineNumber":1482,"sourceCode":"                                    return copyTo.GetStringAuto();\n                                }\n                            }\n                        }\n                    }\n                }\n            }\n\n            return \"\";\n        }\n\n        // This method will determine if the process is running in 32-bit emulation mode on a 64-bit machine.\n        private static bool IsWOW64Process(IntPtr hwnd)\n        {\n            using (SafeProcessHandle hProcess = new SafeProcessHandle(hwnd))\n            {\n                if (hProcess.IsInvalid)\n                {\n                    throw new Win32Exception();\n                }\n\n                // Windows XP(major version 5 and minor version 1) and above\n                if (Environment.OSVersion.Version.Major > 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 1))\n                {\n                    try\n                    {\n                        // IsWow64Process() implemented in Windows XP\n                        bool isWOW64Process;\n\n                        if (!Misc.IsWow64Process(hProcess, out isWOW64Process))\n                        {\n                            // Function failed. Assume not running under WOW64.\n                            return false;\n                        }\n\n                        return isWOW64Process;\n                    }","sourceCodeStart":1464,"sourceCodeEnd":1500,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/CommonXSendMessage.cs#L1464-L1500","documentation":"Win32Exception is thrown when a Win32 API call fails and the library surfaces the OS error code. Here IsWOW64Process opens a process handle from an HWND via SafeProcessHandle; if the handle is invalid (the window's owning process cannot be opened, e.g. access denied or the process exited), a parameterless Win32Exception is raised. This happens during remote-bitness detection when reading text within a UIAutomation structure.","triggerScenarios":"GetTextWithinStructure -> GetTextWithinStructureRemoteBitness -> IsWOW64Process is called with an HWND whose owning process handle cannot be created: the target process has already exited, is protected (elevated/protected-mode process), or the caller lacks PROCESS_QUERY_INFORMATION rights.","commonSituations":"Automating a window whose process crashes/closes mid-automation; inspecting elevated apps (Task Manager, UAC dialogs) from a non-elevated client; cross-session automation (services, different user sessions); Internet Explorer protected mode on legacy Windows.","solutions":["Re-check that the target window/process is still alive (IsWindow / HasExited) before querying and skip bitness detection if it died","Retry the operation after the window is re-resolved; transient invalid handles often mean the window was destroyed","Run the automation client with sufficient privileges (or the target unelevated) so the process handle can be opened","Catch Win32Exception around GetTextWithinStructure and fall back to non-bitness-aware text retrieval"],"exampleFix":"// before\nvar text = GetTextWithinStructure(hwnd);\n// after\nstring text = null;\nif (SafeNativeMethods.IsWindow(hwnd))\n{\n    try { text = GetTextWithinStructure(hwnd); }\n    catch (Win32Exception) { /* target process unavailable; degrade gracefully */ }\n}","handlingStrategy":"try-catch","validationCode":"if (!SafeNativeMethods.IsWindow(hwnd)) return; // window gone; skip bitness detection","typeGuard":"static bool CanQueryProcess(IntPtr hwnd) => SafeNativeMethods.IsWindow(hwnd);","tryCatchPattern":"try { var text = GetTextWithinStructure(hwnd); }\ncatch (Win32Exception ex) { Log.Warn($\"bitness/text query failed: {ex.NativeErrorCode}\"); /* fallback */ }","preventionTips":["Verify IsWindow before automating an HWND","Avoid automating elevated or protected processes from a lower-integrity client","Handle windows closing mid-automation gracefully"],"tags":["win32","ui-automation","process-handle"],"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"}