Support CsWinRT RCW marshalling in WinUI project

このコミットが含まれているのは:
Sergio Pedri committed 2023-04-20 20:07:19 +02:00
1 親 0eee72ff29
コミット df8a4c250b
7ファイル変更
+75 -49

検索条件に一致するファイルはありません

+45
ファイルの表示
@@ -0,0 +1,45 @@
#if WINDOWS_UWP
using System.Runtime.InteropServices;
#endif
using TerraFX.Interop.Windows;
#if !WINDOWS_UWP
using WinRT;
#endif
#if WINDOWS_UWP
namespace ComputeSharp.D2D1.Uwp.Helpers;
#else
namespace ComputeSharp.D2D1.WinUI.Helpers;
#endif
/// <summary>
/// A helper type to handle marshalling of RCW instances.
/// </summary>
internal static class RcwMarshaller
{
/// <summary>
/// Retrieves the underlying native object for an input RCW and casts it to the specified type.
/// </summary>
/// <typeparam name="T">The interface type to retrieve an instance of.</typeparam>
/// <param name="managedObject">The input RCW instance to unwrap.</param>
/// <param name="nativeObject">A pointer to the resulting native object to retrieve.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
public static unsafe HRESULT QueryInterface<T>(object managedObject, T** nativeObject)
where T : unmanaged // IUnknown
{
using ComPtr<IUnknown> unknownObject = default;
#if WINDOWS_UWP
// On UWP, due to built-in COM/WinRT support, Marshal.GetIUnknownForObject can handle all the logic
unknownObject.Attach((IUnknown*)Marshal.GetIUnknownForObject(managedObject));
#else
// On WinUI 3, delegate the RCW unwrapping or CCW creation logic to CsWinRT's APIs
ObjectReferenceValue objectReference = MarshalInspectable<object>.CreateMarshaler2(managedObject);
unknownObject.Attach((IUnknown*)objectReference.Detach());
#endif
// QueryInterface to the specific interface we need
return unknownObject.CopyTo(nativeObject);
}
}