Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resolving IWeakReference from different context #1301

Merged
merged 2 commits into from
Mar 16, 2023
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
44 changes: 43 additions & 1 deletion src/Tests/UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using Windows.Foundation;
using Windows.UI;
using Windows.Security.Credentials.UI;
using Windows.Storage;
using Windows.Storage.Streams;
using Microsoft.UI.Xaml;
Expand All @@ -30,6 +29,7 @@
using System.Reflection;
using Windows.Devices.Enumeration.Pnp;
using System.Diagnostics;
using Windows.Devices.Enumeration;

#if NET
using WeakRefNS = System;
Expand Down Expand Up @@ -2936,5 +2936,47 @@ private void TestExperimentAttribute()
CustomExperimentClass custom = new CustomExperimentClass();
custom.f();
}

void OnDeviceAdded(DeviceWatcher sender, DeviceInformation args)
{
}

void OnDeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate args)
{
}

[Fact]
public void TestWeakReferenceEventsFromMultipleContexts()
{
SemaphoreSlim semaphore = new SemaphoreSlim(0);
DeviceWatcher watcher = null;

Thread staThread = new Thread(() =>
{
Assert.True(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA);

watcher = DeviceInformation.CreateWatcher();
var exception = Record.Exception(() => {
watcher.Added += OnDeviceAdded;
});
Assert.Null(exception);

Thread mtaThread = new Thread(() =>
{
Assert.True(Thread.CurrentThread.GetApartmentState() == ApartmentState.MTA);

exception = Record.Exception(() => {
watcher.Updated += OnDeviceUpdated;
});
Assert.Null(exception);
});
mtaThread.SetApartmentState(ApartmentState.MTA);
mtaThread.Start();
mtaThread.Join();
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
staThread.Join();
}
}
}
18 changes: 16 additions & 2 deletions src/WinRT.Runtime/ComWrappersSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,28 @@ public static ObjectReference<T> GetObjectReferenceForInterface<T>(IntPtr extern
}

public static ObjectReference<T> GetObjectReferenceForInterface<T>(IntPtr externalComObject, Guid iid)
{
return GetObjectReferenceForInterface<T>(externalComObject, iid, true);
}

internal static ObjectReference<T> GetObjectReferenceForInterface<T>(IntPtr externalComObject, Guid iid, bool requireQI)
{
if (externalComObject == IntPtr.Zero)
{
return null;
}

Marshal.ThrowExceptionForHR(Marshal.QueryInterface(externalComObject, ref iid, out IntPtr ptr));
ObjectReference<T> objRef = ObjectReference<T>.Attach(ref ptr);
ObjectReference<T> objRef;
if (requireQI)
{
Marshal.ThrowExceptionForHR(Marshal.QueryInterface(externalComObject, ref iid, out IntPtr ptr));
objRef = ObjectReference<T>.Attach(ref ptr);
}
else
{
objRef = ObjectReference<T>.FromAbi(externalComObject);
}

if (IsFreeThreaded(objRef))
{
return objRef;
Expand Down
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ private static object CreateObject(IntPtr externalComObject)
{
// IWeakReference is IUnknown-based, so implementations of it may not (and likely won't) implement
// IInspectable. As a result, we need to check for them explicitly.
var iunknownObjRef = ComWrappersSupport.GetObjectReferenceForInterface<IUnknownVftbl>(ptr);
var iunknownObjRef = ComWrappersSupport.GetObjectReferenceForInterface<IUnknownVftbl>(ptr, weakReferenceIID, false);
ComWrappersHelper.Init(iunknownObjRef);

return new SingleInterfaceOptimizedObject(typeof(IWeakReference), iunknownObjRef, false);
Expand Down