Skip to content

Commit 344204e

Browse files
committed
Don't throw if IDispatch.GetTypeInfoCount returns an error HRESULT and 0 as count
1 parent 46b173b commit 344204e

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/ComInterop/ComRuntimeHelpers.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,29 @@ internal static string GetNameOfType(ComTypes.ITypeInfo typeInfo)
103103
}
104104

105105
/// <summary>
106-
/// Look for typeinfo using IDispatch.GetTypeInfo
106+
/// Look for type info using IDispatch.GetTypeInfo
107107
/// </summary>
108108
/// <param name="dispatch">IDispatch object</param>
109109
/// <remarks>
110-
/// Some COM objects just dont expose typeinfo. In these cases, this method will return null.
111-
/// Some COM objects do intend to expose typeinfo, but may not be able to do so if the type-library is not properly
112-
/// registered. This will be considered as acceptable or as an error condition depending on throwIfMissingExpectedTypeInfo
110+
/// Some COM objects just don't expose type info. In these cases, this method will return null.
111+
/// Some COM objects do intend to expose type info, but may not be able to do so if the type library
112+
/// is not properly registered. This will be considered an error.
113113
/// </remarks>
114114
/// <returns>Type info</returns>
115115
internal static ComTypes.ITypeInfo GetITypeInfoFromIDispatch(IDispatch dispatch)
116116
{
117117
int hresult = dispatch.TryGetTypeInfoCount(out uint typeCount);
118-
Marshal.ThrowExceptionForHR(hresult);
119-
Debug.Assert(typeCount <= 1);
120118
if (typeCount == 0)
121119
{
120+
// COM objects should return a type count of 0 to indicate that type info is not exposed.
121+
// Some COM objects may return a non-success HRESULT when type info is not supported, so
122+
// we only check the count and not the HRESULT in this case.
122123
return null;
123124
}
124125

126+
Marshal.ThrowExceptionForHR(hresult);
127+
Debug.Assert(typeCount == 1);
128+
125129
IntPtr typeInfoPtr;
126130
hresult = dispatch.TryGetTypeInfo(0, 0, out typeInfoPtr);
127131
if (!ComHresults.IsSuccess(hresult))

0 commit comments

Comments
 (0)