{"record":{"id":"7ea696448cc31311","repo":"dotnet/wpf","slug":"sr-hwndtarget-invalidwindowprocess","errorCode":null,"errorMessage":"SR.HwndTarget_InvalidWindowProcess","messagePattern":"SR\\.HwndTarget_InvalidWindowProcess","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndTarget.cs","lineNumber":531,"sourceCode":"        /// </summary>\n        private void AttachToHwnd(IntPtr hwnd)\n        {\n            int processId = 0;\n            int threadId = UnsafeNativeMethods.GetWindowThreadProcessId(\n                new HandleRef(this, hwnd),\n                out processId\n                );\n\n            if (!UnsafeNativeMethods.IsWindow(new HandleRef(this, hwnd)))\n            {\n                throw new ArgumentException(\n                    SR.HwndTarget_InvalidWindowHandle,\n                    nameof(hwnd)\n                    );\n            }\n            else if (processId != Environment.ProcessId)\n            {\n                throw new ArgumentException(\n                    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                {","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndTarget.cs#L513-L549","documentation":"AttachToHwnd requires the target window to belong to the current process. After reading the hwnd's owning process id via GetWindowThreadProcessId, a mismatch with Environment.ProcessId throws ArgumentException (SR.HwndTarget_InvalidWindowProcess). WPF cannot render into another process's window.","triggerScenarios":"Creating an HwndSource/HwndTarget over an HWND owned by a different process (e.g. a handle obtained via FindWindow/EnumWindows of an external app).","commonSituations":"Attempts to embed foreign application windows in WPF; automation or screen-capture code mistakenly using HwndSource for out-of-process windows.","solutions":["Use an HWND created inside your own process (e.g. HwndHost's BuildWindowCore) as the target","For foreign windows use Win32 SetParent-based hosting or UI Automation, not HwndTarget","Get the HWND from the WPF window itself (WindowInteropHelper.Handle) instead of FindWindow"],"exampleFix":"// before\nvar hwnd = FindWindow(null, \"Notepad\");\nvar source = new HwndSource(0, 0, 0, 0, 0, \"x\", hwnd); // throws: other process\n// after\nvar hwnd = new WindowInteropHelper(myWindow).EnsureHandle();\nvar source = new HwndSource(0, 0, 0, 0, 0, \"x\", hwnd);","handlingStrategy":"validation","validationCode":"GetWindowThreadProcessId(hwnd, out var pid);\nif (pid != Environment.ProcessId) throw new InvalidOperationException(\"HWND belongs to another process\");","typeGuard":"bool IsOwnProcessWindow(IntPtr hwnd) { GetWindowThreadProcessId(hwnd, out var pid); return pid == Environment.ProcessId; }","tryCatchPattern":"try { new HwndSource(...); } catch (ArgumentException) { /* use SetParent/WinForms hosting for foreign windows */ }","preventionTips":["Only wrap HWNDs created by your own process","Use HwndHost for embedding native child windows","Avoid FindWindow-based handles as rendering targets"],"tags":["wpf","interop","hwnd","cross-process"],"backgroundTag":"invalid-argument-value","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"}