microsoft / Windows.UI.Composition-Win32-Samples

Windows.UI.Composition Win32 Samples
MIT License
459 stars 186 forks source link

Some samples use `WindowsRuntimeMarshal` class, that is no longer available in .NET 5+ #110

Closed lostmsu closed 2 years ago

lostmsu commented 2 years ago

Is there a replacement for .NET 5+?

Example:

public static GraphicsCaptureItem CreateItemForMonitor(IntPtr hmon)
{
    var factory = WindowsRuntimeMarshal.GetActivationFactory(typeof(GraphicsCaptureItem));
    var interop = (IGraphicsCaptureItemInterop)factory; 
    var itemPointer = interop.CreateForMonitor(hmon, GraphicsCaptureItemGuid);
    var item = Marshal.GetObjectForIUnknown(itemPointer) as GraphicsCaptureItem;
    Marshal.Release(itemPointer);

    return item;
}
robmikh commented 2 years ago

Take a look at this file:

https://github.com/robmikh/WPFCaptureSample/blob/master/Robmikh.WindowsRuntimeHelpers/CaptureHelper.cs

That repo has a version of the WPF capture sample that's been updated for .NET 5/6.

lostmsu commented 2 years ago

@robmikh that did not immediately work due to my need to understand CSWinRT, so I ended up not using your code. But simply following this table I got a working sample, that does not need to use CSWinRT magic explicitly, and does not require unsafe:

public static GraphicsCaptureItem CreateItemForMonitor(IntPtr hmon)
{
    var factory = GraphicsCaptureItem.As<IGraphicsCaptureItemInterop>();
    var itemPointer = factory.CreateForMonitor(hmon, GraphicsCaptureItemGuid);
    var item = GraphicsCaptureItem.FromAbi(itemPointer);
    Marshal.Release(itemPointer);

    return item;
}
lostmsu commented 2 years ago

@robmikh no plans to update the samples in this repo?

robmikh commented 2 years ago

It's on the list, currently getting the C++ samples updated.