Exemple #1
0
        public override List <ITlibNode> GenChildren()
        {
            var res     = new List <ITlibNode>();
            var ticount = _tlib.GetTypeInfoCount();

            for (var x = 0; x < ticount; ++x)
            {
                _tlib.GetTypeInfo(x, out var ti);
                CommonBuildTlibNode(this, ti, true, true, res);
            }
            return(res);
        }
Exemple #2
0
        public static ITypeInfo[] GetTypeInfos(ITypeLib typeLib)
        {
            var infoCount = typeLib.GetTypeInfoCount();
            var infos = new ITypeInfo[infoCount];

            for (int i = 0; i < infoCount; i++)
            {
                ITypeInfo info;
                typeLib.GetTypeInfo(i, out info);
                infos[i] = info;
            }

            return infos;
        }
Exemple #3
0
        public static IEnumerable <ITypeInfo> GetReferencedEnums(this ITypeLib typeLib)
        {
            var processedTypeInfo = new Dictionary <Guid, ITypeInfo>();

            var count = typeLib.GetTypeInfoCount();

            for (var index = 0; index < count; index++)
            {
                typeLib.GetTypeInfo(index, out var typeInfo);
                foreach (var enumTypeInfo in GetReferencedEnums(typeLib, typeInfo, processedTypeInfo))
                {
                    yield return(enumTypeInfo);
                }
            }
        }
        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();
        }
Exemple #5
0
        public static Dictionary <string, string> GetHelpStrings(Assembly assembly)
        {
            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            var a = assembly.CustomAttributes.FirstOrDefault(x => x.AttributeType.Equals(typeof(ImportedFromTypeLibAttribute)));
            var b = assembly.CustomAttributes.FirstOrDefault(x => x.AttributeType.Equals(typeof(GuidAttribute)));
            var c = assembly.CustomAttributes.FirstOrDefault(x => x.AttributeType.Equals(typeof(TypeLibVersionAttribute)));

            if (a != null)
            {
                Guid guid      = Guid.Parse(String.Format("{0}", b.ConstructorArguments[0].Value));
                int  wVerMajor = (int)c.ConstructorArguments[0].Value;
                int  wVerMinor = (int)c.ConstructorArguments[1].Value;

                ITypeLib typeLib = null;
                typeLib = LoadRegTypeLib(ref guid, wVerMajor, wVerMinor, 0);

                string strLibName       = null;
                string strLibDocString  = null;
                int    dwLibHelpContext = 0;
                string strLibHelpFile   = null;

                typeLib.GetDocumentation(-1, out strLibName, out strLibDocString, out dwLibHelpContext, out strLibHelpFile);

                int count = typeLib.GetTypeInfoCount();

                // Loop through types.
                for (int i = 0; i < count; i++)
                {
                    ITypeInfo typeInfo = null;
                    typeLib.GetTypeInfo(i, out typeInfo);

                    IntPtr pTypeAttr = IntPtr.Zero;

                    typeInfo.GetTypeAttr(out pTypeAttr);
                    System.Runtime.InteropServices.ComTypes.TYPEATTR typeAttr = (System.Runtime.InteropServices.ComTypes.TYPEATTR)Marshal.PtrToStructure(pTypeAttr, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));

                    // Skip type if it is hidden.
                    if (typeAttr.wTypeFlags.HasFlag(System.Runtime.InteropServices.ComTypes.TYPEFLAGS.TYPEFLAG_FHIDDEN) == true)
                    {
                        continue;
                    }

                    string strTypeName       = null;
                    string strTypeDocString  = null;
                    int    dwTypeHelpContext = 0;
                    string strTypeHelpFile   = null;

                    typeInfo.GetDocumentation(-1, out strTypeName, out strTypeDocString, out dwTypeHelpContext, out strTypeHelpFile);

                    string typeKey = String.Format("{0}.{1}", strLibName, strTypeName);
                    dictionary.Add(typeKey, strTypeDocString);

                    for (int j = 0; j < typeAttr.cFuncs; j++)
                    {
                        IntPtr pFuncDesc = IntPtr.Zero;
                        typeInfo.GetFuncDesc(j, out pFuncDesc);

                        System.Runtime.InteropServices.ComTypes.FUNCDESC funcDesc = (System.Runtime.InteropServices.ComTypes.FUNCDESC)Marshal.PtrToStructure(pFuncDesc, typeof(System.Runtime.InteropServices.ComTypes.FUNCDESC));

                        string strMemberName       = null;
                        string strMemberDocString  = null;
                        int    dwMemberHelpContext = 0;
                        string strMemberHelpFile   = null;

                        typeInfo.GetDocumentation(funcDesc.memid, out strMemberName, out strMemberDocString, out dwMemberHelpContext, out strMemberHelpFile);

                        string memberKey = String.Format("{0}.{1}", typeKey, strMemberName);

                        if (!dictionary.ContainsKey(memberKey))
                        {
                            dictionary.Add(memberKey, strMemberDocString);
                        }

                        typeInfo.ReleaseFuncDesc(pFuncDesc);
                    }

                    typeInfo.ReleaseTypeAttr(pTypeAttr);
                }
            }

            return(dictionary);
        }
