Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I traverse a COM Object?

Status
Not open for further replies.

jbullman

Instructor
Jan 7, 2002
34
US
I have the need to take a COM object and programmatically go from top to bottom in it, writing out the objects, collections, properties, etc. I know how to look them up in Object Browser, but how do I produce my own list? I need this to work in a generic way, so it can take any object and spell it out.

I imagine this is easy if you know how. I've looked in the documentation and don't know where to start. I know basic VB but not how to create COM objects.

Thanks for any help.

Judith Ullman
Metro New York Crystal Decisions Training and Consulting Partner
 
Have you tried using the TypeLib Information objects. These are what the Object Browswer uses for its output, and you can include a reference into your project, and then use the same tools.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
And several examples have of the above have been given in forum222. Just do a keyword search there
 
Here is a snipet of my code from my generic test harness for stateless COM objects. Given a DLL it will open it up and load the interfaces into the first list box. Then clicking on one of the interfaces will load the best interface for that interface. (useless step I know) Then clicking that will display the members. Note that you'll see more than just your interface as the IDispatch and IUnknown interface will also be displayed. Going further down and getting info about a member's property is pretty straight forward.

Code:
Private Sub cmdFileBrowse_Click()
On Error GoTo ErrorHandler
    dlgFile.DialogTitle = "Open Script"
    dlgFile.Filter = "ActiveX DLL (*.DLL)|*.DLL|"
    dlgFile.ShowOpen
    txtDLLPath.Text = dlgFile.FileName
    Set oTLI = oTLIApp.TypeLibInfoFromFile(txtDLLPath.Text)
    
    'lstClasses.AddItem oTLI.Name
    Dim intf As InterfaceInfo
    lstClasses2.ListItems.Clear
    For Each intf In oTLI.Interfaces
        lstClasses2.ListItems.Add , intf.Name, oTLI.Name & "." & Right(intf.Name, Len(intf.Name) - 1)
    Next intf
Exit Sub
ErrorHandler:
End Sub

Private Sub lstClasses2_Click()
On Error GoTo ErrorHandler
    Dim tObj As Object
    Dim tmpTLI As tLI.TypeInfo
    Dim tInterface As tLI.InterfaceInfo
    Dim iInterfaceNum As Integer
    lstInterfaces.ListItems.Clear
    If optType(0).Value = True Then
        If Not lstClasses2.SelectedItem Is Nothing Then
            Set tObj = CreateObject(lstClasses2.SelectedItem.Text)
            Set tmpTLI = BestClassInfo(tObj)
            For iInterfaceNum = 1 To tmpTLI.Interfaces.Count
                Set tInterface = tmpTLI.Interfaces(iInterfaceNum)
                lstInterfaces.ListItems.Add , "I" & iInterfaceNum, tInterface.Name
            Next
            'For Each tInterface In tmpTLI.Interfaces
            '    lstInterfaces.ListItems.Add , tInterface.Name, tInterface.Name
            'Next
        End If
    Else
        If Not lstClasses2.SelectedItem Is Nothing Then
            txtLine.Text = IIf(optType(1).Value, "CREATE", "DESTROY") & CONST_DELIM & lstClasses2.SelectedItem.Text
        End If
    End If
Exit Sub
ErrorHandler:
    MsgBox "Could not load type library info"
End Sub

Private Sub lstInterfaces_Click()
    Dim tObj As Object
    Dim tmpTLI As tLI.TypeInfo
    Dim tInterface As tLI.InterfaceInfo
    Dim tMember As tLI.MemberInfo
    Dim iMemberNum As Integer
    
    lstMembers.ListItems.Clear
    If Not lstInterfaces.SelectedItem Is Nothing Then
        Set tObj = CreateObject(lstClasses2.SelectedItem.Text)
        Set tmpTLI = BestClassInfo(tObj)
        Set tInterface = tmpTLI.Interfaces(Right(lstInterfaces.SelectedItem.Key, Len(lstInterfaces.SelectedItem.Key) - 1))
        For iMemberNum = 1 To tInterface.Members.Count
            Set tMember = tInterface.Members(iMemberNum)
            lstMembers.ListItems.Add , "M" & iMemberNum, tMember.Name
        Next
    End If
End Sub

Public Function BestClassInfo(ByVal MyObject As Object) As tLI.TypeInfo
    Set BestClassInfo = tLI.ClassInfoFromObject(MyObject)
    On Error GoTo NotAvailable
    With BestClassInfo.Parent
        With tLI.TypeLibInfoFromRegistry(.Guid, .MajorVersion, .MinorVersion, .LCID)
            Set BestClassInfo = .Me.TypeInfos.IndexedItem(BestClassInfo.TypeInfoNumber)
        End With
    End With
Exit Function
NotAvailable:
Err.Clear
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top