Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Accessing Type Libraries from VB


December 1999/Accessing Type Libraries from VB/Listing 1

Listing 1: typeinfo.bas — Code to browse type libraries

Attribute VB_Name = "basTypeInfo"
Option Explicit
Public Sub invoke(nSelected As Node, obj As Object)

    Dim iDash As Integer
    Dim iParmIndx As Integer
    Dim iNbrParms As Integer
    
    Dim sMethod As String
    Dim sParmValue As String
    
    Dim vParm As Variant
    
    Dim vaParms() As Variant
    
    Dim nChild As Node
    
    Dim ii As InterfaceInfo
    
    Dim mi As MemberInfo
    
    Dim tli As TLIApplication
    
'/* error trapping */
    On Error GoTo invoke_error
    
    Set tli = New TLIApplication
    
'/* methods/properties always delimited with a dash */
    sMethod = nSelected.Text
    iDash = InStr(sMethod, "-")
    If iDash > 0 Then
        
        sMethod = Trim$(Left$(sMethod, iDash - 1))
        
        Set ii = tli.InterfaceInfoFromObject(obj)
        
    '/* get member by id */
    '/* (tag set in loadTreeViewWithClassInfo) */
        Set mi = ii.Members.Item(nSelected.Tag)
        
        Select Case mi.InvokeKind
                
            Case INVOKE_FUNC
                
                iNbrParms = mi.Parameters.Count
                If iNbrParms > 0 Then
                    
                    ReDim vaParms(iNbrParms - 1)
                    
                '/* there is one child item in treeview */
                '/* for each invoke method parameter */
                    Set nChild = nSelected.Child
                    For iParmIndx = 1 To nSelected.Children
                        
                        sParmValue = InputBox( _
                                     "Enter Parameter Value " & _
                                     nChild.Text)
                        
                    '/* blank - assume optional and remove */
                        If Trim$(sParmValue) = "" Then
                            ReDim Preserve vaParms(iNbrParms - 1)
                            
                        Else
                        '/* set parameter type */
                        '/* (tag set in loadTreeViewWithClassInfo) */
                            vaParms(iNbrParms - 1) = _
                                setType(sParmValue, nChild.Tag)
                        End If
                        
                    '/* in reverse order! */
                        iNbrParms = iNbrParms - 1
                        Set nChild = nChild.Next
                        
                    Next iParmIndx
                    
                    vParm = tli.InvokeHookArray(obj, sMethod, _
                                        INVOKE_FUNC, vaParms())
                Else
                    
                '/* no parameters */
                    vParm = tli.InvokeHook(obj, sMethod, _
                                           INVOKE_FUNC)
                End If
                
                MsgBox "Return =  " & vParm
                
            Case INVOKE_PROPERTYPUT
                
                sParmValue = InputBox( _
                             "Enter Value for Property ", _
                             "Property Put")
                
                If sParmValue <> "" Then
                    vParm = setType(sParmValue, mi.ReturnType)
                    Call tli.InvokeHook(obj, sMethod, _
                                        INVOKE_PROPERTYPUT, vParm)
                End If
                
            Case INVOKE_PROPERTYGET
                
                vParm = tli.InvokeHook(obj, sMethod, _
                                 INVOKE_PROPERTYGET)
                MsgBox "Property Value = " & vParm
                
        End Select
    End If
    
invoke_exit:
    Set mi = Nothing
    Set ii = Nothing
    Set tli = Nothing
    Exit Sub
    
invoke_error:
    MsgBox Err.Number & " : " & Err.Description & " (invoke)"
    Resume invoke_exit
    
End Sub
    
Public Function loadListboxWithClassNames(sFilename As String, _
                                          lstClasses As ListBox) _
                                          As String
    
    Dim iTypeIndx As Integer
    
    Dim sRetInfo As String
    
    Dim ti As TypeLibInfo
    
    Dim tli As TLIApplication
    
'/* error trapping */
    On Error GoTo loadListboxWithClassNames_error
    
    Set tli = New TLIApplication
    Set ti = New TypeLibInfo
    
    Set ti = tli.TypeLibInfoFromFile(sFilename)
    sRetInfo = ti.Guid & "  " & ti.MajorVersion & "." & _
               ti.MinorVersion
    
    lstClasses.Clear
    For iTypeIndx = 1 To ti.TypeInfoCount
        
    '/* only classes - ignore anything else */
        If ti.TypeInfos.Item(iTypeIndx).TypeKind = _
                TKIND_COCLASS Then
            lstClasses.AddItem ti.Name & "." & _
                               ti.TypeInfos.Item(iTypeIndx).Name
        End If
    Next iTypeIndx
    
