Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1530

[VB6] - Module for working with COM-Dll without registration.

$
0
0
Hello. I give my module for working with COM-DLL without registration in the registry.
The module has several functions:
  1. GetAllCoclasses - returns to the list of classes and unique identifiers are extracted from a type library.
  2. CreateIDispatch - creates IDispatch implementation by reference to the object and the name of the interface.
  3. CreateObjectEx2 - creates an object by name from a type library.
  4. CreateObjectEx - creates an object by CLSID.
  5. UnloadLibrary - unloads the DLL if it is not used.

vb Code:
  1. ' The module modTrickUnregCOM.bas - for working with COM libraries without registration.
  2. ' © Krivous Anatolii Anatolevich (The trick), 2015
  3.  
  4. Option Explicit
  5.  
  6.  D E C L A R A T I O N
  7.  
  8. Dim iidClsFctr      As GUID
  9. Dim iidUnk          As GUID
  10. Dim isInit          As Boolean
  11.  
  12. ' // Get all co-classes described in type library.
  13. Public Function GetAllCoclasses( _
  14.                 ByRef path As String, _
  15.                 ByRef listOfClsid() As GUID, _
  16.                 ByRef listOfNames() As String, _
  17.                 ByRef countCoClass As Long) As Boolean
  18.                
  19.     Dim typeLib As IUnknown
  20.     Dim typeInf As IUnknown
  21.     Dim ret     As Long
  22.     Dim count   As Long
  23.     Dim index   As Long
  24.     Dim pAttr   As Long
  25.     Dim tKind   As Long
  26.    
  27.     ret = LoadTypeLibEx(StrPtr(path), REGKIND_NONE, typeLib)
  28.    
  29.     If ret Then
  30.         Err.Raise ret
  31.         Exit Function
  32.     End If
  33.    
  34.     count = ITypeLib_GetTypeInfoCount(typeLib)
  35.     countCoClass = 0
  36.    
  37.     If count > 0 Then
  38.    
  39.         ReDim listOfClsid(count - 1)
  40.         ReDim listOfNames(count - 1)
  41.        
  42.         For index = 0 To count - 1
  43.        
  44.             ret = ITypeLib_GetTypeInfo(typeLib, index, typeInf)
  45.                        
  46.             If ret Then
  47.                 Err.Raise ret
  48.                 Exit Function
  49.             End If
  50.            
  51.             ITypeInfo_GetTypeAttr typeInf, pAttr
  52.            
  53.             GetMem4 ByVal pAttr + &H28, tKind
  54.            
  55.             If tKind = TKIND_COCLASS Then
  56.            
  57.                 memcpy listOfClsid(countCoClass), ByVal pAttr, Len(listOfClsid(countCoClass))
  58.                 ret = ITypeInfo_GetDocumentation(typeInf, -1, listOfNames(countCoClass), vbNullString, 0, vbNullString)
  59.                
  60.                 If ret Then
  61.                     ITypeInfo_ReleaseTypeAttr typeInf, pAttr
  62.                     Err.Raise ret
  63.                     Exit Function
  64.                 End If
  65.                
  66.                 countCoClass = countCoClass + 1
  67.                
  68.             End If
  69.            
  70.             ITypeInfo_ReleaseTypeAttr typeInf, pAttr
  71.            
  72.             Set typeInf = Nothing
  73.            
  74.         Next
  75.        
  76.     End If
  77.    
  78.     If countCoClass Then
  79.        
  80.         ReDim Preserve listOfClsid(countCoClass - 1)
  81.         ReDim Preserve listOfNames(countCoClass - 1)
  82.    
  83.     Else
  84.    
  85.         Erase listOfClsid()
  86.         Erase listOfNames()
  87.        
  88.     End If
  89.    
  90.     GetAllCoclasses = True
  91.    
  92. End Function
  93.  
  94. ' // Create IDispach implementation described in type library.
  95. Public Function CreateIDispatch( _
  96.                 ByRef obj As IUnknown, _
  97.                 ByRef typeLibPath As String, _
  98.                 ByRef interfaceName As String) As Object
  99.                
  100.     Dim typeLib As IUnknown
  101.     Dim typeInf As IUnknown
  102.     Dim ret     As Long
  103.     Dim retObj  As IUnknown
  104.     Dim pAttr   As Long
  105.     Dim tKind   As Long
  106.    
  107.     ret = LoadTypeLibEx(StrPtr(typeLibPath), REGKIND_NONE, typeLib)
  108.    
  109.     If ret Then
  110.         Err.Raise ret
  111.         Exit Function
  112.     End If
  113.    
  114.     ret = ITypeLib_FindName(typeLib, interfaceName, 0, typeInf, 0, 1)
  115.    
  116.     If typeInf Is Nothing Then
  117.         Err.Raise &H80004002, , "Interface not found"
  118.         Exit Function
  119.     End If
  120.    
  121.     ITypeInfo_GetTypeAttr typeInf, pAttr
  122.     GetMem4 ByVal pAttr + &H28, tKind
  123.     ITypeInfo_ReleaseTypeAttr typeInf, pAttr
  124.    
  125.     If tKind = TKIND_DISPATCH Then
  126.         Set CreateIDispatch = obj
  127.         Exit Function
  128.     ElseIf tKind <> TKIND_INTERFACE Then
  129.         Err.Raise &H80004002, , "Interface not found"
  130.         Exit Function
  131.     End If
  132.  
  133.     ret = CreateStdDispatch(Nothing, obj, typeInf, retObj)
  134.    
  135.     If ret Then
  136.         Err.Raise ret
  137.         Exit Function
  138.     End If
  139.    
  140.     Set CreateIDispatch = retObj
  141.  
  142. End Function
  143.  
  144. ' // Create object by Name.
  145. Public Function CreateObjectEx2( _
  146.                 ByRef pathToDll As String, _
  147.                 ByRef pathToTLB As String, _
  148.                 ByRef className As String) As IUnknown
  149.                
  150.     Dim typeLib As IUnknown
  151.     Dim typeInf As IUnknown
  152.     Dim ret     As Long
  153.     Dim pAttr   As Long
  154.     Dim tKind   As Long
  155.     Dim clsid   As GUID
  156.    
  157.     ret = LoadTypeLibEx(StrPtr(pathToTLB), REGKIND_NONE, typeLib)
  158.    
  159.     If ret Then
  160.         Err.Raise ret
  161.         Exit Function
  162.     End If
  163.    
  164.     ret = ITypeLib_FindName(typeLib, className, 0, typeInf, 0, 1)
  165.    
  166.     If typeInf Is Nothing Then
  167.         Err.Raise &H80040111, , "Class not found in type library"
  168.         Exit Function
  169.     End If
  170.  
  171.     ITypeInfo_GetTypeAttr typeInf, pAttr
  172.    
  173.     GetMem4 ByVal pAttr + &H28, tKind
  174.    
  175.     If tKind = TKIND_COCLASS Then
  176.         memcpy clsid, ByVal pAttr, Len(clsid)
  177.     Else
  178.         Err.Raise &H80040111, , "Class not found in type library"
  179.         Exit Function
  180.     End If
  181.    
  182.     ITypeInfo_ReleaseTypeAttr typeInf, pAttr
  183.            
  184.     Set CreateObjectEx2 = CreateObjectEx(pathToDll, clsid)
  185.    
  186. End Function
  187.                
  188. ' // Create object by CLSID and path.
  189. Public Function CreateObjectEx( _
  190.                 ByRef path As String, _
  191.                 ByRef clsid As GUID) As IUnknown
  192.                
  193.     Dim hLib    As Long
  194.     Dim lpAddr  As Long
  195.    
  196.     hLib = LoadLibrary(StrPtr(path))
  197.     If hLib = 0 Then
  198.         Err.Raise 53, , Error(53) & " " & Chr$(34) & path & Chr$(34)
  199.         Exit Function
  200.     End If
  201.    
  202.     lpAddr = GetProcAddress(hLib, "DllGetClassObject")
  203.    
  204.     If lpAddr = 0 Then
  205.         Err.Raise 453, , "Can't find dll entry point DllGetClasesObject in " & Chr$(34) & path & Chr$(34)
  206.         Exit Function
  207.     End If
  208.  
  209.     If Not isInit Then
  210.         CLSIDFromString StrPtr(IID_IClassFactory), iidClsFctr
  211.         CLSIDFromString StrPtr(IID_IUnknown), iidUnk
  212.         isInit = True
  213.     End If
  214.    
  215.     Dim ret     As Long
  216.     Dim out     As IUnknown
  217.    
  218.     ret = DllGetClassObject(lpAddr, clsid, iidClsFctr, out)
  219.    
  220.     If ret = 0 Then
  221.  
  222.         ret = IClassFactory_CreateInstance(out, 0, iidUnk, CreateObjectEx)
  223.  
  224.     Else: Err.Raise ret: Exit Function
  225.     End If
  226.    
  227.     Set out = Nothing
  228.    
  229. End Function
  230.  
  231. ' // Unload DLL if not used.
  232. Public Function UnloadLibrary( _
  233.                 ByRef path As String) As Boolean
  234.                
  235.     Dim hLib    As Long
  236.     Dim lpAddr  As Long
  237.     Dim ret     As Long
  238.    
  239.     If Not isInit Then Exit Function
  240.    
  241.     hLib = GetModuleHandle(StrPtr(path))
  242.     If hLib = 0 Then Exit Function
  243.    
  244.     lpAddr = GetProcAddress(hLib, "DllCanUnloadNow")
  245.     If lpAddr = 0 Then Exit Function
  246.    
  247.     ret = DllCanUnloadNow(lpAddr)
  248.    
  249.     If ret = 0 Then
  250.         FreeLibrary hLib
  251.         UnloadLibrary = True
  252.     End If
  253.    
  254. End Function
  255.  
  256. ' // Call "DllGetClassObject" function using a pointer.
  257. Private Function DllGetClassObject( _
  258.                  ByVal funcAddr As Long, _
  259.                  ByRef clsid As GUID, _
  260.                  ByRef iid As GUID, _
  261.                  ByRef out As IUnknown) As Long
  262.                  
  263.     Dim params(2)   As Variant
  264.     Dim types(2)    As Integer
  265.     Dim list(2)     As Long
  266.     Dim resultCall  As Long
  267.     Dim pIndex      As Long
  268.     Dim pReturn     As Variant
  269.    
  270.     params(0) = VarPtr(clsid)
  271.     params(1) = VarPtr(iid)
  272.     params(2) = VarPtr(out)
  273.    
  274.     For pIndex = 0 To UBound(params)
  275.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  276.     Next
  277.    
  278.     resultCall = DispCallFunc(0&, funcAddr, CC_STDCALL, vbLong, 3, types(0), list(0), pReturn)
  279.              
  280.     If resultCall Then Err.Raise 5: Exit Function
  281.    
  282.     DllGetClassObject = pReturn
  283.    
  284. End Function
  285.  
  286. ' // Call "DllCanUnloadNow" function using a pointer.
  287. Private Function DllCanUnloadNow( _
  288.                  ByVal funcAddr As Long) As Long
  289.                  
  290.     Dim resultCall  As Long
  291.     Dim pReturn     As Variant
  292.    
  293.     resultCall = DispCallFunc(0&, funcAddr, CC_STDCALL, vbLong, 0, ByVal 0&, ByVal 0&, pReturn)
  294.              
  295.     If resultCall Then Err.Raise 5: Exit Function
  296.    
  297.     DllCanUnloadNow = pReturn
  298.    
  299. End Function
  300.  
  301. ' // Call "IClassFactory:CreateInstance" method.
  302. Private Function IClassFactory_CreateInstance( _
  303.                  ByVal obj As IUnknown, _
  304.                  ByVal punkOuter As Long, _
  305.                  ByRef riid As GUID, _
  306.                  ByRef out As IUnknown) As Long
  307.    
  308.     Dim params(2)   As Variant
  309.     Dim types(2)    As Integer
  310.     Dim list(2)     As Long
  311.     Dim resultCall  As Long
  312.     Dim pIndex      As Long
  313.     Dim pReturn     As Variant
  314.    
  315.     params(0) = punkOuter
  316.     params(1) = VarPtr(riid)
  317.     params(2) = VarPtr(out)
  318.    
  319.     For pIndex = 0 To UBound(params)
  320.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  321.     Next
  322.    
  323.     resultCall = DispCallFunc(obj, &HC, CC_STDCALL, vbLong, 3, types(0), list(0), pReturn)
  324.          
  325.     If resultCall Then Err.Raise resultCall: Exit Function
  326.      
  327.     IClassFactory_CreateInstance = pReturn
  328.    
  329. End Function
  330.  
  331. ' // Call "ITypeLib:GetTypeInfoCount" method.
  332. Private Function ITypeLib_GetTypeInfoCount( _
  333.                  ByVal obj As IUnknown) As Long
  334.    
  335.     Dim resultCall  As Long
  336.     Dim pReturn     As Variant
  337.  
  338.     resultCall = DispCallFunc(obj, &HC, CC_STDCALL, vbLong, 0, ByVal 0&, ByVal 0&, pReturn)
  339.          
  340.     If resultCall Then Err.Raise resultCall: Exit Function
  341.      
  342.     ITypeLib_GetTypeInfoCount = pReturn
  343.    
  344. End Function
  345.  
  346. ' // Call "ITypeLib:GetTypeInfo" method.
  347. Private Function ITypeLib_GetTypeInfo( _
  348.                  ByVal obj As IUnknown, _
  349.                  ByVal index As Long, _
  350.                  ByRef ppTInfo As IUnknown) As Long
  351.    
  352.     Dim params(1)   As Variant
  353.     Dim types(1)    As Integer
  354.     Dim list(1)     As Long
  355.     Dim resultCall  As Long
  356.     Dim pIndex      As Long
  357.     Dim pReturn     As Variant
  358.    
  359.     params(0) = index
  360.     params(1) = VarPtr(ppTInfo)
  361.    
  362.     For pIndex = 0 To UBound(params)
  363.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  364.     Next
  365.    
  366.     resultCall = DispCallFunc(obj, &H10, CC_STDCALL, vbLong, 2, types(0), list(0), pReturn)
  367.          
  368.     If resultCall Then Err.Raise resultCall: Exit Function
  369.      
  370.     ITypeLib_GetTypeInfo = pReturn
  371.    
  372. End Function
  373.  
  374. ' // Call "ITypeLib:FindName" method.
  375. Private Function ITypeLib_FindName( _
  376.                  ByVal obj As IUnknown, _
  377.                  ByRef szNameBuf As String, _
  378.                  ByVal lHashVal As Long, _
  379.                  ByRef ppTInfo As IUnknown, _
  380.                  ByRef rgMemId As Long, _
  381.                  ByRef pcFound As Integer) As Long
  382.    
  383.     Dim params(4)   As Variant
  384.     Dim types(4)    As Integer
  385.     Dim list(4)     As Long
  386.     Dim resultCall  As Long
  387.     Dim pIndex      As Long
  388.     Dim pReturn     As Variant
  389.    
  390.     params(0) = StrPtr(szNameBuf)
  391.     params(1) = lHashVal
  392.     params(2) = VarPtr(ppTInfo)
  393.     params(3) = VarPtr(rgMemId)
  394.     params(4) = VarPtr(pcFound)
  395.    
  396.     For pIndex = 0 To UBound(params)
  397.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  398.     Next
  399.    
  400.     resultCall = DispCallFunc(obj, &H2C, CC_STDCALL, vbLong, 5, types(0), list(0), pReturn)
  401.          
  402.     If resultCall Then Err.Raise resultCall: Exit Function
  403.      
  404.     ITypeLib_FindName = pReturn
  405.    
  406. End Function
  407.  
  408. ' // Call "ITypeInfo:GetTypeAttr" method.
  409. Private Sub ITypeInfo_GetTypeAttr( _
  410.             ByVal obj As IUnknown, _
  411.             ByRef ppTypeAttr As Long)
  412.    
  413.     Dim resultCall  As Long
  414.     Dim pReturn     As Variant
  415.    
  416.     pReturn = VarPtr(ppTypeAttr)
  417.    
  418.     resultCall = DispCallFunc(obj, &HC, CC_STDCALL, vbEmpty, 1, vbLong, VarPtr(pReturn), 0)
  419.          
  420.     If resultCall Then Err.Raise resultCall: Exit Sub
  421.  
  422. End Sub
  423.  
  424. ' // Call "ITypeInfo:GetDocumentation" method.
  425. Private Function ITypeInfo_GetDocumentation( _
  426.                  ByVal obj As IUnknown, _
  427.                  ByVal memid As Long, _
  428.                  ByRef pBstrName As String, _
  429.                  ByRef pBstrDocString As String, _
  430.                  ByRef pdwHelpContext As Long, _
  431.                  ByRef pBstrHelpFile As String) As Long
  432.    
  433.     Dim params(4)   As Variant
  434.     Dim types(4)    As Integer
  435.     Dim list(4)     As Long
  436.     Dim resultCall  As Long
  437.     Dim pIndex      As Long
  438.     Dim pReturn     As Variant
  439.    
  440.     params(0) = memid
  441.     params(1) = VarPtr(pBstrName)
  442.     params(2) = VarPtr(pBstrDocString)
  443.     params(3) = VarPtr(pdwHelpContext)
  444.     params(4) = VarPtr(pBstrHelpFile)
  445.    
  446.     For pIndex = 0 To UBound(params)
  447.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  448.     Next
  449.    
  450.     resultCall = DispCallFunc(obj, &H30, CC_STDCALL, vbLong, 5, types(0), list(0), pReturn)
  451.          
  452.     If resultCall Then Err.Raise resultCall: Exit Function
  453.      
  454.     ITypeInfo_GetDocumentation = pReturn
  455.    
  456. End Function
  457.  
  458. ' // Call "ITypeInfo:ReleaseTypeAttr" method.
  459. Private Sub ITypeInfo_ReleaseTypeAttr( _
  460.             ByVal obj As IUnknown, _
  461.             ByVal ppTypeAttr As Long)
  462.    
  463.     Dim resultCall  As Long
  464.    
  465.     resultCall = DispCallFunc(obj, &H4C, CC_STDCALL, vbEmpty, 1, vbLong, VarPtr(CVar(ppTypeAttr)), 0)
  466.          
  467.     If resultCall Then Err.Raise resultCall: Exit Sub
  468.  
  469. End Sub
Attached Files

Viewing all articles
Browse latest Browse all 1530

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>