VB and VBA Users Source Code: Querying the interface of an object in a typelib (COM object)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Querying the interface of an object in a typelib (COM object)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Saturday, February 23, 2002
Hits:
939
Category:
Files/Directories/IO
Article:
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. 'Author : Andrew Baker (copyright www.vbusers.com) 'Date : 31/Jan/2002 'Notes : Requires "Typelib Information" (TLBINF32.DLL) to be installed. 'Revisions : 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 'Author : Andrew Baker (copyright www.vbusers.com) 'Date : 31/Jan/2002 'Notes : Requires "Typelib Information" (TLBINF32.DLL) to be installed. 'Revisions : 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
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder