What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleUse the TypeLib routine Invokehook to find a control's name and invoke a routine
DescriptionThis example shows how to use the TypeLib routine InvokeHook to find a control's name and invoke a routine in Visual Basic 6. It uses the InvokeHook method with the INVOKE_PROPERTYGET command to invoke the control's Name property. It uses the INVOKE_FUNC command to invoke the routine.
KeywordsTypeLib, InvokeHook, get name
CategoriesSoftware Engineering, Controls
Thanks to Sergio Perciballi.

The program needs a reference to typelib information (tLbinf32.dll). MSDN has tLbinf32.dll documentation in addons area http://msdn.microsoft.com/vbasic/downloads/addon.asp and http://msdn.microsoft.com/vbasic/downloads/download.asp?ID=094. Sergio says "If you can understand this documentation then you must be a COM programmer!"

The hook subroutine calls InvokeHook, passing it a control, the name of the property to invoke (Name), and the INVOKE_PROPERTY command.

Private m_TLInf As TypeLibInfo

Sub hook()
    Dim ret As Variant
    'The Invoke members of the TLIApplication object
    'allow you to easily use IDispatch::Invoke directly
    ' from VB code
    'This has similar functionality to the VB6 callbyname
    ' function
    'I left out The ReverseArg();ret receives the property
    ' value
    ret = tli.InvokeHook(Me.TxtThisIsTheName, "Name", _
    MsgBox "The name of this textbox is '" & ret & "'"
End Sub
Subroutine hook2 invokes the form's foo subroutine. It calls InvokeHook passing it the name of the object (the form), the name of the method to invoke (foo), the INVOKE_FUNC command, and an array of arguments for the foo method.

The foo method displays its arguments.

Sub hook2()
    'call a function of form1 object
    'foo(integer,integer array)
    'Function call takes 2 arguments
    Dim tli As TLIApplication
    Set tli = New TLIApplication
    Dim arg() As Variant, iarr() As Integer
    Dim i As Integer
    Dim vt As Integer
    ReDim args(1): ReDim iarr(1)

    iarr(0) = 77: iarr(1) = 12345
    'arguments for function are in reverse order: Order
    ' that IDispatch::Invoke wants them
    'pointer to the integer array put in arg  array which
    ' ia a variant array
    args(0) = VarPtrArray(iarr)
    'In order to support ByRef parameters with
    ' InvokeHookArray,
    'you'll have to modify the Variants in the argument
    ' list by assigning a Long pointer value to the
    ' Variant, then using the CopyMemory API to modify the
    ' vt (VarType) field in the underlying Variant
    ' structure to be the VarType of the argument combined
    ' with the VT_BYREF bit.
    vt = VT_BYREF Or VT_ARRAY Or VT_I2
    CopyMemory args(0), vt, 2

    args(1) = VarPtr(i)
    vt = VT_BYREF Or VT_I2
    CopyMemory args(1), vt, 2

    tli.InvokeHookArray Form1, "Foo", INVOKE_FUNC, args
    Set tli = Nothing
End Sub

Sub foo(i As Integer, iarray() As Integer)
    Dim x As Integer
    MsgBox "In Foo subroutine: i= " & i
    ''MsgBox "iarray(0) " & iarray(0)
    MsgBox "iarray count= " & UBound(iarray) + 1
    'assume Lbound=0
    For x = 0 To UBound(iarray) '- 1
        MsgBox "iarray( " & x & ")= " & iarray(x)
    Next x
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.