Exemple #6
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();
        }
Exemple #7
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Creates the entries for the type library and all classes defined in this type
        /// library. We can get the necessary information for the file element (file name and
        /// size) directly from the file. We get the information for the type library from
        /// the type library itself.
        /// </summary>
        /// <param name="parent">The parent node.</param>
        /// <param name="fileName">Name (and path) of the file.</param>
        /// ------------------------------------------------------------------------------------
        public void ProcessTypeLibrary(XmlElement parent, string fileName)
        {
            _baseDirectory = Path.GetDirectoryName(fileName);
            _fileName      = fileName.ToLower();

            try
            {
                XmlNode  oldChild;
                ITypeLib typeLib = Tasks.RegHelper.LoadTypeLib(_fileName);
                IntPtr   pLibAttr;
                typeLib.GetLibAttr(out pLibAttr);
                var libAttr = (TYPELIBATTR)
                              Marshal.PtrToStructure(pLibAttr, typeof(TYPELIBATTR));
                typeLib.ReleaseTLibAttr(pLibAttr);

                string flags = string.Empty;
                if ((libAttr.wLibFlags & LIBFLAGS.LIBFLAG_FHASDISKIMAGE) == LIBFLAGS.LIBFLAG_FHASDISKIMAGE)
                {
                    flags = "HASDISKIMAGE";
                }

                // <file name="FwKernel.dll" asmv2:size="1507328">
                var file = GetOrCreateFileNode(parent, fileName);

                // <typelib tlbid="{2f0fccc0-c160-11d3-8da2-005004defec4}" version="1.0" helpdir=""
                //		resourceid="0" flags="HASDISKIMAGE" />
                if (_tlbGuids.ContainsKey(libAttr.guid))
                {
                    _log.LogWarning("Type library with GUID {0} is defined in {1} and {2}",
                                    libAttr.guid, _tlbGuids[libAttr.guid], Path.GetFileName(fileName));
                }
                else
                {
                    _tlbGuids.Add(libAttr.guid, Path.GetFileName(fileName));
                    XmlElement elem = _doc.CreateElement("typelib", UrnAsmv1);
                    elem.SetAttribute("tlbid", libAttr.guid.ToString("B"));
                    elem.SetAttribute("version", string.Format("{0}.{1}", libAttr.wMajorVerNum,
                                                               libAttr.wMinorVerNum));
                    elem.SetAttribute("helpdir", string.Empty);
                    elem.SetAttribute("resourceid", "0");
                    elem.SetAttribute("flags", flags);
                    oldChild = file.SelectSingleNode(string.Format("typelib[tlbid='{0}']",
                                                                   libAttr.guid.ToString("B")));
                    if (oldChild != null)
                    {
                        file.ReplaceChild(elem, oldChild);
                    }
                    else
                    {
                        file.AppendChild(elem);
                    }
                }

                Debug.WriteLine(string.Format(@"typelib tlbid=""{0}"" version=""{1}.{2}"" helpdir="""" resourceid=""0"" flags=""{3}""",
                                              libAttr.guid, libAttr.wMajorVerNum, libAttr.wMinorVerNum, flags));

                int count = typeLib.GetTypeInfoCount();
                for (int i = 0; i < count; i++)
                {
                    ITypeInfo typeInfo;
                    typeLib.GetTypeInfo(i, out typeInfo);

                    ProcessTypeInfo(parent, libAttr.guid, typeInfo);
                }

                oldChild = parent.SelectSingleNode(string.Format("file[name='{0}']",
                                                                 Path.GetFileName(fileName)));
                if (oldChild != null)
                {
                    parent.ReplaceChild(file, oldChild);
                }
                else
                {
                    parent.AppendChild(file);
                }
            }
            catch (Exception)
            {
                // just ignore if this isn't a type library
                _log.LogMessage(MessageImportance.Normal, "Can't load type library {0}", fileName);
            }
        }
        public override void asCommandLine(Dictionary <string, string> args)
        {
            _typeLibrary = (string)args["TypeLibrary"];
            //_typeLibrary = @"C:\Peach3\ComTest\Release\ComTest.dll";

            if (!File.Exists(_typeLibrary))
            {
                throw new PeachException("Error, the TypeLibrary was not found.");
            }

            ITypeLib typeLib = null;
            int      ret     = LoadTypeLib(_typeLibrary, out typeLib);

            if (ret != 0)
            {
                throw new PeachException("Error loading TypeLibrary.  LoadTypeLib returned " + ret);
            }

            if (typeLib == null)
            {
                throw new PeachException("Error, LoadTypeLib returned a null ITypeLib interface.");
            }

            string name;
            string doc;
            int    helpid;
            string helpfile;

            string [] arrClassification = new string [] { "Enum", "Struct", "Module", "Interface",
                                                          "Dispinterface", "Coclass", "Typedef", "Union" };


            typeLib.GetDocumentation(-1, out name, out doc, out helpid, out helpfile);
            Console.WriteLine(name);

            ITypeInfo typeInfo = null;

            for (int cnt = 0; cnt < typeLib.GetTypeInfoCount(); cnt++)
            {
                // http://www.codeguru.com/cpp/com-tech/activex/misc/article.php/c2569

                Console.WriteLine(" ------------- ");

                typeInfo = null;
                typeLib.GetTypeInfo(cnt, out typeInfo);
                if (typeInfo == null)
                {
                    Console.WriteLine("typeInfo was null, continue!");
                    continue;
                }

                typeLib.GetDocumentation(cnt, out name, out doc, out helpid, out helpfile);
                Console.WriteLine("  " + name);

                System.Runtime.InteropServices.ComTypes.TYPEKIND typeKind;
                typeLib.GetTypeInfoType(cnt, out typeKind);

                Console.WriteLine("  " + arrClassification[(int)typeKind]);

                IntPtr ppTypeAttributes;
                typeInfo.GetTypeAttr(out ppTypeAttributes);
                var typeAttributes = (System.Runtime.InteropServices.ComTypes.TYPEATTR)Marshal.PtrToStructure(ppTypeAttributes, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));

                for (int cntFuncs = 0; cntFuncs < typeAttributes.cFuncs; cntFuncs++)
                {
                    IntPtr ppFuncDesc;
                    typeInfo.GetFuncDesc(cntFuncs, out ppFuncDesc);
                    var funcDesc = (System.Runtime.InteropServices.ComTypes.FUNCDESC)Marshal.PtrToStructure(ppFuncDesc, typeof(System.Runtime.InteropServices.ComTypes.FUNCDESC));

                    int memberID = funcDesc.memid;
                    //var elemDesc = funcDesc.elemdescFunc;

                    typeInfo.GetDocumentation(memberID, out name, out doc, out helpid, out helpfile);
                    Console.WriteLine("    " + name);

                    //funcDesc.

                    typeInfo.ReleaseFuncDesc(ppFuncDesc);
                }

                for (int cntVars = 0; cntVars < typeAttributes.cVars; cntVars++)
                {
                    IntPtr ppVarDesc;
                    typeInfo.GetVarDesc(cntVars, out ppVarDesc);
                    var varDesc = (System.Runtime.InteropServices.ComTypes.VARDESC)Marshal.PtrToStructure(ppVarDesc, typeof(System.Runtime.InteropServices.ComTypes.VARDESC));

                    int memberID = varDesc.memid;

                    typeInfo.GetDocumentation(memberID, out name, out doc, out helpid, out helpfile);
                    Console.WriteLine("    " + name);

                    typeInfo.ReleaseVarDesc(ppVarDesc);
                }

                typeInfo.ReleaseTypeAttr(ppTypeAttributes);
            }
        }
 internal void AnalyzeTypeLibrary(ITypeLib typeLibrary)
 {
     try
     {
         int typeInfoCount = typeLibrary.GetTypeInfoCount();
         for (int i = 0; i < typeInfoCount; i++)
         {
             ITypeInfo ppTI = null;
             try
             {
                 typeLibrary.GetTypeInfo(i, out ppTI);
                 this.AnalyzeTypeInfo(ppTI);
             }
             finally
             {
                 if (ppTI != null)
                 {
                     this.marshalReleaseComObject(ppTI);
                 }
             }
         }
     }
     catch (COMException exception)
     {
         this.encounteredProblems.Add(exception);
     }
 }
