Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Aug 26, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

GetClassesByInterface - Retrieve all the classes that implement a given interface

' Return all classes of the type library specified in
' TypeLibFile parameter that support a certain interface.
' the interface can be specified as an Object or through its IID

' Be sure that "TypeLib Information" type library (TlbInf32.tlb)
' is referenced in your VB project.

Function GetClassesByInterface(ByVal TypeLibFile As String, _
    ObjectOrInterfaceIID As Variant) As Collection
    Dim TLI As New TLIApplication
    Dim TypeLibrary As TypeLibInfo
    Dim Class As CoClassInfo
    Dim Interface As InterfaceInfo
    Dim IID_objInterface As String
    ' prepare the result
    Set GetClassesByInterface = New Collection
    ' raises an error if unable to open the type library
    ' (e.g. file not found or not a TLB)
    Set TypeLibrary = TLI.TypeLibInfoFromFile(TypeLibFile)
    ' get the IID of requested interface
    If VarType(ObjectOrInterfaceIID) = vbString Then
        IID_objInterface = ObjectOrInterfaceIID
        IID_objInterface = TLI.InterfaceInfoFromObject(ObjectOrInterfaceIID) _
    End If
    ' fill the return collection with names of
    ' classes that implement the interface
    For Each Class In TypeLibrary.CoClasses
        For Each Interface In Class.Interfaces
            ' compare GUIDs
            If StrComp(Interface.Guid, IID_objInterface, vbTextCompare) = 0 Then
                ' ok, this class implements the interface
                GetClassesByInterface.Add Class.Name, Class.Name
                Exit For
            End If
End Function
Alberto Falossi
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date