示例#1
0
        private void LoadModules(ITypeLib typeLibrary)
        {
            var typeCount = typeLibrary.GetTypeInfoCount();

            for (var index = 0; index < typeCount; index++)
            {
                try
                {
                    typeLibrary.GetTypeInfo(index, out ITypeInfo info);
                    info.GetTypeAttr(out var typeAttributesPointer);
                    using (DisposalActionContainer.Create(typeAttributesPointer, info.ReleaseTypeAttr))
                    {
                        var typeAttributes = Marshal.PtrToStructure <TYPEATTR>(typeAttributesPointer);
                        KnownTypes.TryGetValue(typeAttributes.guid, out var type);

                        switch (typeAttributes.typekind)
                        {
                        case TYPEKIND.TKIND_ENUM:
                            var enumeration = type ?? new ComEnumeration(this, typeLibrary, info, typeAttributes, index);
                            Debug.Assert(enumeration is ComEnumeration);
                            _enumerations.Add(enumeration as ComEnumeration);
                            if (type == null && !enumeration.Guid.Equals(Guid.Empty))
                            {
                                KnownTypes.TryAdd(typeAttributes.guid, enumeration);
                            }
                            break;

                        case TYPEKIND.TKIND_COCLASS:
                            var coclass = type ?? new ComCoClass(this, typeLibrary, info, typeAttributes, index);
                            Debug.Assert(coclass is ComCoClass && !coclass.Guid.Equals(Guid.Empty));
                            _classes.Add(coclass as ComCoClass);
                            if (type == null)
                            {
                                KnownTypes.TryAdd(typeAttributes.guid, coclass);
                            }
                            break;

                        case TYPEKIND.TKIND_DISPATCH:
                        case TYPEKIND.TKIND_INTERFACE:
                            var intface = type ?? new ComInterface(this, typeLibrary, info, typeAttributes, index);
                            Debug.Assert(intface is ComInterface && !intface.Guid.Equals(Guid.Empty));
                            _interfaces.Add(intface as ComInterface);
                            if (type == null)
                            {
                                KnownTypes.TryAdd(typeAttributes.guid, intface);
                            }
                            break;

                        case TYPEKIND.TKIND_RECORD:
                            var structure = new ComStruct(this, typeLibrary, info, typeAttributes, index);
                            _structs.Add(structure);
                            break;

                        case TYPEKIND.TKIND_MODULE:
                            var module = type ?? new ComModule(this, typeLibrary, info, typeAttributes, index);
                            Debug.Assert(module is ComModule);
                            _modules.Add(module as ComModule);
                            if (type == null && !module.Guid.Equals(Guid.Empty))
                            {
                                KnownTypes.TryAdd(typeAttributes.guid, module);
                            }
                            break;

                        case TYPEKIND.TKIND_ALIAS:
                            var alias = new ComAlias(this, typeLibrary, info, index, typeAttributes);
                            _aliases.Add(alias);
                            if (alias.Guid != Guid.Empty)
                            {
                                KnownAliases.TryAdd(alias.Guid, alias);
                            }
                            break;

                        case TYPEKIND.TKIND_UNION:
                            //TKIND_UNION is not a supported member type in VBA.
                            break;

                        default:
                            throw new NotImplementedException($"Didn't expect a TYPEATTR with multiple typekind flags set in {Path}.");
                        }
                    }
                }
                catch (COMException) { }
            }
            ApplySpecificLibraryTweaks();
        }
示例#2
0
        private void LoadModules(ITypeLib typeLibrary)
        {
            var typeCount = typeLibrary.GetTypeInfoCount();

            for (var index = 0; index < typeCount; index++)
            {
                try
                {
                    ITypeInfo info;
                    typeLibrary.GetTypeInfo(index, out info);
                    IntPtr typeAttributesPointer;
                    info.GetTypeAttr(out typeAttributesPointer);
                    var typeAttributes = (TYPEATTR)Marshal.PtrToStructure(typeAttributesPointer, typeof(TYPEATTR));

                    ComType type;
                    KnownTypes.TryGetValue(typeAttributes.guid, out type);

                    switch (typeAttributes.typekind)
                    {
                    case TYPEKIND.TKIND_ENUM:
                        var enumeration = type ?? new ComEnumeration(typeLibrary, info, typeAttributes, index);
                        _enumerations.Add(enumeration as ComEnumeration);
                        if (type != null)
                        {
                            KnownTypes.TryAdd(typeAttributes.guid, enumeration);
                        }
                        break;

                    case TYPEKIND.TKIND_COCLASS:
                        var coclass = type ?? new ComCoClass(typeLibrary, info, typeAttributes, index);
                        _classes.Add(coclass as ComCoClass);
                        if (type != null)
                        {
                            KnownTypes.TryAdd(typeAttributes.guid, coclass);
                        }
                        break;

                    case TYPEKIND.TKIND_DISPATCH:
                    case TYPEKIND.TKIND_INTERFACE:
                        var intface = type ?? new ComInterface(typeLibrary, info, typeAttributes, index);
                        _interfaces.Add(intface as ComInterface);
                        if (type != null)
                        {
                            KnownTypes.TryAdd(typeAttributes.guid, intface);
                        }
                        break;

                    case TYPEKIND.TKIND_RECORD:
                        var structure = new ComStruct(typeLibrary, info, typeAttributes, index);
                        _structs.Add(structure);
                        break;

                    case TYPEKIND.TKIND_MODULE:
                        var module = type ?? new ComModule(typeLibrary, info, typeAttributes, index);
                        _modules.Add(module as ComModule);
                        if (type != null)
                        {
                            KnownTypes.TryAdd(typeAttributes.guid, module);
                        }
                        break;

                    case TYPEKIND.TKIND_ALIAS:
                        var alias = new ComAlias(typeLibrary, info, index, typeAttributes);
                        _aliases.Add(alias);
                        break;

                    case TYPEKIND.TKIND_UNION:
                        //TKIND_UNION is not a supported member type in VBA.
                        break;

                    default:
                        throw new NotImplementedException(string.Format("Didn't expect a TYPEATTR with multiple typekind flags set in {0}.", Path));
                    }
                    info.ReleaseTypeAttr(typeAttributesPointer);
                }
                catch (COMException) { }
            }
            ApplySpecificLibraryTweaks();
        }