dotnet/wpf · error · ArgumentException

SR.HwndTarget_InvalidWindowProcess

Error message

SR.HwndTarget_InvalidWindowProcess

What it means

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.

Solutions

  1. Use an HWND created inside your own process (e.g. HwndHost's BuildWindowCore) as the target
  2. For foreign windows use Win32 SetParent-based hosting or UI Automation, not HwndTarget
  3. Get the HWND from the WPF window itself (WindowInteropHelper.Handle) instead of FindWindow

Example fix

// before
var hwnd = FindWindow(null, "Notepad");
var source = new HwndSource(0, 0, 0, 0, 0, "x", hwnd); // throws: other process
// after
var hwnd = new WindowInteropHelper(myWindow).EnsureHandle();
var source = new HwndSource(0, 0, 0, 0, 0, "x", hwnd);
Defensive patterns

Strategy: validation

Validate before calling

GetWindowThreadProcessId(hwnd, out var pid);
if (pid != Environment.ProcessId) throw new InvalidOperationException("HWND belongs to another process");

Type guard

bool IsOwnProcessWindow(IntPtr hwnd) { GetWindowThreadProcessId(hwnd, out var pid); return pid == Environment.ProcessId; }

Try / catch

try { new HwndSource(...); } catch (ArgumentException) { /* use SetParent/WinForms hosting for foreign windows */ }

Prevention

When it happens

Trigger: Creating an HwndSource/HwndTarget over an HWND owned by a different process (e.g. a handle obtained via FindWindow/EnumWindows of an external app).

Common situations: Attempts to embed foreign application windows in WPF; automation or screen-capture code mistakenly using HwndSource for out-of-process windows.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7ea696448cc31311. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndTarget.cs:531

        /// </summary>
        private void AttachToHwnd(IntPtr hwnd)
        {
            int processId = 0;
            int threadId = UnsafeNativeMethods.GetWindowThreadProcessId(
                new HandleRef(this, hwnd),
                out processId
                );

            if (!UnsafeNativeMethods.IsWindow(new HandleRef(this, hwnd)))
            {
                throw new ArgumentException(
                    SR.HwndTarget_InvalidWindowHandle,
                    nameof(hwnd)
                    );
            }
            else if (processId != Environment.ProcessId)
            {
                throw new ArgumentException(
                    SR.HwndTarget_InvalidWindowProcess,
                    nameof(hwnd)
                    );
            }
            else if (threadId != SafeNativeMethods.GetCurrentThreadId())
            {
                throw new ArgumentException(
                    SR.HwndTarget_InvalidWindowThread,
                    nameof(hwnd)
                    );
            }

            int hr = VisualTarget_AttachToHwnd(hwnd);

            if (HRESULT.Failed(hr))
            {
                if (hr == unchecked((int)0x80070005)) // E_ACCESSDENIED
                {

View on GitHub (pinned to 81131a70a4)