一个查看类型库(Type Library)详细信息的工具


        在用C#操作Excel作报表的时候,你可能遇到这样的困扰(用其他COM时也存在类似的问题):引用的组件是哪个版本的Office提供的;想使用tlbimp得到excel.dll,却不知道对哪个文件执行tlbimp(Excel9.olb或Excel.exe)。对于前者我自己深有体会:Office XP以后的版本比Office 2000提供的一些方法的参数要多(比如Excel.Workbooks.Open方法),而且引用Office XP以后版本的dll的.NET应用程序在安装Office 2000的系统上可能会出问题(我遇到过由Excel._Worksheet CurSheet = (Excel._Worksheet)WorkBook.Sheets[1];引发的null值问题),另外,不同版本的Excel引发的客户操作也可能是不同的,如果引用Office 2000下的dll后使用Excel.Workbooks[0].Worksheets[0].Delete()方法删除一个sheet时,会弹出以下对话框:

o_typlibregdelsheet.jpg

而引用Office XP以后版本的dll就不会出现这个对话框。 

        我们在C# Project中添加引用时,可以看到下面这个对话框:

o_typelibregnet.jpg

        这个对话框列举了当前系统中所有的COM组件,但是它提供的关于类型库的信息并不详细,而且没有搜索功能,用起来不是很方便。
        其实,类型库的信息存储在注册表的HKEY_CLASSES_ROOT->TypeLib下:

o_typlibreg.jpg

        我们可以使用Microsoft.Win32.RegistryKey类来获取这些信息:

o_typlibregdetailmain.jpg

        搜索:

o_typlibregsearch.jpg

        类型库详细信息:

o_typlibregdetail.jpg

        代码下载:Down 

        下载内容包括:

