Exemplo n.º 1
0
        public void ParseComInfo(TreeViewEx regTreeView)
        {
            Dictionary <string, ParseStage> listOfParseRegNodes = new Dictionary <string, ParseStage> {
                { @"SOFTWARE\Classes\CLSID", ParseStage.CLSID },
                { @"SOFTWARE\WOW6432Node\Classes\CLSID", ParseStage.CLSID },
                { @"SOFTWARE\Classes\WOW6432Node\CLSID", ParseStage.CLSID },
                { @"SOFTWARE\Classes\Interface", ParseStage.Interface },
                { @"SOFTWARE\WOW6432Node\Classes\Interface", ParseStage.Interface },
                { @"SOFTWARE\Classes\WOW6432Node\Interface", ParseStage.Interface },
                { @"SOFTWARE\Classes\TypeLib", ParseStage.TypeLib },
                { @"SOFTWARE\WOW6432Node\Classes\TypeLib", ParseStage.TypeLib },
                { @"SOFTWARE\Classes\WOW6432Node\TypeLib", ParseStage.TypeLib },
                { @"SOFTWARE\Classes", ParseStage.ProgId },
                { @"SOFTWARE\WOW6432Node\Classes", ParseStage.ProgId },
                { @"SOFTWARE\Classes\WOW6432Node", ParseStage.ProgId },
            };

            foreach (TreeNode item in regTreeView.Nodes)
            {
                foreach (var regNodePath in listOfParseRegNodes)
                {
                    switch (regNodePath.Value)
                    {
                    case ParseStage.CLSID:
                    {
                        var clsidNode = item.GetNode(regNodePath.Key);
                        if (clsidNode == null)
                        {
                            continue;
                        }
                        foreach (TreeNode clsidItem in clsidNode.Nodes)
                        {
                            ComClassRegInfo comClassRegInfo;
                            string          comFile;
                            ParseComClass(clsidItem, out comClassRegInfo, out comFile);
                            if (string.IsNullOrWhiteSpace(comFile))
                            {
                                if (!_allRegFileNameEmptyClasses.ContainsKey(comClassRegInfo.clsid))
                                {
                                    _allRegFileNameEmptyClasses.Add(comClassRegInfo.clsid, comClassRegInfo);
                                }
                            }
                            else
                            {
                                ListOfComDLL.Add(comFile);
                                ComFileInfo fileInfo;
                                if (_allRegComFiles.TryGetValue(comFile, out fileInfo))
                                {
                                    if (!fileInfo.ComClassInfo.ContainsKey(comClassRegInfo.clsid))
                                    {
                                        fileInfo.ComClassInfo.Add(comClassRegInfo.clsid, comClassRegInfo);
                                    }
                                }
                                else
                                {
                                    fileInfo = new ComFileInfo();
                                    fileInfo.ComClassInfo.Add(comClassRegInfo.clsid, comClassRegInfo);
                                    _allRegComFiles.Add(comFile, fileInfo);
                                }

                                if (!_allRegComClasses.ContainsKey(comClassRegInfo.clsid))
                                {
                                    _allRegComClasses.Add(comClassRegInfo.clsid, comClassRegInfo);
                                }
                            }
                        }
                    }
                    break;

                    case ParseStage.Interface:
                    {
                        var interfaceNode = item.GetNode(regNodePath.Key);
                        if (interfaceNode == null)
                        {
                            continue;
                        }
                        foreach (TreeNode interfaceNodeItem in interfaceNode.Nodes)
                        {
                            ComInterfaceExternalProxyStubRegInfo externalProxyStub;
                            ComInterfaceProxyStubRegInfo         proxyStub;
                            ParseInterface(interfaceNodeItem, out externalProxyStub, out proxyStub);
                            if (!_allRegComInterfaceExternalProxyStub.ContainsKey(externalProxyStub.iid))
                            {
                                _allRegComInterfaceExternalProxyStub.Add(externalProxyStub.iid, externalProxyStub);
                            }
                            if (!_allRegComInterfaceProxyStub.ContainsKey(proxyStub.iid))
                            {
                                _allRegComInterfaceProxyStub.Add(proxyStub.iid, proxyStub);
                            }
                        }
                    }
                    break;

                    case ParseStage.TypeLib:
                    {
                        var typeLibNode = item.GetNode(regNodePath.Key);
                        if (typeLibNode == null)
                        {
                            continue;
                        }
                        foreach (TreeNode typeLibItem in typeLibNode.Nodes)
                        {
                            Dictionary <string, ComTypeLibRegInfo> comClassRegInfoList;
                            ParseTypeLib(typeLibItem, out comClassRegInfoList);
                            foreach (var fileItem in comClassRegInfoList)
                            {
                                ComFileInfo fileInfo;
                                if (_allRegComFiles.TryGetValue(fileItem.Key, out fileInfo))
                                {
                                    if (!fileInfo.TypeLibInfo.ContainsKey(fileItem.Value.tlbid))
                                    {
                                        fileInfo.TypeLibInfo.Add(fileItem.Value.tlbid, fileItem.Value);
                                    }
                                }
                                //else
                                //{
                                //    fileInfo = new ComFileInfo();
                                //    fileInfo.TypeLibInfo.Add(fileItem.Value.tlbid, fileItem.Value);
                                //    _allComFiles.Add(fileItem.Key, fileInfo);
                                //}
                            }
                        }
                    }
                    break;

                    case ParseStage.ProgId:
                    {
                        var progIdNode = item.GetNode(regNodePath.Key);
                        if (progIdNode == null)
                        {
                            continue;
                        }
                        foreach (TreeNode progIdItem in progIdNode.Nodes)
                        {
                            string progid = progIdItem.Text;
                            string clsid  = progIdItem.GetNodeValue("CLSID");
                            if ((!string.IsNullOrWhiteSpace(clsid)) && (!string.IsNullOrWhiteSpace(progid)))
                            {
                                ComClassRegInfo comclassInfo;
                                if (_allRegComClasses.TryGetValue(clsid, out comclassInfo))
                                {
                                    comclassInfo.AddProgId(progid);
                                }
                            }
                        }
                    }
                    break;
                    }
                }
            }

            foreach (var item in _allRegFileNameEmptyClasses)
            {
                foreach (var fileInfo in _allRegComFiles)
                {
                    if (fileInfo.Value.TypeLibInfo.ContainsKey(item.Value.tlbid))
                    {
                        if (!fileInfo.Value.ComClassInfo.ContainsKey(item.Value.clsid))
                        {
                            fileInfo.Value.ComClassInfo.Add(item.Value.clsid, item.Value);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void UpdateTypeInfo(List <string> listOfComFile, List <string> allFiles)
        {
            foreach (var item in listOfComFile)
            {
                string filePath = allFiles.GetFilePath(item);
                if (File.Exists(filePath))
                {
                    string      tlbFilePath = string.Format("{0}\\{1}.tlb", Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
                    ComFileInfo fileInfo;
                    string      fileName = Path.GetFileName(filePath);
                    ITypeLib    typeLib;
                    if (File.Exists(tlbFilePath))
                    {
                        typeLib = TypeLib.GetTypeLib(tlbFilePath);
                    }
                    else
                    {
                        typeLib = TypeLib.GetTypeLib(filePath);
                    }

                    if (typeLib == null)
                    {
                        if (_allRegComFiles.TryGetValue(fileName, out fileInfo))
                        {
                            fileInfo.name = fileName;
                            _allFileComFiles.Add(fileName, fileInfo);
                        }
                        continue;
                    }

                    fileInfo      = new ComFileInfo();
                    fileInfo.name = fileName;
                    ComTypeLibRegInfo typeLibInfo = new ComTypeLibRegInfo();
                    _allFileComFiles.Add(fileInfo.name, fileInfo);

                    IntPtr typeLibAttribPtr;
                    typeLib.GetLibAttr(out typeLibAttribPtr);

                    if (typeLibAttribPtr != IntPtr.Zero)
                    {
                        var typeLibAttrib = Marshal.PtrToStructure <System.Runtime.InteropServices.ComTypes.TYPELIBATTR>(typeLibAttribPtr);

                        typeLibInfo.tlbid   = typeLibAttrib.guid.ToString("B");
                        typeLibInfo.version = typeLibAttrib.wMajorVerNum.ToString();
                        typeLibInfo.flags   = typeLibAttrib.wLibFlags.ToString();
                        typeLibInfo.helpdir = "";
                        string typelibname;
                        string description;
                        int    dwHelpContext;
                        string helpStr;
                        typeLib.GetDocumentation(-1, out typelibname, out description, out dwHelpContext, out helpStr);
                        typeLibInfo.name = typelibname;
                        fileInfo.TypeLibInfo.Add(typeLibInfo.tlbid, typeLibInfo);
                    }

                    typeLib.ReleaseTLibAttr(typeLibAttribPtr);

                    int totalNoDefined = typeLib.GetTypeInfoCount();

                    for (int typeIndex = 0; typeIndex < totalNoDefined; typeIndex++)
                    {
                        ITypeInfo typeInfo;
                        typeLib.GetTypeInfo(typeIndex, out typeInfo);

                        IntPtr typeAttrPtr;
                        typeInfo.GetTypeAttr(out typeAttrPtr);

                        if (typeAttrPtr != IntPtr.Zero)
                        {
                            var typeAttr = Marshal.PtrToStructure <System.Runtime.InteropServices.ComTypes.TYPEATTR>(typeAttrPtr);
                            if (typeAttr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS)
                            {
                                ComClassRegInfo coClassInfo;
                                string          clsid = typeAttr.guid.ToString("B");
                                if (!_allRegComClasses.TryGetValue(clsid, out coClassInfo))
                                {
                                    coClassInfo       = new ComClassRegInfo();
                                    coClassInfo.clsid = clsid;
                                }
                                string className;
                                string description;
                                int    dwHelpContext;
                                string helpStr;
                                typeInfo.GetDocumentation(-1, out className, out description, out dwHelpContext, out helpStr);
                                coClassInfo.tlbid       = typeLibInfo.tlbid;
                                coClassInfo.description = description;
                                coClassInfo.name        = className;
                                if (string.IsNullOrWhiteSpace(coClassInfo.progid))
                                {
                                    coClassInfo.AddProgId(string.Format("{0}.{1}", typeLibInfo.name, className));
                                }
                                fileInfo.ComClassInfo.Add(clsid, coClassInfo);
                            }
                            else if (typeAttr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_INTERFACE ||
                                     typeAttr.typekind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_DISPATCH)
                            {
                                ComInterfaceExternalProxyStubRegInfo proxyStub;
                                string clsid = typeAttr.guid.ToString("B");
                                if (!_allRegComInterfaceExternalProxyStub.TryGetValue(clsid, out proxyStub))
                                {
                                    proxyStub     = new ComInterfaceExternalProxyStubRegInfo();
                                    proxyStub.iid = clsid;
                                    if (typeAttr.wTypeFlags == System.Runtime.InteropServices.ComTypes.TYPEFLAGS.TYPEFLAG_FDUAL ||
                                        typeAttr.wTypeFlags == System.Runtime.InteropServices.ComTypes.TYPEFLAGS.TYPEFLAG_FOLEAUTOMATION)
                                    {
                                        //Just add std automation proxy
                                        proxyStub.proxyStubClsid32 = "{00020424-0000-0000-C000-000000000046}";
                                    }
                                }
                                string interfaceName;
                                string description;
                                int    dwHelpContext;
                                string helpStr;
                                typeInfo.GetDocumentation(-1, out interfaceName, out description, out dwHelpContext, out helpStr);
                                proxyStub.tlbid = typeLibInfo.tlbid;
                                proxyStub.name  = interfaceName;
                                fileInfo.InterfaceInfo.Add(clsid, proxyStub);
                            }
                        }

                        typeInfo.ReleaseTypeAttr(typeAttrPtr);
                    }
                }
            }
        }