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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object attribute 2

Status
Not open for further replies.

hessrk

Programmer
Oct 3, 2001
6
0
0
US
Much like how the Watch window works in the VB6 IDE (where you can see the attributes of an object), how can I get those same attributes (the variables, functions, etc) so that I may save them to a text file (in my program)?

Thanks in advance,
Rodney
 
Use .NET - it supports object introspection. Seriously, you COULD write some code to call QueryInterface on the object's class to get the public interface. As far as the variables, you'd have to serialize them one at a time - just like it's done in a user control.
 
You might also take a took at TypeLib. Include type TypeLib Information Reference to the VB project, and then you can use code similar to the following:

Much of this code has been commented out this came from a routine where the only thing of interest was the name and value of properties, but further research into the TLIApplication will show that you can get function names and parameters lists as well.
Code:
Public Sub ShowProperties(rCtl_Control As Object)
    
   Dim lTLI_Appl          As TLIApplication
   Dim lInt_Interface     As InterfaceInfo
   Dim lMem_MemberInfo    As MemberInfo
   Dim lStr_PropName      As String
   Dim lStr_PropValue     As String
   
   Set lTLI_Appl = New TLIApplication
   Set lInt_Interface = TLI.InterfaceInfoFromObject(rCtl_Control)
   
   For Each lMem_MemberInfo In lInt_Interface.Members
      lStr_PropName = ""
      Select Case lMem_MemberInfo.InvokeKind
         Case INVOKE_FUNC
            ' Dont Want to Invoke the function
'                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
            ' Dont Want to Change the Value
'                sParmValue = InputBox( _
'                             "Enter Value for Property ", _
'                             "Property Put")
'
'                If sParmValue <> &quot;&quot; Then
'                    vParm = setType(sParmValue, mi.ReturnType)
'                    Call TLI.InvokeHook(obj, sMethod, _
'                                        INVOKE_PROPERTYPUT, vParm)
'                End If
         Case INVOKE_PROPERTYGET
            Select Case lMem_MemberInfo.ReturnType.VarType
               Case VT_I2, VT_EMPTY, VT_DISPATCH
                  ' Ignore these types
               Case Else
                  lStr_PropName = lMem_MemberInfo.Name
                  lStr_PropValue = CStr(lTLI_Appl.InvokeHook(rCtl_Control, lMem_MemberInfo.MemberId, INVOKE_PROPERTYGET))
                  MsgBox &quot;Control: &quot; & rCtl_Control.Name & vbCrLf & &quot;Property &quot; & lStr_PropName & &quot; = &quot; & lStr_PropValue
            End Select
      End Select
   Next lMem_MemberInfo
   
   Set lTLI_Appl = Nothing
   Set lInt_Interface = Nothing
   Set lMem_MemberInfo = Nothing

End Sub
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
It's relatively straightforward to do object inspection in VB. We've covered it on a number of occassions here. However, since the search engine is still down (hopefully, when it comes back it will have been worth the wait!), here's my solution. You will need to add a reference to the Typelib Information library, and a form with a command button and a listbox:
[tt]
Option Explicit

Private Sub Command1_Click()
getProperties Form1.Text1
End Sub

Public Sub getProperties(objTarget As Object)
Dim myTLI As TLIApplication
Dim myII As InterfaceInfo
Dim myMI As MemberInfo
Dim PropertyString As String

Set myTLI = New TLIApplication

Set myII = TLI.InterfaceInfoFromObject(objTarget)

For Each myMI In myII.Members
PropertyString = &quot;&quot;
' For this example only bother with properties that could return values
If myMI.InvokeKind = INVOKE_PROPERTYGET Then
PropertyString = myMI.Name
List1.AddItem PropertyString
End If
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top