o_typlibregfiles.jpg

 

        TypeLibs的详细内容:

  1 None.gif using  System;
  2 None.gif
  3 None.gif namespace  TypeLibBrowser
  4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  5ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  6InBlock.gif    /// TypeLibs 的摘要说明。
  7ExpandedSubBlockEnd.gif    /// </summary>

  8InBlock.gif    public class TypeLibs : System.Collections.CollectionBase
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 10InBlock.gif        public TypeLibs()
 11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 12InBlock.gif            //
 13InBlock.gif            // TODO: 在此处添加构造函数逻辑
 14InBlock.gif            //
 15ExpandedSubBlockEnd.gif        }

 16InBlock.gif
 17InBlock.gif        private string Obj2Str(object pObj)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            if (pObj == null)
 20InBlock.gif                return String.Empty;
 21InBlock.gif            else
 22InBlock.gif                return pObj.ToString();
 23ExpandedSubBlockEnd.gif        }

 24InBlock.gif
 25InBlock.gif        public void GetTL()
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 27InBlock.gif            const string ROOT_TLB = "TypeLib";
 28InBlock.gif
 29InBlock.gif            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot;
 30InBlock.gif            Microsoft.Win32.RegistryKey regTLibRoot = regKey.OpenSubKey(ROOT_TLB);
 31InBlock.gif
 32InBlock.gif            foreach(string strTypeLib in regTLibRoot.GetSubKeyNames())
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 34InBlock.gif                Microsoft.Win32.RegistryKey regTL = regTLibRoot.OpenSubKey(strTypeLib);
 35InBlock.gif                foreach(string strVersion in regTL.GetSubKeyNames())
 36ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 37InBlock.gif                    Microsoft.Win32.RegistryKey regVersion = regTL.OpenSubKey(strVersion);
 38InBlock.gif                    TypeLibBrowser.TypeLib typeLib = new TypeLib();
 39InBlock.gif                    typeLib.GUID = strTypeLib;
 40InBlock.gif                    typeLib.Version = strVersion;
 41InBlock.gif                    foreach(string strValueName in regVersion.GetValueNames())
 42ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 43InBlock.gif                        switch (strValueName)
 44ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 45InBlock.gif                            case "PrimaryInteropAssemblyName" :
 46InBlock.gif                                typeLib.PrimaryInteropAssemblyName = this.Obj2Str(regVersion.GetValue(strValueName));
 47InBlock.gif                                break;
 48InBlock.gif                            default :
 49InBlock.gif                                typeLib.FullName = this.Obj2Str(regVersion.GetValue(strValueName));
 50InBlock.gif                                break;
 51ExpandedSubBlockEnd.gif                        }

 52ExpandedSubBlockEnd.gif                    }

 53InBlock.gif                    foreach(string strSubKey in regVersion.GetSubKeyNames())
 54ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 55InBlock.gif                        Microsoft.Win32.RegistryKey regSubKey = regVersion.OpenSubKey(strSubKey);
 56InBlock.gif                        switch (strSubKey)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 58InBlock.gif                            case "FLAGS" :
 59InBlock.gif                                typeLib.Flags = this.Obj2Str(regSubKey.GetValue(""));
 60InBlock.gif                                break;
 61InBlock.gif                            case "HELPDIR" :
 62InBlock.gif                                typeLib.HelpDir = this.Obj2Str(regSubKey.GetValue(""));
 63InBlock.gif                                break;
 64InBlock.gif                            default :
 65InBlock.gif                                TypeLibBrowser.TLBFiles typeLibFiles = new TLBFiles();
 66InBlock.gif                                foreach(string strPlatform in regSubKey.GetSubKeyNames())
 67ExpandedSubBlockStart.gifContractedSubBlock.gif                                dot.gif{
 68InBlock.gif                                    TypeLibBrowser.TLBFile tlbFile = new TLBFile();
 69InBlock.gif                                    tlbFile.Number = strSubKey;
 70InBlock.gif                                    switch (strPlatform.Trim().ToUpper())
 71ExpandedSubBlockStart.gifContractedSubBlock.gif                                    dot.gif{
 72InBlock.gif                                        case "WIN16" :
 73InBlock.gif                                            tlbFile.Platform = TypeLibBrowser.TypeLibraryPlatformType.Win16;
 74InBlock.gif                                            break;
 75InBlock.gif                                        case "WIN32" :
 76InBlock.gif                                            tlbFile.Platform = TypeLibBrowser.TypeLibraryPlatformType.Win32;
 77InBlock.gif                                            break;
 78InBlock.gif                                        case "WIN64" :
 79InBlock.gif                                            tlbFile.Platform = TypeLibBrowser.TypeLibraryPlatformType.Win64;
 80InBlock.gif                                            break;
 81InBlock.gif                                        default :
 82InBlock.gif                                            tlbFile.Platform = TypeLibBrowser.TypeLibraryPlatformType.未知;
 83InBlock.gif                                            break;
 84ExpandedSubBlockEnd.gif                                    }

 85InBlock.gif                                    tlbFile.File = this.Obj2Str(regSubKey.OpenSubKey(strPlatform).GetValue(""));                    
 86InBlock.gif                                    typeLibFiles.Add(tlbFile);
 87ExpandedSubBlockEnd.gif                                }

 88InBlock.gif                                typeLib.Files = typeLibFiles;
 89InBlock.gif                                break;
 90ExpandedSubBlockEnd.gif                        }

 91ExpandedSubBlockEnd.gif                    }

 92InBlock.gif                    List.Add(typeLib);
 93ExpandedSubBlockEnd.gif                }

 94ExpandedSubBlockEnd.gif            }

 95ExpandedSubBlockEnd.gif        }

 96InBlock.gif
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 98InBlock.gif        /// 根据GUID和Version搜索Type Library
 99InBlock.gif        /// </summary>
100InBlock.gif        /// <param name="pGUID"></param>
101InBlock.gif        /// <param name="pVersion"></param>
102ExpandedSubBlockEnd.gif        /// <returns></returns>

103InBlock.gif        public TypeLibBrowser.TypeLib GetTLDetail(string pGUID, string pVersion)
104ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
105InBlock.gif            if (this.InnerList.Count == 0)
106InBlock.gif                this.GetTL();
107InBlock.gif
108InBlock.gif            foreach (TypeLibBrowser.TypeLib t in this.InnerList)
109InBlock.gif                if (pGUID == t.GUID && pVersion == t.Version)
110InBlock.gif                    return t;
111InBlock.gif
112InBlock.gif            return null;
113ExpandedSubBlockEnd.gif        }

114ExpandedSubBlockEnd.gif    }

115ExpandedBlockEnd.gif}

116 None.gif

        再回到本文开头提出的两个问题,我们可以发现在“详细信息”中包含了我们想知道的答案。

        BTW:微软为Excel VBA提供的帮助本身就包含两个版本:2000和2002的,2003的应该与2002的相同,需要这个帮助(包括VBA For Access 2000、Excel 2000 、Excel 2002、Outlook 2000、Outlook 2002、Word 2000、Word 2002的资料)的朋友可以留下自己的Email,文件有点大,我无法上传,这些帮助对我们使用C#操作Office大有裨益!
        
        emteeth.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值