Skip to content

Commit

Permalink
Check if global instance is registered
Browse files Browse the repository at this point in the history
  • Loading branch information
elinor-fung committed Mar 12, 2020
1 parent dad304e commit 234c733
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ public void RegisterAsGlobalInstance()
}
}

/// <summary>
/// Get whether or not a global <see cref="ComWrappers" /> instance has been registered.
/// </summary>
internal static bool IsGlobalInstanceRegistered()
{
return s_globalInstance != null;
}

/// <summary>
/// Get the runtime provided IUnknown implementation.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,15 @@ public static string GetTypeInfoName(ITypeInfo typeInfo)
throw new ArgumentNullException(nameof(o));
}

// Passing null as the ComWrapper implementation will use the globally registered wrappper (if available)
IntPtr ptrMaybe;
if (ComWrappers.TryGetOrCreateComInterfaceForObjectInternal(impl: null, o, CreateComInterfaceFlags.TrackerSupport, out ptrMaybe))
return ptrMaybe;
if (ComWrappers.IsGlobalInstanceRegistered())
{
// Passing null as the ComWrapper implementation will use the globally registered wrappper (if available)
IntPtr ptrMaybe;
if (ComWrappers.TryGetOrCreateComInterfaceForObjectInternal(impl: null, o, CreateComInterfaceFlags.TrackerSupport, out ptrMaybe))
{
return ptrMaybe;
}
}

return GetIUnknownForObjectNative(o, false);
}
Expand Down Expand Up @@ -420,10 +425,15 @@ public static object GetObjectForIUnknown(IntPtr /* IUnknown* */ pUnk)
throw new ArgumentNullException(nameof(pUnk));
}

// Passing null as the ComWrapper implementation will use the globally registered wrappper (if available)
object? objMaybe;
if (ComWrappers.TryGetOrCreateObjectForComInstanceInternal(impl: null, pUnk, CreateObjectFlags.TrackerObject, wrapperMaybe: null, out objMaybe))
return objMaybe!;
if (ComWrappers.IsGlobalInstanceRegistered())
{
// Passing null as the ComWrapper implementation will use the globally registered wrappper (if available)
object? objMaybe;
if (ComWrappers.TryGetOrCreateObjectForComInstanceInternal(impl: null, pUnk, CreateObjectFlags.TrackerObject, wrapperMaybe: null, out objMaybe))
{
return objMaybe!;
}
}

return GetObjectForIUnknownNative(pUnk);
}
Expand All @@ -438,10 +448,15 @@ public static object GetUniqueObjectForIUnknown(IntPtr unknown)
throw new ArgumentNullException(nameof(unknown));
}

// Passing null as the ComWrapper implementation will use the globally registered wrappper (if available)
object? objMaybe;
if (ComWrappers.TryGetOrCreateObjectForComInstanceInternal(impl: null, unknown, CreateObjectFlags.TrackerObject | CreateObjectFlags.UniqueInstance, wrapperMaybe: null, out objMaybe))
return objMaybe!;
if (ComWrappers.IsGlobalInstanceRegistered())
{
// Passing null as the ComWrapper implementation will use the globally registered wrappper (if available)
object? objMaybe;
if (ComWrappers.TryGetOrCreateObjectForComInstanceInternal(impl: null, unknown, CreateObjectFlags.TrackerObject | CreateObjectFlags.UniqueInstance, wrapperMaybe: null, out objMaybe))
{
return objMaybe!;
}
}

return GetUniqueObjectForIUnknownNative(unknown);
}
Expand Down

0 comments on commit 234c733

Please sign in to comment.