{"record":{"id":"d3519805d390d4f3","repo":"dotnet/wpf","slug":"sr-childwindownotcreated","errorCode":null,"errorMessage":"SR.ChildWindowNotCreated","messagePattern":"SR\\.ChildWindowNotCreated","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Interop/HwndHost.cs","lineNumber":1011,"sourceCode":"            finally\n            {\n                // Be careful to clear our guard bit.\n                _isBuildingWindow = false;\n            }\n        }\n\n\n        private void BuildWindow(HandleRef hwndParent)\n        {\n            // Demand unmanaged code to the caller. IT'S RISKY TO REMOVE THIS\n            DemandIfUntrusted();\n\n            // Allow the derived class to build our HWND.\n            _hwnd = BuildWindowCore(hwndParent);\n\n            if(_hwnd.Handle == IntPtr.Zero || !UnsafeNativeMethods.IsWindow(_hwnd))\n            {\n                throw new InvalidOperationException(SR.ChildWindowNotCreated);\n            }\n\n            // Make sure that the window that was created is indeed a child window.\n            int windowStyle = UnsafeNativeMethods.GetWindowLong(new HandleRef(this,_hwnd.Handle), NativeMethods.GWL_STYLE);\n            if((windowStyle & NativeMethods.WS_CHILD) == 0)\n            {\n                throw new InvalidOperationException(SR.HostedWindowMustBeAChildWindow);\n            }\n\n            // Make sure the child window is the child of the expected parent window.\n            if(hwndParent.Handle != UnsafeNativeMethods.GetParent(_hwnd))\n            {\n                throw new InvalidOperationException(SR.ChildWindowMustHaveCorrectParent);\n            }\n\n            // Test to see if hwndParent and _hwnd have different DPI_AWARENESS_CONTEXT's\n            if (DpiUtil.GetDpiAwarenessContext(_hwnd.Handle) != DpiUtil.GetDpiAwarenessContext(hwndParent.Handle))\n            {","sourceCodeStart":993,"sourceCodeEnd":1029,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Interop/HwndHost.cs#L993-L1029","documentation":"HwndHost.BuildWindow validates the HWND returned by the derived class's BuildWindowCore override. If the returned handle is IntPtr.Zero or no longer a valid window (IsWindow fails), it throws InvalidOperationException (SR.ChildWindowNotCreated) because the subclass failed to create its child window.","triggerScenarios":"A derived HwndHost's BuildWindowCore returned a null/zero HandleRef without throwing, or returned a handle to a window that was already destroyed before validation.","commonSituations":"Custom HwndHost subclasses wrapping native controls whose creation failed silently, wrong parent handles, native DLL issues, or returning a destroyed handle on re-hosting.","solutions":["Fix BuildWindowCore so it returns a valid, live HWND; check the native CreateWindow/CreateWindowEx result before returning.","If native creation fails, throw a meaningful exception from BuildWindowCore instead of returning zero.","Ensure the native control is created on the same UI thread and not destroyed prematurely."],"exampleFix":"// before\nprotected override HandleRef BuildWindowCore(HandleRef parent) =>\n    new HandleRef(this, _maybeZeroHwnd);\n// after\nprotected override HandleRef BuildWindowCore(HandleRef parent) {\n    var hwnd = NativeMethods.CreateControlWindow(parent.Handle);\n    if (hwnd == IntPtr.Zero)\n        throw new Win32Exception(Marshal.GetLastWin32Error());\n    return new HandleRef(this, hwnd);\n}","handlingStrategy":"validation","validationCode":"var hwnd = NativeCreateWindow(...);\nif (hwnd == IntPtr.Zero || !UnsafeNativeMethods.IsWindow(hwnd))\n    throw new Win32Exception(Marshal.GetLastWin32Error());\n// then return it from BuildWindowCore","typeGuard":"bool IsValidHwnd(IntPtr hwnd) => hwnd != IntPtr.Zero && UnsafeNativeMethods.IsWindow(hwnd);","tryCatchPattern":"try\n{\n    hwndHost.Measure(arrangeSize);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"child window\"))\n{\n    Log($\"BuildWindowCore returned an invalid HWND: {ex}\");\n}","preventionTips":["Check native CreateWindow return values and GetLastError in BuildWindowCore.","Throw early with a specific message instead of returning a zero handle.","Verify the native control exists and is created on the UI thread."],"tags":["wpf","hwndhost","interop","win32"],"backgroundTag":"window-handle-creation-failed","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"}