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

[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 = GetModuleHandle(StrPtr(path))
  197.    
  198.     If hLib = 0 Then
  199.    
  200.         hLib = LoadLibrary(StrPtr(path))
  201.         If hLib = 0 Then
  202.             Err.Raise 53, , Error(53) & " " & Chr$(34) & path & Chr$(34)
  203.             Exit Function
  204.         End If
  205.        
  206.     End If
  207.    
  208.     lpAddr = GetProcAddress(hLib, "DllGetClassObject")
  209.    
  210.     If lpAddr = 0 Then
  211.         Err.Raise 453, , "Can't find dll entry point DllGetClasesObject in " & Chr$(34) & path & Chr$(34)
  212.         Exit Function
  213.     End If
  214.  
  215.     If Not isInit Then
  216.         CLSIDFromString StrPtr(IID_IClassFactory), iidClsFctr
  217.         CLSIDFromString StrPtr(IID_IUnknown), iidUnk
  218.         isInit = True
  219.     End If
  220.    
  221.     Dim ret     As Long
  222.     Dim out     As IUnknown
  223.    
  224.     ret = DllGetClassObject(lpAddr, clsid, iidClsFctr, out)
  225.    
  226.     If ret = 0 Then
  227.  
  228.         ret = IClassFactory_CreateInstance(out, 0, iidUnk, CreateObjectEx)
  229.  
  230.     Else: Err.Raise ret: Exit Function
  231.     End If
  232.    
  233.     Set out = Nothing
  234.    
  235. End Function
  236.  
  237. ' // Unload DLL if not used.
  238. Public Function UnloadLibrary( _
  239.                 ByRef path As String) As Boolean
  240.                
  241.     Dim hLib    As Long
  242.     Dim lpAddr  As Long
  243.     Dim ret     As Long
  244.    
  245.     If Not isInit Then Exit Function
  246.    
  247.     hLib = GetModuleHandle(StrPtr(path))
  248.     If hLib = 0 Then Exit Function
  249.    
  250.     lpAddr = GetProcAddress(hLib, "DllCanUnloadNow")
  251.     If lpAddr = 0 Then Exit Function
  252.    
  253.     ret = DllCanUnloadNow(lpAddr)
  254.    
  255.     If ret = 0 Then
  256.         FreeLibrary hLib
  257.         UnloadLibrary = True
  258.     End If
  259.    
  260. End Function
  261.  
  262. ' // Call "DllGetClassObject" function using a pointer.
  263. Private Function DllGetClassObject( _
  264.                  ByVal funcAddr As Long, _
  265.                  ByRef clsid As GUID, _
  266.                  ByRef iid As GUID, _
  267.                  ByRef out As IUnknown) As Long
  268.                  
  269.     Dim params(2)   As Variant
  270.     Dim types(2)    As Integer
  271.     Dim list(2)     As Long
  272.     Dim resultCall  As Long
  273.     Dim pIndex      As Long
  274.     Dim pReturn     As Variant
  275.    
  276.     params(0) = VarPtr(clsid)
  277.     params(1) = VarPtr(iid)
  278.     params(2) = VarPtr(out)
  279.    
  280.     For pIndex = 0 To UBound(params)
  281.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  282.     Next
  283.    
  284.     resultCall = DispCallFunc(0&, funcAddr, CC_STDCALL, vbLong, 3, types(0), list(0), pReturn)
  285.              
  286.     If resultCall Then Err.Raise 5: Exit Function
  287.    
  288.     DllGetClassObject = pReturn
  289.    
  290. End Function
  291.  
  292. ' // Call "DllCanUnloadNow" function using a pointer.
  293. Private Function DllCanUnloadNow( _
  294.                  ByVal funcAddr As Long) As Long
  295.                  
  296.     Dim resultCall  As Long
  297.     Dim pReturn     As Variant
  298.    
  299.     resultCall = DispCallFunc(0&, funcAddr, CC_STDCALL, vbLong, 0, ByVal 0&, ByVal 0&, pReturn)
  300.              
  301.     If resultCall Then Err.Raise 5: Exit Function
  302.    
  303.     DllCanUnloadNow = pReturn
  304.    
  305. End Function
  306.  
  307. ' // Call "IClassFactory:CreateInstance" method.
  308. Private Function IClassFactory_CreateInstance( _
  309.                  ByVal obj As IUnknown, _
  310.                  ByVal punkOuter As Long, _
  311.                  ByRef riid As GUID, _
  312.                  ByRef out As IUnknown) As Long
  313.    
  314.     Dim params(2)   As Variant
  315.     Dim types(2)    As Integer
  316.     Dim list(2)     As Long
  317.     Dim resultCall  As Long
  318.     Dim pIndex      As Long
  319.     Dim pReturn     As Variant
  320.    
  321.     params(0) = punkOuter
  322.     params(1) = VarPtr(riid)
  323.     params(2) = VarPtr(out)
  324.    
  325.     For pIndex = 0 To UBound(params)
  326.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  327.     Next
  328.    
  329.     resultCall = DispCallFunc(obj, &HC, CC_STDCALL, vbLong, 3, types(0), list(0), pReturn)
  330.          
  331.     If resultCall Then Err.Raise resultCall: Exit Function
  332.      
  333.     IClassFactory_CreateInstance = pReturn
  334.    
  335. End Function
  336.  
  337. ' // Call "ITypeLib:GetTypeInfoCount" method.
  338. Private Function ITypeLib_GetTypeInfoCount( _
  339.                  ByVal obj As IUnknown) As Long
  340.    
  341.     Dim resultCall  As Long
  342.     Dim pReturn     As Variant
  343.  
  344.     resultCall = DispCallFunc(obj, &HC, CC_STDCALL, vbLong, 0, ByVal 0&, ByVal 0&, pReturn)
  345.          
  346.     If resultCall Then Err.Raise resultCall: Exit Function
  347.      
  348.     ITypeLib_GetTypeInfoCount = pReturn
  349.    
  350. End Function
  351.  
  352. ' // Call "ITypeLib:GetTypeInfo" method.
  353. Private Function ITypeLib_GetTypeInfo( _
  354.                  ByVal obj As IUnknown, _
  355.                  ByVal index As Long, _
  356.                  ByRef ppTInfo As IUnknown) As Long
  357.    
  358.     Dim params(1)   As Variant
  359.     Dim types(1)    As Integer
  360.     Dim list(1)     As Long
  361.     Dim resultCall  As Long
  362.     Dim pIndex      As Long
  363.     Dim pReturn     As Variant
  364.    
  365.     params(0) = index
  366.     params(1) = VarPtr(ppTInfo)
  367.    
  368.     For pIndex = 0 To UBound(params)
  369.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  370.     Next
  371.    
  372.     resultCall = DispCallFunc(obj, &H10, CC_STDCALL, vbLong, 2, types(0), list(0), pReturn)
  373.          
  374.     If resultCall Then Err.Raise resultCall: Exit Function
  375.      
  376.     ITypeLib_GetTypeInfo = pReturn
  377.    
  378. End Function
  379.  
  380. ' // Call "ITypeLib:FindName" method.
  381. Private Function ITypeLib_FindName( _
  382.                  ByVal obj As IUnknown, _
  383.                  ByRef szNameBuf As String, _
  384.                  ByVal lHashVal As Long, _
  385.                  ByRef ppTInfo As IUnknown, _
  386.                  ByRef rgMemId As Long, _
  387.                  ByRef pcFound As Integer) As Long
  388.    
  389.     Dim params(4)   As Variant
  390.     Dim types(4)    As Integer
  391.     Dim list(4)     As Long
  392.     Dim resultCall  As Long
  393.     Dim pIndex      As Long
  394.     Dim pReturn     As Variant
  395.    
  396.     params(0) = StrPtr(szNameBuf)
  397.     params(1) = lHashVal
  398.     params(2) = VarPtr(ppTInfo)
  399.     params(3) = VarPtr(rgMemId)
  400.     params(4) = VarPtr(pcFound)
  401.    
  402.     For pIndex = 0 To UBound(params)
  403.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  404.     Next
  405.    
  406.     resultCall = DispCallFunc(obj, &H2C, CC_STDCALL, vbLong, 5, types(0), list(0), pReturn)
  407.          
  408.     If resultCall Then Err.Raise resultCall: Exit Function
  409.      
  410.     ITypeLib_FindName = pReturn
  411.    
  412. End Function
  413.  
  414. ' // Call "ITypeInfo:GetTypeAttr" method.
  415. Private Sub ITypeInfo_GetTypeAttr( _
  416.             ByVal obj As IUnknown, _
  417.             ByRef ppTypeAttr As Long)
  418.    
  419.     Dim resultCall  As Long
  420.     Dim pReturn     As Variant
  421.    
  422.     pReturn = VarPtr(ppTypeAttr)
  423.    
  424.     resultCall = DispCallFunc(obj, &HC, CC_STDCALL, vbEmpty, 1, vbLong, VarPtr(pReturn), 0)
  425.          
  426.     If resultCall Then Err.Raise resultCall: Exit Sub
  427.  
  428. End Sub
  429.  
  430. ' // Call "ITypeInfo:GetDocumentation" method.
  431. Private Function ITypeInfo_GetDocumentation( _
  432.                  ByVal obj As IUnknown, _
  433.                  ByVal memid As Long, _
  434.                  ByRef pBstrName As String, _
  435.                  ByRef pBstrDocString As String, _
  436.                  ByRef pdwHelpContext As Long, _
  437.                  ByRef pBstrHelpFile As String) As Long
  438.    
  439.     Dim params(4)   As Variant
  440.     Dim types(4)    As Integer
  441.     Dim list(4)     As Long
  442.     Dim resultCall  As Long
  443.     Dim pIndex      As Long
  444.     Dim pReturn     As Variant
  445.    
  446.     params(0) = memid
  447.     params(1) = VarPtr(pBstrName)
  448.     params(2) = VarPtr(pBstrDocString)
  449.     params(3) = VarPtr(pdwHelpContext)
  450.     params(4) = VarPtr(pBstrHelpFile)
  451.    
  452.     For pIndex = 0 To UBound(params)
  453.         list(pIndex) = VarPtr(params(pIndex)):   types(pIndex) = VarType(params(pIndex))
  454.     Next
  455.    
  456.     resultCall = DispCallFunc(obj, &H30, CC_STDCALL, vbLong, 5, types(0), list(0), pReturn)
  457.          
  458.     If resultCall Then Err.Raise resultCall: Exit Function
  459.      
  460.     ITypeInfo_GetDocumentation = pReturn
  461.    
  462. End Function
  463.  
  464. ' // Call "ITypeInfo:ReleaseTypeAttr" method.
  465. Private Sub ITypeInfo_ReleaseTypeAttr( _
  466.             ByVal obj As IUnknown, _
  467.             ByVal ppTypeAttr As Long)
  468.    
  469.     Dim resultCall  As Long
  470.    
  471.     resultCall = DispCallFunc(obj, &H4C, CC_STDCALL, vbEmpty, 1, vbLong, VarPtr(CVar(ppTypeAttr)), 0)
  472.          
  473.     If resultCall Then Err.Raise resultCall: Exit Sub
  474.  
  475. End Sub

Download.

Viewing all articles
Browse latest Browse all 1448

Trending Articles



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