VB and VBA Users Source Code: Returning the interface from a Type Library
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Returning the interface from a Type Library
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Saturday, May 15, 2004
Hits:
1251
Category:
Visual Basic General
Article:
The following code demonstrates how to return the interface from a type library: '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 : '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 : '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 'Get all the names of the objects in the COM control avObjects = TypeLibObjects("C:\WINDOWS\SYSTEM\MSCAL.OCX") For lThisObject = 1 To UBound(avObjects) 'Retrieve the interface for each object Debug.Print "------------------------------" Debug.Print "Interface for object " & avObjects(lThisObject) & ":" avInterface = TypeLibObjectInterface("C:\WINDOWS\SYSTEM\MSCAL.OCX", CStr(avObjects(lThisObject))) 'Display results 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