{"record":{"id":"ef2592af2dd0a339","repo":"dotnet/wpf","slug":"invalidoperationexception-hwndwrapper","errorCode":null,"errorMessage":"InvalidOperationException","messagePattern":"InvalidOperationException","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs","lineNumber":295,"sourceCode":"\n            // return our result\n            return result;\n        }\n\n        private void CheckForCreateWindowFailure( IntPtr result, bool handled )\n        {\n            if( ! _isInCreateWindow )\n                return;\n            \n            if( IntPtr.Zero != result )\n            {\n                System.Diagnostics.Debug.WriteLine(\"Non-zero WndProc result=\" + result);\n                if( handled )\n                {\n                    if( System.Diagnostics.Debugger.IsAttached )\n                        System.Diagnostics.Debugger.Break();\n                    else\n                        throw new InvalidOperationException();\n                }\n            }\n        }\n\n\n        /// <summary>\n        /// Destroys the window with the given handle and class atom and unregisters its window class\n        /// </summary>\n        /// <param name=\"args\">A DestrowWindowParams instance</param>\n        internal static object DestroyWindow(object args)\n        {\n            nint handle = ((DestroyWindowArgs)args).Handle;\n            ushort classAtom = ((DestroyWindowArgs)args).ClassAtom;\n\n            Invariant.Assert(handle != 0,\n               \"Attempting to destroy an invalid hwnd\");\n\n            UnsafeNativeMethods.DestroyWindow(new HandleRef(null, handle));","sourceCodeStart":277,"sourceCodeEnd":313,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/HwndWrapper.cs#L277-L313","documentation":"CheckForCreateWindowFailure validates the result of CreateWindowEx inside HwndWrapper.WndProc. CreateWindowEx sends WM_CREATE (and WM_NCCREATE) synchronously, so a non-zero result with handled=true during creation means a handler claimed to handle a creation message but returned an invalid result — an internal invariant violation, surfaced as InvalidOperationException.","triggerScenarios":"A WM_NCCREATE/WM_CREATE handler in the WndProc pipeline sets handled=true while returning a non-zero (non-HWND-success) result during CreateWindowEx, most often from a custom WndProcHook or subclass that mishandles creation messages.","commonSituations":"Custom HwndWrapper subclasses or injected hooks that mark WM_CREATE handled and return wrong values; running under a debugger-detached environment where the Debug.Break path is skipped.","solutions":["Do not set handled=true for WM_NCCREATE/WM_CREATE messages in custom WndProc hooks, or return the correct result when doing so","Fix the hook so it returns 0/continues default processing for creation messages it does not fully handle","Reproduce under a debugger — the code deliberately breaks there to identify the offending handler","Check recently added WndProcHook callbacks registered on this HwndWrapper"],"exampleFix":"// before\ncase WM_CREATE:\n    handled = true;\n    return 1; // non-zero result with handled -> InvalidOperationException\n// after\ncase WM_CREATE:\n    return 0; // let default processing continue, handled stays false","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { wrapper.Create(); } catch (InvalidOperationException ex) { log(\"WM_CREATE handler returned invalid result\", ex); throw; }","preventionTips":["Never mark WM_NCCREATE/WM_CREATE handled unless you return the correct non-zero result CreateWindowEx expects","Return 0 (continue default processing) from hooks for creation messages you don't fully own","Debug with a debugger attached — the code Debug.Break()s there by design to pinpoint the bad handler"],"tags":["wpf","win32","wndproc","invariant"],"backgroundTag":"internal-invariant-violation","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"}