Visual Basic Programming Code Examples
Visual Basic > Applications VBA Code Examples
Querying the interface of an object in a typelib (COM object)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
Querying the interface of an object in a typelib (COM object)
The following code demonstrates how to iterate/enumerate all the objects in a typelib and returns the exposed interface (from the objects vTable) of these objects in an array.
'Purpose : Returns an array containing the exposed objects contained inside a COM object.
'Inputs : sFileName The file name of the object to return the interface of.
'Outputs : Returns a one based string array of object contained in a typelib.
'Notes : Requires "Typelib Information" (TLBINF32.DLL) to be installed.
Function TypeLibObjects(sFileName As String) As Variant
Dim oTypeLib As Object 'TLI.TypeLibInfo
Dim oTypeInfo As Object 'TLI.TypeInfo
Dim asObjects() As String, lCount As Long
On Error GoTo ErrFailed
Set oTypeLib = CreateObject("TLI.TypeLibInfo")
oTypeLib.ContainingFile = sFileName
If oTypeLib.TypeInfoCount Then
ReDim asObjects(1 To oTypeLib.TypeInfoCount)
For Each oTypeInfo In oTypeLib.TypeInfos
lCount = lCount + 1
asObjects(lCount) = oTypeInfo.Name
Next
TypeLibObjects = asObjects
End If
Set oTypeLib = Nothing
Set oTypeInfo = Nothing
Exit Function
ErrFailed:
Debug.Print "Error in TypeLibObjects " & Err.Description
Debug.Assert False
End Function
'Purpose : Returns an array containing the interface from a COM object, i.e. iterates
' through the entities exposed of a type library
'Inputs : sFileName The file name of the object to return the interface of.
'Outputs : Returns a one based string array of interface call names
'Notes : Requires "Typelib Information" (TLBINF32.DLL) to be installed.
Function TypeLibObjectInterface(sFileName As String, sObjectName As String) As Variant
Dim oTypeLib As Object 'TLI.TypeLibInfo
Dim oSearchResults As Object 'TLI.SearchResults
Dim oThisItem As Object
Dim asInterface() As String
Dim lSearchData As Long
Dim lCount As Long
On Error GoTo ErrFailed
Set oTypeLib = CreateObject("TLI.TypeLibInfo")
oTypeLib.ContainingFile = sFileName
If oTypeLib.TypeInfoCount Then
lSearchData = oTypeLib.MakeSearchData(sObjectName)
Set oSearchResults = oTypeLib.GetMembers(lSearchData)
'Found required object, return interface
ReDim asInterface(1 To oSearchResults.Count)
For Each oThisItem In oSearchResults
lCount = lCount + 1
asInterface(lCount) = oThisItem.Name
Next
TypeLibObjectInterface = asInterface
End If
Set oTypeLib = Nothing
Set oSearchResults = Nothing
Exit Function
ErrFailed:
Debug.Print "Error in TypeLibObjectInterface " & Err.Description
Debug.Assert False
End Function
'Demonstration of how to return an the interface of a COM object
Sub Test()
Dim avObjects As Variant, avInterface As Variant
Dim lThisObject As Long, lThisItem As Long
avObjects = TypeLibObjects("C:\WINDOWS\SYSTEM\MSCAL.OCX")
For lThisObject = 1 To UBound(avObjects)
Debug.Print "------------------------------"
Debug.Print "Interface for object " & avObjects(lThisObject)
avInterface = TypeLibObjectInterface("C:\WINDOWS\SYSTEM\MSCAL.OCX", CStr(avObjects(lThisObject)))
For lThisItem = 1 To UBound(avInterface)
Debug.Print avInterface(lThisItem)
Next
Next
End Sub