Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Type Library Browser for .NET 2.0

0.00/5 (No votes)
13 Nov 2005 1  
Example application to demonstrate how to get the COM+ type library information from the Registry and display it.

Sample Image - typelibbrowser.jpg

Introduction

Just wondered around the other day and tried to find a ready made class library to get COM+/ActiveX Type Library information from registered objects. Since I couldn't find anything, I decided to quickly implement my own version.

The solution

The basic idea is that you should create a new object of type TypeLibraries, and with a single call of the method GetAllFromRegistry(), should populate it with information from the Registry. The form of collection is CollectionBase, so basically it should be easy to sort, search, and modify the way you want. Another method you can find is FindByGUIDAndVersion which searches from the TypeLibraries collection TypeLibrary class with similar GUID and version information - this is because many times the GUID might be the same, but only the version changes.

The whole thing is basically in the following code - the rest of the classes are merely placeholders:

Public Sub GetAllFromRegistry()

    Const TLB_ROOT As String = "TypeLib"
    Dim rkey As RegistryKey = Registry.ClassesRoot

    Dim regTypeLibRoot As RegistryKey = rkey.OpenSubKey(TLB_ROOT)

    For Each strTypeLib As String In regTypeLibRoot.GetSubKeyNames
        Dim regTypeLib As RegistryKey = regTypeLibRoot.OpenSubKey(strTypeLib)

        ' In strVersions we should have list of typelib versions
        For Each strVersion As String In regTypeLib.GetSubKeyNames
            Dim regVersion As RegistryKey = regTypeLib.OpenSubKey(strVersion)

            ' Now we can create a new TypeLibrary object since under 
            ' this value there should be only descriptive info of Type Library
            Dim typeLib As New TypeLibrary

            ' Set values we know by now
            typeLib.GUID = strTypeLib
            typeLib.Version = strVersion

            ' Here we should have at least one value for 
            ' descriptive name of TypeLib - not always though...
            For Each strValueName As String In regVersion.GetValueNames
                Select Case strValueName
                    Case "PrimaryInteropAssemblyName"
                    ' We got name of PrimaryInteropAssemblyName
                        typeLib.PrimaryInteropAssemblyName = _
                                regVersion.GetValue(strValueName)
                    Case Else ' We got descriptive name of TypeLib
                        typeLib.FullName = regVersion.GetValue(strValueName)
                End Select
            Next

            ' Iterate through subkeys
            For Each strSubKey As String In regVersion.GetSubKeyNames

                Dim regSubKey As RegistryKey = regVersion.OpenSubKey(strSubKey)

                Select Case strSubKey
                    Case "FLAGS" : typeLib.Flags = regSubKey.GetValue("")
                    Case "HELPDIR" : typeLib.HelpDir = regSubKey.GetValue("")
                    Case Else ' We found propably number with subkey 
                              ' of "win32","win16" or even "win64"

                        Dim typeLibFiles As New TypeLibraryFiles

                        For Each strPlatform As String In regSubKey.GetSubKeyNames
                            Dim tlbFile As New TypeLibraryFile
                            tlbFile.Number = strSubKey
                            Select Case UCase(Trim(strPlatform))
                                Case "WIN16" : tlbFile.Platform = _
                                     TypeLibraryFile.TypeLibraryPlatformType.Win16
                                Case "WIN32" : tlbFile.Platform = _
                                     TypeLibraryFile.TypeLibraryPlatformType.Win32
                                Case "WIN64" : tlbFile.Platform = _
                                     TypeLibraryFile.TypeLibraryPlatformType.Win64
                                Case Else : tlbFile.Platform = _
                                     TypeLibraryFile.TypeLibraryPlatformType.Unknown
                            End Select

                            tlbFile.File = _
                                    regSubKey.OpenSubKey(strPlatform).GetValue("")
                            ' Get name of file as a default value

                            typeLibFiles.Add(tlbFile)
                            ' Add file to collection
                        Next

                        typeLib.Files = typeLibFiles
                        ' Set files collection to typelib info
                End Select
            Next

            MyBase.List.Add(typeLib)
        Next
    Next
End Sub

Public Function FindByGUIDAndVersion(ByVal GUID As String, _
                ByVal Version As String) As TypeLibrary
    If Me.InnerList.Count = 0 Then Me.GetAllFromRegistry()
    ' if no data exists, populate
    For Each t As TypeLibrary In Me.InnerList
        If GUID = t.GUID And Version = t.Version Then Return t
    Next

    Return Nothing
End Function

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here