diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs index 731437a123009..6ac8a57a2001b 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs @@ -289,6 +289,14 @@ public void RegisterAsGlobalInstance() } } + /// + /// Get whether or not a global instance has been registered. + /// + internal static bool IsGlobalInstanceRegistered() + { + return s_globalInstance != null; + } + /// /// Get the runtime provided IUnknown implementation. /// diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs index 11b0fdf1241cb..b702d8bff8ed1 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs @@ -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); } @@ -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); } @@ -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); }