Exemple #10
0
        public static void ParseTypeLib(string filePath)
        {
            string   fileNameOnly = Path.GetFileNameWithoutExtension(filePath);
            ITypeLib typeLib      = LoadTypeLib(filePath);

            int count = typeLib.GetTypeInfoCount();

            Console.WriteLine($"typeLib count is {count}");
            IntPtr ipLibAtt = IntPtr.Zero;

            typeLib.GetLibAttr(out ipLibAtt);

            var typeLibAttr = (System.Runtime.InteropServices.ComTypes.TYPELIBATTR)
                              Marshal.PtrToStructure(ipLibAtt, typeof(System.Runtime.InteropServices.ComTypes.TYPELIBATTR));
            Guid tlbId = typeLibAttr.guid;

            for (int i = 0; i < count; i++)
            {
                ITypeInfo typeInfo = null;
                typeLib.GetTypeInfo(i, out typeInfo);

                //figure out what guids, typekind, and names of the thing we're dealing with
                IntPtr ipTypeAttr = IntPtr.Zero;
                typeInfo.GetTypeAttr(out ipTypeAttr);

                //unmarshal the pointer into a structure into something we can read
                var typeattr = (System.Runtime.InteropServices.ComTypes.TYPEATTR)
                               Marshal.PtrToStructure(ipTypeAttr, typeof(System.Runtime.InteropServices.ComTypes.TYPEATTR));

                System.Runtime.InteropServices.ComTypes.TYPEKIND typeKind = typeattr.typekind;
                Guid typeId = typeattr.guid;

                //get the name of the type
                string strName, strDocString, strHelpFile;
                int    dwHelpContext;
                typeLib.GetDocumentation(i, out strName, out strDocString, out dwHelpContext, out strHelpFile);


                if (typeKind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS)
                {
                    string xmlComClassFormat = "<comClass clsid=\"{0}\" tlbid=\"{1}\" description=\"{2}\" progid=\"{3}.{4}\"></comClass>";
                    string comClassXml       = String.Format(xmlComClassFormat,
                                                             typeId.ToString("B").ToUpper(),
                                                             tlbId.ToString("B").ToUpper(),
                                                             strDocString,
                                                             fileNameOnly, strName
                                                             );
                    Console.WriteLine(comClassXml);
                }
                else if (typeKind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_INTERFACE)
                {
                    string xmlProxyStubFormat = "<comInterfaceExternalProxyStub name=\"{0}\" iid=\"{1}\" tlbid=\"{2}\" proxyStubClsid32=\"{3}\"></comInterfaceExternalProxyStub>";
                    string proxyStubXml       = String.Format(xmlProxyStubFormat,
                                                              strName,
                                                              typeId.ToString("B").ToUpper(),
                                                              tlbId.ToString("B").ToUpper(),
                                                              "{00020424-0000-0000-C000-000000000046}"
                                                              );
                    Console.WriteLine(proxyStubXml);
                }
            }

            return;
        }