Skip to content

Enable GraphicsCaptureItem creation from Microsoft.UI.WindowId/DisplayId#6280

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/enable-graphics-capture-item-creation
Draft

Enable GraphicsCaptureItem creation from Microsoft.UI.WindowId/DisplayId#6280
Copilot wants to merge 2 commits intomainfrom
copilot/enable-graphics-capture-item-creation

Conversation

Copy link
Contributor

Copilot AI commented Mar 9, 2026

Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hwnd) returns Microsoft.UI.WindowId, but Windows.Graphics.Capture.GraphicsCaptureItem.TryCreateFromWindowId expects Windows.UI.WindowId. Despite identical memory layout, the namespace gap forces developers into unsafe manual reinterpret hacks.

Changes

New API: Microsoft.Windows.Graphics.Capture.GraphicsCaptureItem

Static runtimeclass with two factory methods that use IGraphicsCaptureItemInterop COM interface directly, bypassing the Windows.UI.WindowId requirement entirely:

// Before: painful namespace mismatch, no supported path
var msWindowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hwnd);
// ❌ GraphicsCaptureItem.TryCreateFromWindowId(msWindowId); // wrong type!

// After: first-class interop
using Microsoft.Windows.Graphics.Capture;
var msWindowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hwnd);
var item = GraphicsCaptureItem.TryCreateFromWindowId(msWindowId); // ✅
  • TryCreateFromWindowId(Microsoft.UI.WindowId) — extracts HWND from WindowId.Value, calls IGraphicsCaptureItemInterop::CreateForWindow
  • TryCreateFromDisplayId(Microsoft.UI.DisplayId) — calls IGraphicsCaptureItemInterop::CreateForMonitor; covers the display capture interop gap symmetrically
  • Both follow TryCreate* convention: return null on invalid handle or non-capturable target; propagate unexpected COM activation failures as exceptions per WinRT boundary policy

New files

  • dev/Interop/GraphicsCapture/Microsoft.Windows.Graphics.Capture.idl
  • dev/Interop/GraphicsCapture/GraphicsCaptureItem.{h,cpp}
  • dev/Interop/GraphicsCapture/GraphicsCapture.vcxitems
  • dev/Projections/CS/Microsoft.Windows.Graphics.Capture.Projection/
  • test/GraphicsCaptureTests/ — API tests for null-WindowId and null-DisplayId guard conditions

Modified files

  • dev/WindowsAppRuntime_DLL/WindowsAppRuntime_DLL.vcxproj — import new vcxitems
  • dev/WindowsAppRuntime_DLL/pch.h — add winrt/Windows.Graphics.Capture.h
  • build/NuSpecs/WindowsAppSDK-Nuget-Native.WinRt.props — register Microsoft.Windows.Graphics.Capture.winmd
  • WindowsAppRuntime.sln — wire up all new projects

A microsoft employee must use /azp run to validate using the pipelines below.

WARNING:
Comments made by azure-pipelines bot maybe inaccurate.
Please see pipeline link to verify that the build is being ran.

For status checks on the main branch, please use TransportPackage-Foundation-PR
(https://microsoft.visualstudio.com/ProjectReunion/_build?definitionId=81063&_a=summary)
and run the build against your PR branch with the default parameters.

Original prompt

This section details on the original issue you should resolve

<issue_title>Enable creating GraphicsCaptureItem from HWND by resolving the WindowId namespace mismatch</issue_title>
<issue_description>I am developing a C# application using Windows.Graphics.Capture.
I am facing a friction point when trying to use GraphicsCaptureItem.TryCreateFromWindowId(Windows.UI.WindowId windowId).
While Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hwnd) exists to bridge the Win32 and WinUI worlds,
it returns a Microsoft.UI.WindowId struct. However, the Capture API expects a Windows.UI.WindowId struct.
Although these two structs share the same memory layout (a single ulong Value),
they reside in different namespaces and lack a built-in conversion or shared interface.
This forces developers to perform manual bitwise copying or "fake" casts,
which feels like an unsupported hack rather than a first-class developer experience.

To improve the interoperability between Win32 and Windows.Graphics.Capture,
I request one of the following:

1.Direct HWND Support:
Add GraphicsCaptureItem.TryCreateFromHwnd(IntPtr hwnd) to bypass the WindowId confusion entirely.

2.Official Conversion Utility:
Provide a standard method such as WindowId.FromHwnd(IntPtr hwnd) that returns the correct WindowId type for the target API,
or a bridge between Microsoft.UI.WindowId and Windows.UI.WindowId.

3.Documentation Clarity:
If WindowId.Value is guaranteed to be the same as an HWND handle,
please explicitly state this in the official documentation so developers can perform the cast safely.

The current lack of a clear, unified path for HWND-based capture is a significant hurdle for modernizing Windows apps.
I hope to see a more streamlined Interop experience in future updates.

Thank you for your consideration.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Adds a new static runtimeclass GraphicsCaptureItem in the
Microsoft.Windows.Graphics.Capture namespace with:
- TryCreateFromWindowId(Microsoft.UI.WindowId) - creates a
  GraphicsCaptureItem from a WinAppSDK WindowId
- TryCreateFromDisplayId(Microsoft.UI.DisplayId) - creates a
  GraphicsCaptureItem from a WinAppSDK DisplayId

This resolves the namespace mismatch between Microsoft.UI.WindowId
(returned by Win32Interop.GetWindowIdFromWindow) and
Windows.UI.WindowId (expected by GraphicsCaptureItem.TryCreateFromWindowId)
by using IGraphicsCaptureItemInterop COM interface directly.

Co-authored-by: lauren-ciha <64796985+lauren-ciha@users.noreply.github.com>
Copilot AI changed the title [WIP] Enable creating GraphicsCaptureItem from HWND by resolving WindowId mismatch Enable GraphicsCaptureItem creation from Microsoft.UI.WindowId/DisplayId Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable creating GraphicsCaptureItem from HWND by resolving the WindowId namespace mismatch

2 participants