{"record":{"id":"ce31d5608c5bee30","repo":"dotnet/wpf","slug":"sr-childwindowmusthavecorrectparent","errorCode":null,"errorMessage":"SR.ChildWindowMustHaveCorrectParent","messagePattern":"SR\\.ChildWindowMustHaveCorrectParent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Interop/HwndHost.cs","lineNumber":1024,"sourceCode":"            // 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\n#if WCP_SERVER2003_OR_LATER_ENABLED\n            IntPtr hCurrentThread = UnsafeNativeMethods.GetCurrentThread();\n            if ((idWindowThread == SafeNativeMethods.GetThreadId(hCurrentThread)) &&\n                (idWindowProcess == UnsafeNativeMethods.GetProcessIdOfThread(hCurrentThread)))\n#else\n            if ((idWindowThread == SafeNativeMethods.GetCurrentThreadId()) &&","sourceCodeStart":1006,"sourceCodeEnd":1042,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Interop/HwndHost.cs#L1006-L1042","documentation":"HwndHost.BuildWindow checks that the hosted HWND's parent is exactly the parent handle WPF passed in. If GetParent(_hwnd) does not match hwndParent, it throws InvalidOperationException (SR.ChildWindowMustHaveCorrectParent), enforcing that the child window was created with (or reparented to) the expected WPF parent.","triggerScenarios":"BuildWindowCore creates the window with a different/NULL parent, reparents the window after creation, or another component re-parents the HWND before HwndHost validation runs.","commonSituations":"Wrapping pre-existing native windows without calling SetParent, native libraries that re-parent their own windows, passing IntPtr.Zero as parent in custom CreateWindow calls.","solutions":["Create the window with the hwndParent passed to BuildWindowCore as its parent.","If wrapping an existing window, call SetParent(existingHwnd, hwndParent.Handle) before returning it.","Check for third-party code re-parenting the window and remove or defer that reparenting."],"exampleFix":"// before\nprotected override HandleRef BuildWindowCore(HandleRef parent) {\n    return new HandleRef(this, CreateWindow(cls, IntPtr.Zero)); // NULL parent\n}\n// after\nprotected override HandleRef BuildWindowCore(HandleRef parent) {\n    return new HandleRef(this, CreateWindow(cls, parent.Handle));\n}","handlingStrategy":"validation","validationCode":"if (UnsafeNativeMethods.GetParent(hwnd) != parent.Handle)\n    UnsafeNativeMethods.SetParent(new HandleRef(null, hwnd), parent);","typeGuard":"bool HasExpectedParent(IntPtr child, IntPtr expectedParent) =>\n    UnsafeNativeMethods.GetParent(child) == expectedParent;","tryCatchPattern":null,"preventionTips":["Create hosted windows using exactly the hwndParent passed to BuildWindowCore.","Call SetParent when adopting existing windows into the WPF tree.","Audit native libraries for re-parenting behavior that changes the window's parent."],"tags":["wpf","hwndhost","win32","parenting"],"backgroundTag":"window-parent-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"}