Advertisement
Guest User

CRegComp.cls

a guest
Nov 14th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.  Persistable = 0  'NotPersistable
  5.  DataBindingBehavior = 0  'vbNone
  6.  DataSourceBehavior  = 0  'vbNone
  7.  MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "ICompReg"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. ' ==============================================================
  17. ' CompReg.cls - allows to register or unregister a COM component
  18. ' ==============================================================
  19.  
  20. ' infinite wait
  21. Private Const INFINITE = &HFFFF
  22.  
  23. ' wait status codes
  24. Private Const WAIT_OBJECT_0 = &H0&
  25. Private Const WAIT_ABANDONED = &H80&
  26. Private Const WAIT_TIMEOUT = &H102&
  27. Private Const WAIT_FAILED = &HFFFFFFFF
  28.  
  29. ' results from the reg/unreg operation
  30. Public Enum enRegStatus
  31.     S_OK = &H0&
  32.     S_FALSE = &H1&
  33.     SELFREG_E_TYPELIB = &H80040200
  34.     SELFREG_E_CLASS = &H80040201
  35.     E_OUTOFMEMORY = &H8007000E
  36.     E_UNEXPECTED = &H8000FFFF
  37.     P_NOTFOUND = &H9000F001
  38.     P_NOTCOMP = &H9000F002
  39.     P_CANTEXEC = &H9000F003
  40.     P_EXECERR = &H9000F004
  41. End Enum
  42.  
  43. ' Entry point names
  44. Private Const FN_REG = "DllRegisterServer"
  45. Private Const FN_UNR = "DllUnregisterServer"
  46.  
  47. ' APIs used throughout the code
  48. Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
  49. Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
  50. Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
  51. Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lpParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
  52. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  53. Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
  54. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  55.  
  56. ' private workareas
  57. Private mbRegFlag       As Boolean
  58. Private mnStatusCode    As enRegStatus
  59.  
  60. ' Register/UnRegister a COM component
  61. Public Function RegUnreg(ByVal sCompName As String, ByVal bRegister As Boolean) As enRegStatus
  62.     Dim hLibrary As Long, lpProcAddr As Long, hThread As Long, lThreadID As Long
  63.     Dim lResult As Long, lTemp As Long, sFunc As String
  64.    
  65.     ' initialize return code and try to
  66.    ' load the specified component file
  67.    lResult = P_NOTFOUND
  68.     hLibrary = LoadLibrary(sCompName)
  69.     If (hLibrary <> 0) Then
  70.         ' now let's try to find the register
  71.        ' on unregister function entry point
  72.        lResult = P_NOTCOMP
  73.         If (bRegister) Then
  74.             sFunc = FN_REG
  75.         Else
  76.             sFunc = FN_UNR
  77.         End If
  78.         lpProcAddr = GetProcAddress(hLibrary, FN_UNR)
  79.         If (lpProcAddr <> 0) Then
  80.             ' we got the function entry point, now since VB
  81.            ' doesn't know what to do with a function ptr we
  82.            ' will use the createthread function to call the
  83.            ' component function by using its pointer
  84.            lResult = P_CANTEXEC
  85.             hThread = CreateThread(0&, 0&, lpProcAddr, 0&, 0&, lThreadID)
  86.             If (hThread <> 0) Then
  87.                 ' let's wait for the thread to terminate and
  88.                ' get its return code to check what happened
  89.                lResult = P_EXECERR
  90.                 lTemp = WaitForSingleObject(hThread, INFINITE)
  91.                 If lTemp = WAIT_OBJECT_0 Then
  92.                     ' should be S_OK from the reg/unreg if all ok
  93.                    lTemp = GetExitCodeThread(hThread, lResult)
  94.                 End If
  95.                 ' done with thread, close the thread handle
  96.                Call CloseHandle(hThread)
  97.             End If
  98.         End If
  99.         ' release/unload the component
  100.        Call FreeLibrary(hLibrary)
  101.     End If
  102.    
  103.     ' store the reg/unreg flag and the result code
  104.    mbRegFlag = bRegister
  105.     mnStatusCode = lResult
  106.    
  107.     ' all done, return the result code (0=all ok)
  108.    RegUnreg = lResult
  109. End Function
  110.  
  111. ' reg/unreg status (exit) code
  112. Public Property Get StatusCode() As enRegStatus
  113.     StatusCode = mnStatusCode
  114. End Property
  115.  
  116. ' reg/unreg status msg, the messages have been
  117. ' taken out from various sources (msdn etc...)
  118. Public Property Get StatusMsg() As String
  119.     Dim sMsg As String
  120.  
  121.     ' initialize and decode the status code to a message
  122.    sMsg = ""
  123.     Select Case mnStatusCode
  124.         Case S_OK
  125.             ' all ok
  126.            If mbRegFlag = True Then
  127.                 sMsg = "The registry entries were created successfully."
  128.             Else
  129.                 sMsg = "The registry entries were removed successfully."
  130.             End If
  131.         Case S_FALSE
  132.             ' ok, but with some side issues
  133.            If mbRegFlag = False Then
  134.                 sMsg = "Unregistration of this server’s known entries was successful, but other entries still exist for this server’s classes."
  135.             End If
  136.         Case SELFREG_E_TYPELIB
  137.             ' problems with "tlb"
  138.            If mbRegFlag = True Then
  139.                 sMsg = "The server was unable to complete the registration of all the type libraries used by its classes."
  140.             Else
  141.                 sMsg = "The server was unable to remove the entries of all the type libraries used by its classes."
  142.             End If
  143.         Case SELFREG_E_CLASS
  144.             ' problems with classes
  145.            If mbRegFlag = True Then
  146.                 sMsg = "The server was unable to complete the registration of all the object classes."
  147.             Else
  148.                 sMsg = "The server was unable to remove the entries of all the object classes."
  149.             End If
  150.         Case E_OUTOFMEMORY
  151.             ' need more memory
  152.            sMsg = "The server ran out of memory while processing the request."
  153.         Case E_UNEXPECTED
  154.             ' whoops this shouldn't happen
  155.            sMsg = "An unexpected internal error occurred."
  156.         Case P_NOTFOUND
  157.             ' the file to (un)register wasn't found
  158.            sMsg = "Component not found."
  159.         Case P_NOTCOMP
  160.             ' no reg/unreg entry point
  161.            sMsg = "The specified file is not a component."
  162.         Case P_CANTEXEC
  163.             ' error calling the entry point
  164.            sMsg = "Can't call the requested registration function."
  165.         Case P_EXECERR
  166.             ' the entry point call terminated abnormally
  167.            sMsg = "The requested function terminated abnormally."
  168.         Case Else
  169.             ' all other error codes
  170.            sMsg = ""
  171.     End Select
  172.    
  173.     ' complete and return the message
  174.    If Len(sMsg) < 1 Then
  175.         sMsg = "Unknown return code"
  176.     End If
  177.     StatusMsg = "0x" & Right("00000000" & Hex(mnStatusCode), 8) & " " & sMsg
  178. End Property
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement