Skip to content

Commit

Permalink
Fix nits.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaanstra committed Jan 24, 2024
1 parent 9be15b9 commit 760d15f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
60 changes: 36 additions & 24 deletions src/WinRT.Runtime/ActivationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace WinRT
{
internal unsafe sealed class DllModule
{
readonly string _fileName;
readonly IntPtr _moduleHandle;
readonly delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int> _GetActivationFactory;
readonly delegate* unmanaged[Stdcall]<int> _CanUnloadNow; // TODO: Eventually periodically call
private readonly string _fileName;
private readonly IntPtr _moduleHandle;
private readonly delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int> _GetActivationFactory;
private readonly delegate* unmanaged[Stdcall]<int> _CanUnloadNow; // TODO: Eventually periodically call

static readonly string _currentModuleDirectory = AppContext.BaseDirectory;
private static readonly string _currentModuleDirectory = AppContext.BaseDirectory;

static Dictionary<string, DllModule> _cache = new System.Collections.Generic.Dictionary<string, DllModule>(StringComparer.Ordinal);
private static Dictionary<string, DllModule> _cache = new Dictionary<string, DllModule>(StringComparer.Ordinal);

public static bool TryLoad(string fileName, out DllModule module)
{
Expand All @@ -38,7 +38,7 @@ public static bool TryLoad(string fileName, out DllModule module)
}
}

private static unsafe bool TryCreate(string fileName, out DllModule module)
private static bool TryCreate(string fileName, out DllModule module)
{
// Explicitly look for module in the same directory as this one, and
// use altered search path to ensure any dependencies in the same directory are found.
Expand Down Expand Up @@ -137,18 +137,18 @@ public unsafe (ObjectReference<IActivationFactoryVftbl> obj, int hr) GetActivati
}
}

internal sealed class WinrtModule
internal sealed class WinRTModule
{
readonly IntPtr _mtaCookie;
volatile static WinrtModule _instance;
private static WinrtModule MakeWinRTModule()
private static volatile WinRTModule _instance;
private readonly IntPtr _mtaCookie;
private static WinRTModule MakeWinRTModule()
{
global::System.Threading.Interlocked.CompareExchange(ref _instance, new WinrtModule(), null);
global::System.Threading.Interlocked.CompareExchange(ref _instance, new WinRTModule(), null);
return _instance;
}
public static WinrtModule Instance => _instance ?? MakeWinRTModule();
public static WinRTModule Instance => _instance ?? MakeWinRTModule();

public unsafe WinrtModule()
public unsafe WinRTModule()
{
IntPtr mtaCookie;
Marshal.ThrowExceptionForHR(Platform.CoIncrementMTAUsage(&mtaCookie));
Expand Down Expand Up @@ -182,25 +182,37 @@ public static unsafe (ObjectReference<I> obj, int hr) GetActivationFactory<I>(st
}
}

~WinrtModule()
~WinRTModule()
{
Marshal.ThrowExceptionForHR(Platform.CoDecrementMTAUsage(_mtaCookie));
}
}

#if EMBED
internal
#else
public
#endif
static class ActivationFactory
internal static class IActivationFactoryMethods
{
public static unsafe ObjectReference<I> ActivateInstance<I>(IObjectReference obj)
{
IntPtr instancePtr;
global::WinRT.ExceptionHelpers.ThrowExceptionForHR((*(delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>**)obj.ThisPtr)[6](obj.ThisPtr, &instancePtr));
try
{
return ComWrappersSupport.GetObjectReferenceForInterface<I>(instancePtr);
}
finally
{
MarshalInspectable<object>.DisposeAbi(instancePtr);
}
}
}

internal static class ActivationFactory
{
public static IObjectReference Get(string typeName)
public static ObjectReference<IActivationFactoryVftbl> Get(string typeName)
{
// Prefer the RoGetActivationFactory HRESULT failure over the LoadLibrary/etc. failure
int hr;
ObjectReference<IActivationFactoryVftbl> factory;
(factory, hr) = WinrtModule.GetActivationFactory<IActivationFactoryVftbl>(typeName, InterfaceIIDs.IActivationFactory_IID);
(factory, hr) = WinRTModule.GetActivationFactory<IActivationFactoryVftbl>(typeName, InterfaceIIDs.IActivationFactory_IID);
if (factory != null)
{
return factory;
Expand Down Expand Up @@ -237,7 +249,7 @@ public static ObjectReference<I> Get<
// Prefer the RoGetActivationFactory HRESULT failure over the LoadLibrary/etc. failure
int hr;
ObjectReference<I> factory;
(factory, hr) = WinrtModule.GetActivationFactory<I>(typeName, iid);
(factory, hr) = WinRTModule.GetActivationFactory<I>(typeName, iid);
if (factory != null)
{
return factory;
Expand Down
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace WinRT
{
static partial class Context
internal static partial class Context
{
[DllImport("api-ms-win-core-com-l1-1-0.dll")]
private static extern unsafe int CoGetContextToken(IntPtr* contextToken);
Expand Down

0 comments on commit 760d15f

Please sign in to comment.