{"record":{"id":"b158a0fdc40b6860","repo":"dotnet/wpf","slug":"sr-hostedwindowmustbeachildwindow","errorCode":null,"errorMessage":"SR.HostedWindowMustBeAChildWindow","messagePattern":"SR\\.HostedWindowMustBeAChildWindow","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Interop/HwndHost.cs","lineNumber":1018,"sourceCode":"\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            {\n                _hasDpiAwarenessContextTransition = true;\n            }\n\n            // Only subclass the child HWND if it is owned by our thread.\n            int idWindowProcess;\n            int idWindowThread = UnsafeNativeMethods.GetWindowThreadProcessId(_hwnd, out idWindowProcess);\n","sourceCodeStart":1000,"sourceCodeEnd":1036,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Interop/HwndHost.cs#L1000-L1036","documentation":"After BuildWindowCore returns, HwndHost verifies that the created window has the WS_CHILD style. If the window is not a child window, BuildWindow throws InvalidOperationException (SR.HostedWindowMustBeAChildWindow) because HwndHost can only host child windows inside the WPF window hierarchy.","triggerScenarios":"A derived BuildWindowCore creates a top-level (WS_POPUP/WS_OVERLAPPED) window, or creates a child window then removes/overrides the WS_CHILD style bit.","commonSituations":"Reusing existing top-level native windows as hosted content, copying window-creation code that omits WS_CHILD, or altering styles via SetWindowLong after creation.","solutions":["Create the hosted window with the WS_CHILD style and the WPF-provided hwndParent.","If wrapping an existing window, reparent it with SetParent to the WPF parent and add WS_CHILD via SetWindowLong.","Do not strip WS_CHILD in post-creation style adjustments."],"exampleFix":"// before\nCreateWindowEx(0, cls, \"\", WS_OVERLAPPEDWINDOW, ...);\n// after\nCreateWindowEx(0, cls, \"\", WS_CHILD | WS_VISIBLE, ...);","handlingStrategy":"validation","validationCode":"int style = UnsafeNativeMethods.GetWindowLong(new HandleRef(null, hwnd), NativeMethods.GWL_STYLE);\nif ((style & NativeMethods.WS_CHILD) == 0)\n    throw new InvalidOperationException(\"Hosted window must have WS_CHILD style\");","typeGuard":"bool IsChildWindow(IntPtr hwnd) =>\n    (UnsafeNativeMethods.GetWindowLong(new HandleRef(null, hwnd), NativeMethods.GWL_STYLE)\n        & NativeMethods.WS_CHILD) != 0;","tryCatchPattern":null,"preventionTips":["Always include WS_CHILD in window creation styles for HwndHost content.","Do not modify window styles after creation in ways that remove WS_CHILD.","Use SetWindowLong to add WS_CHILD when wrapping pre-existing windows."],"tags":["wpf","hwndhost","win32","window-styles"],"backgroundTag":"window-style-mismatch","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"}