loadListboxWithClassNames_exit:
    Set ti = Nothing
    Set tli = Nothing
    
'/* return */
    loadListboxWithClassNames = sRetInfo
    Exit Function
    
loadListboxWithClassNames_error:
    MsgBox Err.Number & " : " & Err.Description & _
           " (loadListboxWithClassNames)"
    Resume loadListboxWithClassNames_exit
    
End Function
    
Public Function loadTreeviewWithClassInfo(sCOMName As String, _
                                          tvwClasses As TreeView) _
                                          As String
    
    Dim iMemberIndx As Integer
    Dim iNbrMembers As Integer
    Dim iParameterIndx As Integer
    Dim iTreeviewIndx As Integer
    Dim iSaveIndx As Integer
    Dim iNbrParms As Integer
    Dim iMemberID As Integer
    
    Dim sRetInfo As String
    
    Dim obj As Object
    
    Dim n As Node
    
    Dim ii As InterfaceInfo
    
    Dim mi As MemberInfo
    
    Dim ti As TypeLibInfo
    
    Dim tli As TLIApplication
    
'/* error trapping */
    On Error GoTo loadTreeviewWithClassInfo_error
    
    Set obj = CreateObject(sCOMName)
    
    Set tli = New TLIApplication
    
    Set ti = New TypeLibInfo
    
    Set ii = tli.InterfaceInfoFromObject(obj)
    
    iNbrMembers = ii.Members.Count
    
    Set ti = ii.Parent
    sRetInfo = ti.Guid & "  " & ti.MajorVersion & "." & _
               ti.MinorVersion
    
    tvwClasses.Nodes.Clear
    tvwClasses.Nodes.Add , , , Mid$(ii.Name, 2)
    
'/* treeview index, assign to each node sequentially */
    iTreeviewIndx = 1
'/* type lib id for method, ie. function or property get/put */
    iMemberID = 0
    
    For iMemberIndx = 1 To iNbrMembers
        
        Set mi = ii.Members.Item(iMemberIndx)
        iMemberID = iMemberID + 1
        
        If Not ignoreName(mi.Name) Then
            
        '/* method name, method type and return type */
            tvwClasses.Nodes.Add 1, tvwChild, , mi.Name & " - " & _
            getInvokeKindsName(mi.InvokeKind) & _
                " (" & getVTTypeName(mi.ReturnType) & ")"
            
            iTreeviewIndx = iTreeviewIndx + 1
            
        '/* save member id - see InvokeHook logic */
            tvwClasses.Nodes.Item(iTreeviewIndx).Tag = iMemberID
            
            iNbrParms = mi.Parameters.Count
            
            iSaveIndx = iTreeviewIndx
            For iParameterIndx = 1 To iNbrParms
                
            '/* parameter name and type as child node of method */
                tvwClasses.Nodes.Add iSaveIndx, tvwChild, , _
                    mi.Parameters.Item(iParameterIndx).Name & _
                    " (" & getVTTypeName(mi.Parameters.Item _
                    (iParameterIndx).VarTypeInfo) & ")"
                
                iTreeviewIndx = iTreeviewIndx + 1
                
            '/* save parameter type - see InvokeHook logic */
                tvwClasses.Nodes.Item(iTreeviewIndx).Tag = _
                    mi.Parameters(iParameterIndx).VarTypeInfo
                
            Next iParameterIndx
            
        End If
        
    Next iMemberIndx
    
    tvwClasses.Nodes(1).Expanded = True
    
loadTreeviewWithClassInfo_exit:
    Set mi = Nothing
    Set ii = Nothing
    Set ti = Nothing
    Set tli = Nothing
    Set obj = Nothing
    
'/* return */
    loadTreeviewWithClassInfo = sRetInfo
    Exit Function
    
    
loadTreeviewWithClassInfo_error:
    MsgBox Err.Number & " : " & Err.Description & _
           " (loadTreeviewWithClassInfo)"
    Resume loadTreeviewWithClassInfo_exit
    
End Function

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.