internal ComTypeDesc(ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc) { if (typeInfo != null) { ComRuntimeHelpers.GetInfoFromType(typeInfo, out _typeName, out _documentation); } TypeLib = typeLibDesc; }
internal ComTypeClassDesc(ComTypes.ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc) : base(typeInfo, typeLibDesc) { ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo); Guid = typeAttr.guid; for (int i = 0; i < typeAttr.cImplTypes; i++) { typeInfo.GetRefTypeOfImplType(i, out int hRefType); typeInfo.GetRefTypeInfo(hRefType, out ComTypes.ITypeInfo currentTypeInfo); typeInfo.GetImplTypeFlags(i, out ComTypes.IMPLTYPEFLAGS implTypeFlags); bool isSourceItf = (implTypeFlags & ComTypes.IMPLTYPEFLAGS.IMPLTYPEFLAG_FSOURCE) != 0; AddInterface(currentTypeInfo, isSourceItf); } }
private static ComTypes.ITypeInfo GetCoClassTypeInfo(object rcw, ComTypes.ITypeInfo typeInfo) { Debug.Assert(typeInfo != null); if (rcw is IProvideClassInfo provideClassInfo) { IntPtr typeInfoPtr = IntPtr.Zero; try { provideClassInfo.GetClassInfo(out typeInfoPtr); if (typeInfoPtr != IntPtr.Zero) { return(Marshal.GetObjectForIUnknown(typeInfoPtr) as ComTypes.ITypeInfo); } } finally { if (typeInfoPtr != IntPtr.Zero) { Marshal.Release(typeInfoPtr); } } } // retrieving class information through IPCI has failed - // we can try scanning the typelib to find the coclass typeInfo.GetContainingTypeLib(out ComTypes.ITypeLib typeLib, out int _); string typeName = ComRuntimeHelpers.GetNameOfType(typeInfo); ComTypeLibDesc typeLibDesc = ComTypeLibDesc.GetFromTypeLib(typeLib); ComTypeClassDesc coclassDesc = typeLibDesc.GetCoClassForInterface(typeName); if (coclassDesc == null) { return(null); } Guid coclassGuid = coclassDesc.Guid; typeLib.GetTypeInfoOfGuid(ref coclassGuid, out ComTypes.ITypeInfo typeInfoCoClass); return(typeInfoCoClass); }
internal ComTypeEnumDesc(ComTypes.ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc) : base(typeInfo, typeLibDesc) { ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo); string[] memberNames = new string[typeAttr.cVars]; object[] memberValues = new object[typeAttr.cVars]; IntPtr p; // For each enum member get name and value. for (int i = 0; i < typeAttr.cVars; i++) { typeInfo.GetVarDesc(i, out p); // Get the enum member value (as object). ComTypes.VARDESC varDesc; try { varDesc = (ComTypes.VARDESC)Marshal.PtrToStructure(p, typeof(ComTypes.VARDESC)); if (varDesc.varkind == ComTypes.VARKIND.VAR_CONST) { memberValues[i] = Marshal.GetObjectForNativeVariant(varDesc.desc.lpvarValue); } } finally { typeInfo.ReleaseVarDesc(p); } // Get the enum member name memberNames[i] = ComRuntimeHelpers.GetNameOfMethod(typeInfo, varDesc.memid); } _memberNames = memberNames; _memberValues = memberValues; }
internal static ComTypeLibDesc GetFromTypeLib(ComTypes.ITypeLib typeLib) { // check whether we have already loaded this type library ComTypes.TYPELIBATTR typeLibAttr = ComRuntimeHelpers.GetTypeAttrForTypeLib(typeLib); ComTypeLibDesc typeLibDesc; lock (s_cachedTypeLibDesc) { if (s_cachedTypeLibDesc.TryGetValue(typeLibAttr.guid, out typeLibDesc)) { return(typeLibDesc); } } typeLibDesc = new ComTypeLibDesc { Name = ComRuntimeHelpers.GetNameOfLib(typeLib), _typeLibAttributes = typeLibAttr }; int countTypes = typeLib.GetTypeInfoCount(); for (int i = 0; i < countTypes; i++) { typeLib.GetTypeInfoType(i, out ComTypes.TYPEKIND typeKind); typeLib.GetTypeInfo(i, out ComTypes.ITypeInfo typeInfo); if (typeKind == ComTypes.TYPEKIND.TKIND_COCLASS) { ComTypeClassDesc classDesc = new ComTypeClassDesc(typeInfo, typeLibDesc); typeLibDesc._classes.AddLast(classDesc); } else if (typeKind == ComTypes.TYPEKIND.TKIND_ENUM) { ComTypeEnumDesc enumDesc = new ComTypeEnumDesc(typeInfo, typeLibDesc); typeLibDesc._enums.Add(enumDesc.TypeName, enumDesc); } else if (typeKind == ComTypes.TYPEKIND.TKIND_ALIAS) { ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo); if (typeAttr.tdescAlias.vt == (short)VarEnum.VT_USERDEFINED) { ComRuntimeHelpers.GetInfoFromType(typeInfo, out string aliasName, out _); typeInfo.GetRefTypeInfo(typeAttr.tdescAlias.lpValue.ToInt32(), out ComTypes.ITypeInfo referencedTypeInfo); ComTypes.TYPEATTR referencedTypeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(referencedTypeInfo); ComTypes.TYPEKIND referencedTypeKind = referencedTypeAttr.typekind; if (referencedTypeKind == ComTypes.TYPEKIND.TKIND_ENUM) { ComTypeEnumDesc enumDesc = new ComTypeEnumDesc(referencedTypeInfo, typeLibDesc); typeLibDesc._enums.Add(aliasName, enumDesc); } } } } // cached the typelib using the guid as the dictionary key lock (s_cachedTypeLibDesc) { s_cachedTypeLibDesc.Add(typeLibAttr.guid, typeLibDesc); } return(typeLibDesc); }
internal TypeLibMetaObject(Expression expression, ComTypeLibDesc lib) : base(expression, BindingRestrictions.Empty, lib) { _lib = lib; }