{"record":{"id":"92274d0d9da6fe40","repo":"dotnet/wpf","slug":"win32-error","errorCode":null,"errorMessage":"Win32 error","messagePattern":"Win32 error","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Popup.cs","lineNumber":3220,"sourceCode":"\n                StringBuilder sb = new StringBuilder(NativeMethods.MAX_PATH);\n                // Search up from the last one until we find the first weboc hwnd.\n                while (lastHwnd != IntPtr.Zero)\n                {\n                    if (UnsafeNativeMethods.GetClassName(new HandleRef(null, lastHwnd), sb, NativeMethods.MAX_PATH) != 0)\n                    {\n                        if (string.Equals(sb.ToString(), WebOCWindowClassName, StringComparison.OrdinalIgnoreCase))\n                        {\n                            break;\n                        }\n                        else\n                        {\n                            lastHwnd = UnsafeNativeMethods.GetWindow(new HandleRef(null, lastHwnd), NativeMethods.GW_HWNDPREV);\n                        }\n                    }\n                    else\n                    {\n                        throw new Win32Exception();\n                    }\n                }\n\n                return lastHwnd;\n            }\n\n            internal void SetHitTestable(bool hitTestable)\n            {\n\n                // get the window handle\n                IntPtr handle = Handle;\n\n                Int32 styles = UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), NativeMethods.GWL_EXSTYLE);\n\n                int flags = styles;\n\n                if (((flags & NativeMethods.WS_EX_TRANSPARENT) == 0) != hitTestable)\n                {","sourceCodeStart":3202,"sourceCodeEnd":3238,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/Popup.cs#L3202-L3238","documentation":"Popup's internal window-anchoring helper walks sibling HWNDs with GetWindow(GW_HWNDPREV); when the Win32 call chain fails or returns an unexpected state it throws new Win32Exception(), which carries the last Win32 error as its message. This is an OS-level failure while positioning the popup's HWND relative to others.","triggerScenarios":"Win32 GetWindow failing during popup z-order/positioning (invalid or destroyed HWND, window closed concurrently, Win32 error such as ERROR_INVALID_WINDOW_HANDLE); usually under stress, rapid open/close, or across desktops/session transitions (e.g. RDP disconnect).","commonSituations":"Popups shown during window teardown, heavy automation/UITests, RDP session changes, or low-memory conditions where HWNDs are destroyed while the popup is being positioned.","solutions":["Inspect Win32Exception.NativeErrorCode to identify the Win32 failure and the lifecycle race causing it.","Guard popup open/close ordering: don't close the owner window while a popup is being positioned (defer Close with Dispatcher).","Retry the operation — transient races during session changes (RDP, lock screen) usually clear.","Keep the WPF runtime current; several popup HWND race fixes shipped in .NET/WPF servicing releases."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if (!popup.IsOpen) return; // don't operate on a popup whose HWND may be gone","typeGuard":null,"tryCatchPattern":"try { popup.IsOpen = true; }\ncatch (Win32Exception wex) { Log(wex.NativeErrorCode); /* retry after dispatcher idle */ }","preventionTips":["Avoid closing owner windows while popups are positioning","Retry transient Win32 failures after Dispatcher.BeginInvoke","Keep WPF/.NET patched — popup HWND race fixes ship in servicing releases"],"tags":["wpf","popup","win32","hwnd"],"backgroundTag":"win32-api-error","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"}