cocus

Untitled

Jan 16th, 2013
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3.  
  4. ' IUnknown interface
  5. ' every other interface inherits from IUnknown
  6. Private Type IUnknown
  7.     QueryInterface          As Long
  8.     AddRef                  As Long
  9.     Release                 As Long
  10. End Type
  11.  
  12. ' ITypeLib interface
  13. ' information about a type library
  14. Private Type ITypeLib
  15.     IUnk                    As IUnknown ' inherits from IUnknown
  16.    GetTypeInfoCount        As Long     ' retrieves the number of type descriptions in the library
  17.    GetTypeInfo             As Long     ' retrieves the specified type description in the library
  18.    GetTypeInfoType         As Long     ' retrieves the type of a type description
  19.    GetTypeInfoOfGuid       As Long     ' retrieves the type description that corresponds to the specified (GUID)
  20.    GetLibAttr              As Long     ' retrieves the structure that contains the library's attributes
  21.    GetTypeComp             As Long     ' retrieves a pointer to the ITypeComp for a type library
  22.    GetDocumentation        As Long     ' retrieves the library's documentation string
  23.    IsName                  As Long     ' indicates whether a passed-in string contains the name of a type or a member described in the library
  24.    FindName                As Long     ' finds occurrences of a type description in a type library
  25.    ReleaseTLibAttr         As Long     ' releases the TLIBATTR originally obtained from GetLibAttr
  26. End Type
  27.  
  28. Private Const S_OK = 0
  29.  
  30. ' load a type library from a file
  31. Private Declare Function LoadTypeLib Lib "oleaut32" ( _
  32.     ByVal szFile As Long, pptlib As Any) As Long
  33.    
  34. ' copy memory from one location to the other
  35. Private Declare Sub CpyMem Lib "kernel32" Alias "RtlMoveMemory" ( _
  36.     pDst As Any, pSrc As Any, ByVal dwLen As Long)
  37.  
  38. Private Declare Function RegisterTypeLibForUser Lib "oleaut32" ( _
  39.      pptlib As Any, ByVal szFullPath As Long, ByVal szHelpDir As Long) As Long
  40.    
  41.  
  42. ' open a type library
  43. Public Function RegisterTypeLib(ByVal sFile As String) As Boolean
  44.     Dim ret         As Long
  45.     Dim pVTbl       As Long
  46.     Dim oTypeLib    As Long
  47.     Dim udtTypeLib  As ITypeLib
  48.    
  49.     ret = LoadTypeLib(StrPtr(sFile), oTypeLib)
  50.  
  51.     If Not (ret = S_OK) Then
  52.         MsgBox "Error reading TypeLib of file '" & sFile & "'", vbCritical
  53.         Exit Function
  54.     End If
  55.    
  56.     MsgBox "Opened TLB"
  57.    
  58.    
  59.     ' get the VTable of ITypeLib
  60.    CpyMem pVTbl, ByVal oTypeLib, 4
  61.     CpyMem udtTypeLib, ByVal pVTbl, Len(udtTypeLib)
  62.  
  63.     MsgBox "Ref set"
  64.  
  65.     If Not (RegisterTypeLibForUser(udtTypeLib, VarPtr(sFile), VarPtr(GetDir(sFile))) = S_OK) Then
  66.         MsgBox "Error registering TypeLib of file '" & sFile & "'", vbCritical
  67.         Exit Function
  68.     End If
  69.  
  70.     RegisterTypeLib = True
  71.  
  72. End Function
  73.  
  74. Public Function GetDir(ByVal strPath As String) As String
  75.     GetDir = Left$(strPath, InStrRev(strPath, "\"))
  76. End Function
Advertisement
Add Comment
Please, Sign In to add comment