Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ internal static IntPtr CreateCCWForObjectUnsafe(object obj)
public static IObjectReference CreateCCWForObject(object obj)
{
IntPtr ccw = ComWrappers.GetOrCreateComInterfaceForObject(obj, CreateComInterfaceFlags.TrackerSupport);
return ObjectReference<IUnknownVftbl>.Attach(ref ccw, IID.IID_IUnknown);
return ObjectReference<IUnknownVftbl>.AttachFreeThreadedUnsafe(ref ccw);
}

internal static IntPtr CreateCCWForObjectForABI(object obj, Guid iid)
Expand Down
21 changes: 21 additions & 0 deletions src/WinRT.Runtime/ObjectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,27 @@ public static ObjectReference<T> Attach(ref IntPtr thisPtr)
}
}

/// <summary>
/// Creates an <see cref="ObjectReference{T}"/> instance for a COM pointer to an agile object, without validation.
/// </summary>
/// <param name="thisPtr">The COM pointer to wrap.</param>
/// <returns>The resulting <see cref="ObjectReference{T}"/> instance.</returns>
/// <remarks>
/// This method will not validate that the target COM object is actually free-threaded.
/// It is the responsibility of callers to ensure only free-threaded objects are used.
/// </remarks>
internal static ObjectReference<T> AttachFreeThreadedUnsafe(ref IntPtr thisPtr)
{
if (thisPtr == IntPtr.Zero)
{
return null;
}

var obj = new ObjectReference<T>(thisPtr);
thisPtr = IntPtr.Zero;
return obj;
}

public static ObjectReference<T> Attach(ref IntPtr thisPtr, Guid iid)
{
if (thisPtr == IntPtr.Zero)
Expand Down