public static ITypeLib GetContainingTypeLib(this ITypeInfo typeInfo, out int index) { ITypeLib typeLib; typeInfo.GetContainingTypeLib(out typeLib, out index); return(typeLib); }
public static string GetManagedName(this ITypeInfo typeInfo) { var typeInfo2 = typeInfo as ITypeInfo2; if (typeInfo2 != null) { // ReSharper disable EmptyGeneralCatchClause try { var guid = managedNameGuid; object data; typeInfo2.GetCustData(ref guid, out data); var name = data as string; if (name != null) { return(name.Trim()); } } catch (Exception) { } // ReSharper restore EmptyGeneralCatchClause } return(typeInfo.GetContainingTypeLib().GetManagedName() + "." + Marshal.GetTypeInfoName(typeInfo)); }
public static Type GetInteropType(this System.Object o) { if (o == null) { throw new ArgumentNullException(); } if (Marshal.IsComObject(o)) { IDispatch dispatch = o as IDispatch; if (dispatch != null) { ITypeLib typeLib = null; ITypeInfo typeInfo = dispatch.GetTypeInfo(0, 0); int index = 0; typeInfo.GetContainingTypeLib(out typeLib, out index); string typeLibName = Marshal.GetTypeLibName(typeLib); string typeInfoName = Marshal.GetTypeInfoName(typeInfo); string typeFullName = String.Format("{0}.{1}", typeLibName, typeInfoName); System.Reflection.Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); System.Reflection.Assembly assembly = assemblies.FirstOrDefault(x => x.GetType(typeFullName) != null); if (assembly != null) { return(assembly.GetType(typeFullName)); } } } return(o.GetType()); }
private string GetComObjectFullyQualifiedName(object o) { if (o == null) { throw new ArgumentNullException(); } if (Marshal.IsComObject(o)) { IDispatch dispatch = o as IDispatch; if (dispatch != null) { ITypeLib typeLib = null; ITypeInfo typeInfo = dispatch.GetTypeInfo(0, 0); int index = 0; typeInfo.GetContainingTypeLib(out typeLib, out index); string typeLibName = Marshal.GetTypeLibName(typeLib); string typeInfoName = Marshal.GetTypeInfoName(typeInfo); return(String.Format("{0}.{1}", typeLibName, typeInfoName)); } } return(o.GetType().FullName); }
public static string GetManagedName(this ITypeInfo typeInfo) { if (typeInfo is ITypeInfo2 typeInfo2) { // ReSharper disable EmptyGeneralCatchClause try { var guid = managedNameGuid; typeInfo2.GetCustData(ref guid, out var data); if (data is string name) { return(name.Trim()); } } catch (Exception) { } // ReSharper restore EmptyGeneralCatchClause } return(typeInfo.GetContainingTypeLib().GetManagedName() + "." + Marshal.GetTypeInfoName(typeInfo)); }
/// <summary> /// Gets the full typename of an object /// In the case of .NET objects it is in the form 'Type.Namespace'.'Type.Name' /// In the case of COM objects it is in the form 'TypeLibName'.'TypeInfoName' /// </summary> /// <param name="obj">The object to get the full typename of</param> /// <returns>The full type name</returns> private string GetObjectFullTypeName(object obj) { if (Marshal.IsComObject(obj)) { IDispatch dispatch = (IDispatch)obj; if (dispatch.GetTypeInfoCount() != 1) { throw new Exception("Failed to get runtime type information"); } ITypeInfo typeInfo = dispatch.GetTypeInfo(0, 0); ITypeLib typeLib; int index; typeInfo.GetContainingTypeLib(out typeLib, out index); string typeLibName = Marshal.GetTypeLibName(typeLib); string typeInfoName = Marshal.GetTypeInfoName(typeInfo); return(typeLibName + "." + typeInfoName); } else { Type typeObject = obj.GetType(); return(typeObject.Namespace + "." + typeObject.Name); } }
internal ComTypeBrowserInfo GetTypeInfo(ITypeInfo typeInfo) { Debug.Assert(typeInfo != null, "typeInfo != null"); ComTypeBrowserInfo ComTypeBrowserInfo = (ComTypeBrowserInfo)m_cachedTypes[typeInfo]; if (ComTypeBrowserInfo == null) { ITypeLib typeLib; int index; typeInfo.GetContainingTypeLib(out typeLib, out index); TypeLibraryBrowserInfo typeLibrary = GetTypeLibraryInfo(typeLib); Debug.Assert(typeLibrary != null, "typeLibrary != null"); TypeLibraryNamespaceBrowserInfo ns = typeLibrary.GetNamespace(); Debug.Assert(ns != null, "ns != null"); // Retrieving the namespaces may add types to CachedTypes, so try to find this type again. ComTypeBrowserInfo = (ComTypeBrowserInfo)m_cachedTypes[typeInfo]; if (ComTypeBrowserInfo == null) { ComTypeBrowserInfo = new ComTypeBrowserInfo(typeInfo, ns); m_cachedTypes.Add(typeInfo, ComTypeBrowserInfo); } } return(ComTypeBrowserInfo); }
private static Type GetTypeForTypeInfo(ITypeInfo typeInfo) { // ReSharper disable EmptyGeneralCatchClause try { ITypeLib typeLib; int index; typeInfo.GetContainingTypeLib(out typeLib, out index); var assembly = LoadPrimaryInteropAssembly(typeLib); if (assembly != null) { var name = GetManagedTypeInfoName(typeInfo, typeLib); var guid = GetTypeInfoGuid(typeInfo); var type = assembly.GetType(name, false, true); if ((type != null) && (type.GUID == guid)) { return(type); } var types = assembly.GetTypes(); if ((index >= 0) && (index < types.Length)) { type = types[index]; if ((type.GUID == guid) && (type.FullName == name)) { return(type); } } // ReSharper disable once PossibleNullReferenceException type = types.FirstOrDefault(testType => (testType.GUID == guid) && (testType.FullName.Equals(name, StringComparison.OrdinalIgnoreCase))); if (type != null) { return(type); } } var pTypeInfo = Marshal.GetComInterfaceForObject(typeInfo, typeof(ITypeInfo)); try { return(Marshal.GetTypeForITypeInfo(pTypeInfo)); } finally { Marshal.Release(pTypeInfo); } } catch (Exception) { } return(null); // ReSharper restore EmptyGeneralCatchClause }
private static Type GetTypeForTypeInfo(ITypeInfo typeInfo) { // ReSharper disable EmptyGeneralCatchClause try { ITypeLib typeLib; int index; typeInfo.GetContainingTypeLib(out typeLib, out index); var assembly = LoadPrimaryInteropAssembly(typeLib); if (assembly != null) { var name = GetManagedTypeInfoName(typeInfo, typeLib); var guid = GetTypeInfoGuid(typeInfo); var type = assembly.GetType(name, false /*throwOnError*/); if ((type != null) && (type.GUID == guid)) { return(type); } var types = assembly.GetTypes(); if ((index >= 0) && (index < types.Length)) { type = types[index]; if ((type.GUID == guid) && (type.FullName == name)) { return(type); } } type = types.FirstOrDefault(testType => (testType.GUID == guid) && (testType.FullName == name)); if (type != null) { return(type); } } var pTypeInfo = RawCOMHelpers.QueryInterface <ITypeInfo>(Marshal.GetIUnknownForObject(typeInfo)); try { return(Marshal.GetTypeForITypeInfo(pTypeInfo)); } finally { Marshal.Release(pTypeInfo); } } catch (Exception) { } return(null); // ReSharper restore EmptyGeneralCatchClause }
public static ComTypeLibrary FromIDispatch(IDispatch dispatch) { ITypeLib typeLib = null; ITypeInfo typeInfo = dispatch.GetTypeInfo(); int index = 0; typeInfo.GetContainingTypeLib(out typeLib, out index); return(new ComTypeLibrary(typeLib)); }
protected ComBase(ITypeInfo info) { ITypeLib typeLib; int index; info.GetContainingTypeLib(out typeLib, out index); Index = index; Debug.Assert(typeLib != null); Documentation = new ComDocumentation(typeLib, index); }
public static Type GetDispatchTypeInfo(object comObj) { Type ret = null; try { if (!comObj.GetType().IsCOMObject) { ret = comObj.GetType(); } else { IntPtr typeInfo = IntPtr.Zero; try { IDispatch disp = (IDispatch)comObj; disp.GetTypeInfo(0, 0x409, out typeInfo); ITypeInfo ti = (ITypeInfo)Marshal.GetObjectForIUnknown(typeInfo); ITypeLib tl = null; int iIndex = 0; ti.GetContainingTypeLib(out tl, out iIndex); Guid typelibGuid = Marshal.GetTypeLibGuid(tl); Assembly asm = ConvertTypeLibToAssembly(tl); if (asm != null) { ret = asm.GetType(Marshal.GetTypeLibName(tl) + "." + Marshal.GetTypeInfoName(ti)); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } finally { if (typeInfo != IntPtr.Zero) { Marshal.Release(typeInfo); } } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.ToString()); } return(ret); }
private static Type GetTypeForTypeInfo(ITypeInfo typeInfo) { // ReSharper disable EmptyGeneralCatchClause try { int index; var typeLib = typeInfo.GetContainingTypeLib(out index); var assembly = LoadPrimaryInteropAssembly(typeLib); if (assembly != null) { var name = typeInfo.GetManagedName(); var guid = typeInfo.GetGuid(); var type = assembly.GetType(name, false, true); if ((type != null) && (type.GUID == guid)) { return(type); } var types = assembly.GetAllTypes().ToArray(); if ((index >= 0) && (index < types.Length)) { type = types[index]; if ((type.GUID == guid) && (type.FullName == name)) { return(type); } } // ReSharper disable once PossibleNullReferenceException type = types.FirstOrDefault(testType => (testType.GUID == guid) && (testType.FullName.Equals(name, StringComparison.OrdinalIgnoreCase))); if (type != null) { return(type); } } return(typeInfo.GetManagedType()); } catch (Exception) { } return(null); // ReSharper restore EmptyGeneralCatchClause }
/// <summary> /// Analyze the given type looking for dependencies on other type libraries /// </summary> /// <param name="typeInfo"></param> private void AnalyzeTypeInfo(ITypeInfo typeInfo) { ITypeLib containingTypeLib = null; int indexInContainingTypeLib; try { typeInfo.GetContainingTypeLib(out containingTypeLib, out indexInContainingTypeLib); TYPELIBATTR containingTypeLibAttributes; ComReference.GetTypeLibAttrForTypeLib(ref containingTypeLib, out containingTypeLibAttributes); // Have we analyzed this type info already? If so skip it. AnalyzedTypesInfoKey typeInfoId = new AnalyzedTypesInfoKey( containingTypeLibAttributes.guid, containingTypeLibAttributes.wMajorVerNum, containingTypeLibAttributes.wMinorVerNum, containingTypeLibAttributes.lcid, indexInContainingTypeLib); // Get enough information about the type to figure out if we want to register it as a dependency TYPEATTR typeAttributes; ComReference.GetTypeAttrForTypeInfo(typeInfo, out typeAttributes); // Is it one of the types we don't care about? if (!CanSkipType(typeInfo, containingTypeLib, typeAttributes, containingTypeLibAttributes)) { _dependencies.Add(containingTypeLibAttributes); if (_analyzedTypes.Add(typeInfoId)) { // We haven't already analyzed this type, so rescan ScanImplementedTypes(typeInfo, typeAttributes); ScanDefinedVariables(typeInfo, typeAttributes); ScanDefinedFunctions(typeInfo, typeAttributes); } } // Make sure if we encounter this type again, we won't rescan it, since we already know we can skip it else { _analyzedTypes.Add(typeInfoId); } } finally { if (containingTypeLib != null) { _marshalReleaseComObject(containingTypeLib); } } }
public static Guid GetOrCreateGuid(this ITypeInfo typeInfo) { var guid = typeInfo.GetGuid(); if (guid != Guid.Empty) { return(guid); } var guidBytes = typeInfo.GetContainingTypeLib().GetGuid().ToByteArray(); var nameBytes = BitConverter.GetBytes(typeInfo.GetName().GetDigestAsUInt64()); for (var index = 0; index < guidBytes.Length; index++) { guidBytes[index] ^= nameBytes[index % nameBytes.Length]; } return(new Guid(guidBytes)); }
public ComTypeInfo FromITypeInfo(ITypeInfo typeInfo) { if (typeInfo == null) return null; ITypeLib typeLib = null; int index = 0; typeInfo.GetContainingTypeLib(out typeLib, out index); ComTypeLibrary comTypeLibrary = GetComTypeLibrary(typeLib); string typeName = Marshal.GetTypeInfoName(typeInfo); if (comTypeLibrary != null) { return comTypeLibrary.ComTypeInfos.Where( x => x.Name.Equals(typeName)).FirstOrDefault(); } return null; }
public static string GetContainingTypeLibPath(ITypeInfo typeInfo) { if (typeInfo == null) { throw new Exceptions.NullParameterException(typeof(ComInterop), "GetContainingTypeLibPath", "typeInfo"); } ITypeLib typeLib; int typeIndex; typeInfo.GetContainingTypeLib(out typeLib, out typeIndex); try { return(GetTypeLibPath(typeLib)); } finally { Marshal.ReleaseComObject(typeLib); } }
private void AnalyzeTypeInfo(ITypeInfo typeInfo) { ITypeLib ppTLB = null; try { int num; System.Runtime.InteropServices.ComTypes.TYPELIBATTR typelibattr; System.Runtime.InteropServices.ComTypes.TYPEATTR typeattr; typeInfo.GetContainingTypeLib(out ppTLB, out num); ComReference.GetTypeLibAttrForTypeLib(ref ppTLB, out typelibattr); string key = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}.{3}:{4}", new object[] { typelibattr.guid, typelibattr.wMajorVerNum, typelibattr.wMinorVerNum, typelibattr.lcid, num }); ComReference.GetTypeAttrForTypeInfo(typeInfo, out typeattr); if (!this.CanSkipType(typeInfo, ppTLB, typeattr, typelibattr)) { this.dependencies[typelibattr] = null; if (!this.analyzedTypes.ContainsKey(key)) { this.analyzedTypes.Add(key, null); this.ScanImplementedTypes(typeInfo, typeattr); this.ScanDefinedVariables(typeInfo, typeattr); this.ScanDefinedFunctions(typeInfo, typeattr); } } else if (!this.analyzedTypes.ContainsKey(key)) { this.analyzedTypes.Add(key, null); } } finally { if (ppTLB != null) { this.marshalReleaseComObject(ppTLB); } } }
public ComTypeInfo FromITypeInfo(ITypeInfo typeInfo) { if (typeInfo == null) { return(null); } ITypeLib typeLib = null; int index = 0; typeInfo.GetContainingTypeLib(out typeLib, out index); ComTypeLibrary comTypeLibrary = GetComTypeLibrary(typeLib); string typeName = Marshal.GetTypeInfoName(typeInfo); if (comTypeLibrary != null) { return(comTypeLibrary.ComTypeInfos.Where( x => x.Name.Equals(typeName)).FirstOrDefault()); } return(null); }
private void AnalyzeTypeInfo(ITypeInfo typeInfo) { ITypeLib ppTLB = null; try { int num; System.Runtime.InteropServices.ComTypes.TYPELIBATTR typelibattr; System.Runtime.InteropServices.ComTypes.TYPEATTR typeattr; typeInfo.GetContainingTypeLib(out ppTLB, out num); ComReference.GetTypeLibAttrForTypeLib(ref ppTLB, out typelibattr); string key = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}.{3}:{4}", new object[] { typelibattr.guid, typelibattr.wMajorVerNum, typelibattr.wMinorVerNum, typelibattr.lcid, num }); ComReference.GetTypeAttrForTypeInfo(typeInfo, out typeattr); if (!this.CanSkipType(typeInfo, ppTLB, typeattr, typelibattr)) { this.dependencies[typelibattr] = null; if (!this.analyzedTypes.ContainsKey(key)) { this.analyzedTypes.Add(key, null); this.ScanImplementedTypes(typeInfo, typeattr); this.ScanDefinedVariables(typeInfo, typeattr); this.ScanDefinedFunctions(typeInfo, typeattr); } } else if (!this.analyzedTypes.ContainsKey(key)) { this.analyzedTypes.Add(key, null); } } finally { if (ppTLB != null) { this.marshalReleaseComObject(ppTLB); } } }
public static ITypeLib GetContainingTypeLib(this ITypeInfo typeInfo) { return(typeInfo.GetContainingTypeLib(out _)); }
private static IEnumerable <ITypeInfo> GetReferencedEnums(ITypeLib typeLib, ITypeInfo typeInfo, Dictionary <Guid, ITypeInfo> processedTypeInfo) { if (typeInfo == null) { yield break; } var guid = typeInfo.GetOrCreateGuid(); if (processedTypeInfo.ContainsKey(guid)) { yield break; } processedTypeInfo.Add(guid, typeInfo); if (typeInfo.IsEnum()) { yield return(typeInfo); yield break; } if (typeInfo.GetContainingTypeLib().GetGuid() != typeLib.GetGuid()) { yield break; } using (var typeAttrScope = typeInfo.CreateAttrScope()) { for (var funcIndex = 0; funcIndex < typeAttrScope.Value.cFuncs; funcIndex++) { using (var funcDescScope = typeInfo.CreateFuncDescScope(funcIndex)) { foreach (var enumTypeInfo in GetReferencedEnums(typeLib, typeInfo, funcDescScope.Value, processedTypeInfo)) { yield return(enumTypeInfo); } } } for (var varIndex = 0; varIndex < typeAttrScope.Value.cVars; varIndex++) { using (var varDescScope = typeInfo.CreateVarDescScope(varIndex)) { foreach (var enumTypeInfo in GetReferencedEnums(typeLib, typeInfo, varDescScope.Value, processedTypeInfo)) { yield return(enumTypeInfo); } } } for (var implTypeIndex = 0; implTypeIndex < typeAttrScope.Value.cImplTypes; implTypeIndex++) { typeInfo.GetRefTypeOfImplType(implTypeIndex, out var href); typeInfo.GetRefTypeInfo(href, out var refTypeInfo); var refGuid = refTypeInfo.GetGuid(); if ((refGuid == typeof(IDispatch).GUID) || (refGuid == typeof(IDispatchEx).GUID)) { continue; } foreach (var enumTypeInfo in GetReferencedEnums(typeLib, refTypeInfo, processedTypeInfo)) { yield return(enumTypeInfo); } } } }
public static ITypeLib GetContainingTypeLib(this ITypeInfo typeInfo) { int index; return(typeInfo.GetContainingTypeLib(out index)); }
public static IPropertyBag GetTypeLibEnums(this ITypeInfo typeInfo) { var typeLib = typeInfo.GetContainingTypeLib(); var typeLibName = typeLib.GetName(); var rootNode = new PropertyBag(true); var typeInfoCount = typeLib.GetTypeInfoCount(); for (var typeInfoIndex = 0; typeInfoIndex < typeInfoCount; typeInfoIndex++) { typeLib.GetTypeInfo(typeInfoIndex, out typeInfo); var typeInfoName = typeInfo.GetName(); var typeAttr = typeInfo.GetTypeAttr(); if (typeAttr.typekind == TYPEKIND.TKIND_ALIAS) { ITypeInfo refTypeInfo; typeInfo.GetRefTypeInfo(unchecked ((int)(long)typeAttr.tdescAlias.lpValue), out refTypeInfo); typeInfo = refTypeInfo; typeAttr = typeInfo.GetTypeAttr(); } if (typeAttr.typekind == TYPEKIND.TKIND_ENUM) { var varCount = typeAttr.cVars; for (var varIndex = 0; varIndex < varCount; varIndex++) { IntPtr pVarDesc; typeInfo.GetVarDesc(varIndex, out pVarDesc); try { var varDesc = (VARDESC)Marshal.PtrToStructure(pVarDesc, typeof(VARDESC)); if (varDesc.varkind == VARKIND.VAR_CONST) { var varName = typeInfo.GetMemberName(varDesc.memid); object typeLibNodeObj; if (!rootNode.TryGetValue(typeLibName, out typeLibNodeObj) || !(typeLibNodeObj is PropertyBag)) { typeLibNodeObj = new PropertyBag(true); rootNode.SetPropertyNoCheck(typeLibName, typeLibNodeObj); } object typeInfoNodeObj; var typeLibNode = (PropertyBag)typeLibNodeObj; if (!typeLibNode.TryGetValue(typeInfoName, out typeInfoNodeObj) || !(typeInfoNodeObj is PropertyBag)) { typeInfoNodeObj = new PropertyBag(true); typeLibNode.SetPropertyNoCheck(typeInfoName, typeInfoNodeObj); } var typeInfoNode = (PropertyBag)typeInfoNodeObj; typeInfoNode.SetPropertyNoCheck(varName, Marshal.GetObjectForNativeVariant(varDesc.desc.lpvarValue)); } } finally { typeInfo.ReleaseVarDesc(pVarDesc); } } } } return(rootNode); }