{"record":{"id":"a22578ac31940246","repo":"dotnet/wpf","slug":"sr-hwndtarget-windowalreadyhascontent","errorCode":null,"errorMessage":"SR.HwndTarget_WindowAlreadyHasContent","messagePattern":"SR\\.HwndTarget_WindowAlreadyHasContent","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndTarget.cs","lineNumber":550,"sourceCode":"                    SR.HwndTarget_InvalidWindowProcess,\n                    nameof(hwnd)\n                    );\n            }\n            else if (threadId != SafeNativeMethods.GetCurrentThreadId())\n            {\n                throw new ArgumentException(\n                    SR.HwndTarget_InvalidWindowThread,\n                    nameof(hwnd)\n                    );\n            }\n\n            int hr = VisualTarget_AttachToHwnd(hwnd);\n\n            if (HRESULT.Failed(hr))\n            {\n                if (hr == unchecked((int)0x80070005)) // E_ACCESSDENIED\n                {\n                    throw new InvalidOperationException(\n                        SR.HwndTarget_WindowAlreadyHasContent\n                        );\n                }\n                else\n                {\n                    HRESULT.Check(hr);\n                }\n            }\n\n            EnsureNotificationWindow();\n            _notificationWindowHelper.AttachHwndTarget(this);\n            UnsafeNativeMethods.WTSRegisterSessionNotification(hwnd, NativeMethods.NOTIFY_FOR_THIS_SESSION);\n        }\n\n        [DllImport(DllImport.MilCore, EntryPoint = \"MilVisualTarget_AttachToHwnd\")]\n        internal static extern int VisualTarget_AttachToHwnd(\n            IntPtr hwnd\n            );","sourceCodeStart":532,"sourceCodeEnd":568,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndTarget.cs#L532-L568","documentation":"After VisualTarget_AttachToHwnd returns E_ACCESSDENIED (0x80070005), HwndTarget translates that HRESULT into InvalidOperationException (SR.HwndTarget_WindowAlreadyHasContent). It means the Win32 window already has another rendering target attached, and a window can host only one HwndTarget's composition content.","triggerScenarios":"Creating a second HwndSource (or calling HwndSource.FromHwnd) on an HWND that already has WPF content; attaching to a WPF Window's own hwnd from custom code.","commonSituations":"Trying to host WPF content inside an existing WPF window via interop instead of adding it to the visual tree; re-parenting an HwndSource onto a window that already has one.","solutions":["Use a fresh child HWND (e.g. via HwndHost) for the new HwndSource instead of the window that already has content","Add the content to the existing WPF visual tree rather than interop-attaching to the same hwnd","Ensure the previous HwndSource on that hwnd is disposed before creating a new one"],"exampleFix":"// before\nvar s2 = HwndSource.FromHwnd(existingWpfWindowHwnd); // E_ACCESSDENIED -> throws\n// after\nvar host = new HwndHost();\nexistingPanel.Children.Add(host); // host renders into its own child hwnd","handlingStrategy":"validation","validationCode":"if (HwndSource.FromHwnd(hwnd) != null) throw new InvalidOperationException(\"window already hosts WPF content\");","typeGuard":null,"tryCatchPattern":"try { new HwndSource(...); } catch (InvalidOperationException) { /* reuse existing source or attach to a child hwnd */ }","preventionTips":["One HwndSource per HWND — track created sources in a map","Add WPF content via the visual tree instead of re-attaching to an existing window's hwnd","Dispose old HwndSource before creating a replacement"],"tags":["wpf","interop","hwnd","rendering"],"backgroundTag":"invalid-state-transition","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"}