home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / Epsita_Ant21314110212008.psc / CRegistry.cls < prev    next >
Text File  |  2005-04-22  |  26KB  |  640 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = 0   'False
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "CRegistry"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. '* Description : Class for working with the system registry.
  15.  
  16. Option Explicit
  17.  
  18. ' Error handling definitions
  19. Private Const E_ERR_BASE = 18000 + vbObjectError
  20. Public Enum EErrRegistry
  21.     eErrRegistry_InvalidKeyName = E_ERR_BASE + 1
  22.     eErrRegistry_InvalidValueName
  23.     eErrRegistry_ComponentFailure
  24. End Enum
  25. Private Const S_ERR_InvalidKeyName = "Invalid KeyName value"
  26. Private Const S_ERR_InvalidValueName = "Invalid value name"
  27. Private Const S_ERR_ComponentFailure = "CRegistry component failure"
  28.  
  29. ' Public class enums
  30. Public Enum ERegRoot
  31.     eRegRoot_HKeyClassesRoot = &H80000000
  32.     eRegRoot_HKeyCurrentUser = &H80000001
  33.     eRegRoot_HKeyLocalMachine = &H80000002
  34.     eRegRoot_HKeyUsers = &H80000003
  35.     eRegRoot_HKeyCurrentConfig = &H80000005
  36.     eRegRoot_HKeyDynData = &H80000006
  37. End Enum
  38.  
  39. Public Enum ERegValue
  40.     eRegValue_None = 0
  41.     eRegValue_Sz = 1
  42.     eRegValue_ExpandSz = 2
  43.     eRegValue_Binary = 3
  44.     eRegValue_DWord = 4
  45.     eRegValue_DWordLittleEndian = 4
  46.     eRegValue_DWordBigEndian = 5
  47.     eRegValue_Link = 6
  48.     eRegValue_MultiSz = 7
  49.     eRegValue_ResourceList = 8
  50.     eRegValue_FullResourceDescriptor = 9
  51.     eRegValue_ResourceRequirementsList = 10
  52. End Enum
  53.  
  54. ' Private class constants
  55. Private Const KEY_QUERY_VALUE = &H1
  56. Private Const KEY_SET_VALUE = &H2
  57. Private Const KEY_CREATE_SUB_KEY = &H4
  58. Private Const KEY_ENUMERATE_SUB_KEYS = &H8
  59. Private Const KEY_NOTIFY = &H10
  60. Private Const KEY_CREATE_LINK = &H20
  61. Private Const KEY_ALL_ACCESS = &H3F
  62. Private Const REG_OPTION_NON_VOLATILE = 0&
  63. Private Const REG_OPTION_VOLATILE = &H1
  64. Private Const REG_CREATED_NEW_KEY = &H1
  65. Private Const REG_OPENED_EXISTING_KEY = &H2
  66. Private Const STANDARD_RIGHTS_ALL = &H1F0000
  67. Private Const SPECIFIC_RIGHTS_ALL = &HFFFF
  68. Private Const ERROR_SUCCESS = 0&
  69. Private Const ERROR_ACCESS_DENIED = 5
  70. Private Const ERROR_INVALID_DATA = 13&
  71. Private Const ERROR_MORE_DATA = 234
  72. Private Const ERROR_NO_MORE_ITEMS = 259
  73. Private Const MAX_PATH = 256
  74.  
  75. ' Private class type definitions
  76. Private Type SECURITY_ATTRIBUTES
  77.     nLength                  As Long
  78.     lpSecurityDescriptor     As Long
  79.     bInheritHandle           As Boolean
  80. End Type
  81. Private Type FILETIME
  82.     dwLowDateTime    As Long
  83.     dwHighDateTime   As Long
  84. End Type
  85.  
  86. ' Private class API function declarations
  87. Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
  88. Private Declare Function RegSetValueExStr Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal szData As String, ByVal cbData As Long) As Long
  89. Private Declare Function RegSetValueExLong Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, szData As Long, ByVal cbData As Long) As Long
  90. Private Declare Function RegSetValueExByte Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, szData As Byte, ByVal cbData As Long) As Long
  91. Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
  92. Private Declare Function RegQueryValueExStr Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal szData As String, ByRef lpcbData As Long) As Long
  93. Private Declare Function RegQueryValueExLong Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, szData As Long, ByRef lpcbData As Long) As Long
  94. Private Declare Function RegQueryValueExByte Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, szData As Byte, ByRef lpcbData As Long) As Long
  95. Private Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
  96. Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
  97. Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
  98. Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, ByVal lpType As Long, ByVal lpData As Long, ByVal lpcbData As Long) As Long
  99. Private Declare Function RegEnumValueLong Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
  100. Private Declare Function RegEnumValueStr Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
  101. Private Declare Function RegEnumValueByte Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
  102. Private Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, ByVal lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, lpftLastWriteTime As Any) As Long
  103. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  104. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
  105. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
  106. Private Declare Function ExpandEnvironmentStrings Lib "kernel32" Alias "ExpandEnvironmentStringsA" (ByVal lpSrc As String, ByVal lpDst As String, ByVal nSize As Long) As Long
  107.  
  108. ' Private variables to hold property values
  109. Private m_RootKey As ERegRoot
  110.  
  111.  
  112. '*****************************************************************************************
  113. '* Function    : Init
  114. '* Notes       : Use this routine for basic object initialization.
  115. '*****************************************************************************************
  116. Public Function Init(RootKey As ERegRoot)
  117.     On Error GoTo hComponentFailure
  118.     
  119.     m_RootKey = RootKey
  120.  
  121.     Exit Function
  122.  
  123. hComponentFailure:
  124.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  125. End Function
  126.  
  127.  
  128. '*****************************************************************************************
  129. '* Property    : RootKey
  130. '* Notes       : Returns or sets the key that will be used as root key.
  131. '*****************************************************************************************
  132. Public Property Get RootKey() As ERegRoot
  133.     On Error GoTo hComponentFailure
  134.     
  135.     RootKey = m_RootKey
  136.  
  137.     Exit Property
  138.  
  139. hComponentFailure:
  140.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  141. End Property
  142.  
  143. Public Property Let RootKey(ByVal eKey As ERegRoot)
  144.     On Error GoTo hComponentFailure
  145.     
  146.     m_RootKey = eKey
  147.  
  148.     Exit Property
  149.  
  150. hComponentFailure:
  151.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  152. End Property
  153.  
  154.  
  155. '*****************************************************************************************
  156. '* Function    : CreateKey
  157. '* Notes       : Creates the specified registry key.
  158. '*               Returns true if the key was created, false otherwise.
  159. '*****************************************************************************************
  160. Public Function CreateKey(KeyName As String) As Boolean
  161.     On Error GoTo hComponentFailure
  162.     
  163.     Dim tSA      As SECURITY_ATTRIBUTES
  164.     Dim hKey     As Long
  165.     Dim lCreate  As Long
  166.     Dim lRet     As Long
  167.     
  168.     If Len(KeyName) = 0 Then
  169.         On Error GoTo 0
  170.         Err.Raise eErrRegistry_InvalidKeyName, App.EXEName & ".CRegistry", S_ERR_InvalidKeyName
  171.     End If
  172.  
  173.     lRet = RegCreateKeyEx(m_RootKey, KeyName, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, tSA, hKey, lCreate)
  174.     If lRet = ERROR_SUCCESS Then RegCloseKey hKey
  175.     
  176.     CreateKey = (lRet = ERROR_SUCCESS)
  177.  
  178.     Exit Function
  179.  
  180. hComponentFailure:
  181.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  182. End Function
  183.  
  184.  
  185. '*****************************************************************************************
  186. '* Function    : DeleteKey
  187. '* Notes       : Deletes  the specified registry key.
  188. '*               Returns true if the key was deleted, false otherwise.
  189. '*****************************************************************************************
  190. Public Function DeleteKey(KeyName As String) As Boolean
  191.     On Error GoTo hComponentFailure
  192.     
  193.     Dim lRet As Long
  194.     
  195.     If Len(KeyName) = 0 Then
  196.         On Error GoTo 0
  197.         Err.Raise eErrRegistry_InvalidKeyName, App.EXEName & ".CRegistry", S_ERR_InvalidKeyName
  198.     End If
  199.     
  200.     lRet = RegDeleteKey(m_RootKey, KeyName)
  201.     DeleteKey = (lRet = ERROR_SUCCESS)
  202.  
  203.     Exit Function
  204.  
  205. hComponentFailure:
  206.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  207. End Function
  208.  
  209.  
  210. '*****************************************************************************************
  211. '* Function    : DeleteValue
  212. '* Notes       : Removes a named value from the specified registry key.
  213. '*               Returns true if the key was deleted, false otherwise.
  214. '*****************************************************************************************
  215. Public Function DeleteValue(ByVal KeyName As String, ByVal ValueName As String) As Boolean
  216.     On Error GoTo hComponentFailure
  217.     
  218.     Dim lRet As Long
  219.     Dim hKey As Long
  220.     
  221.     If Len(KeyName) = 0 Then
  222.         On Error GoTo 0
  223.         Err.Raise eErrRegistry_InvalidKeyName, App.EXEName & ".CRegistry", S_ERR_InvalidKeyName
  224.     End If
  225.     
  226.     DeleteValue = False
  227.  
  228.     lRet = RegOpenKeyEx(m_RootKey, KeyName, 0, KEY_ALL_ACCESS, hKey)
  229.     
  230.     If lRet = ERROR_SUCCESS Then
  231.         lRet = RegDeleteValue(hKey, ValueName)
  232.         DeleteValue = (lRet = ERROR_SUCCESS)
  233.     End If
  234.  
  235.     Exit Function
  236.  
  237. hComponentFailure:
  238.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  239. End Function
  240.  
  241.  
  242. '*****************************************************************************************
  243. '* Function    : GetAllSubKeys
  244. '* Notes       : Retrieves all the subkeys belonging to a registry key.
  245. '*               Returns a long integer value containing the number of retrieved subkeys.
  246. '*****************************************************************************************
  247. Public Function GetAllSubKeys(ByVal KeyName As String, ByRef SubKeys() As String) As Long
  248.     On Error GoTo hComponentFailure
  249.     
  250.     Dim Count        As Long
  251.     Dim dwReserved   As Long
  252.     Dim hKey         As Long
  253.     Dim iPos         As Long
  254.     Dim lenBuffer    As Long
  255.     Dim lIndex       As Long
  256.     Dim lRet         As Long
  257.     Dim lType        As Long
  258.     Dim sCompKey     As String
  259.     Dim szBuffer     As String
  260.         
  261.     Erase SubKeys
  262.     
  263.     Count = 0
  264.     lIndex = 0
  265.  
  266.     lRet = RegOpenKeyEx(m_RootKey, KeyName, 0, KEY_ENUMERATE_SUB_KEYS, hKey)
  267.     
  268.     Do While lRet = ERROR_SUCCESS
  269.         
  270.         szBuffer = String$(MAX_PATH, 0)
  271.         lenBuffer = Len(szBuffer)
  272.         
  273.         lRet = RegEnumKey(hKey, lIndex, szBuffer, lenBuffer)
  274.         
  275.         If (lRet = ERROR_SUCCESS) Then
  276.             
  277.             Count = Count + 1
  278.             
  279.             ReDim Preserve SubKeys(1 To Count) As String
  280.             iPos = InStr(szBuffer, Chr$(0))
  281.             
  282.             If (iPos > 0) Then
  283.                 SubKeys(Count) = Left$(szBuffer, iPos - 1)
  284.             Else
  285.                 SubKeys(Count) = Left$(szBuffer, lenBuffer)
  286.             End If
  287.         
  288.         End If
  289.         
  290.         lIndex = lIndex + 1
  291.     
  292.     Loop
  293.     
  294.     If (hKey <> 0) Then RegCloseKey hKey
  295.     GetAllSubKeys = Count
  296.  
  297.     Exit Function
  298.  
  299. hComponentFailure:
  300.     If (hKey <> 0) Then RegCloseKey hKey
  301.     GetAllSubKeys = 0
  302.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  303. End Function
  304.  
  305.  
  306. '*****************************************************************************************
  307. '* Function    : GetAllValueNames
  308. '* Notes       : Retrieves all the  value names belonging to a registry key.
  309. '*               Returns a long integer containing the number of retrieved names.
  310. '*****************************************************************************************
  311. Public Function GetAllValueNames(ByVal KeyName As String, ByRef ValueNames() As String) As Long
  312.     On Error GoTo hComponentFailure
  313.     
  314.     Dim ft           As Currency
  315.     Dim cJunk        As Long
  316.     Dim cNameMax     As Long
  317.     Dim Count        As Long
  318.     Dim hKey         As Long
  319.     Dim lIndex       As Long
  320.     Dim lNameSize    As Long
  321.     Dim lRet         As Long
  322.     Dim sData        As String
  323.     Dim sName        As String
  324.     
  325.     Count = 0
  326.     Erase ValueNames()
  327.     
  328.     lIndex = 0
  329.     lRet = RegOpenKeyEx(m_RootKey, KeyName, 0, KEY_QUERY_VALUE, hKey)
  330.     
  331.     If (lRet = ERROR_SUCCESS) Then
  332.         
  333.         lRet = RegQueryInfoKey(hKey, "", cJunk, 0, cJunk, cJunk, cJunk, cJunk, cNameMax, cJunk, cJunk, ft)
  334.         
  335.         Do While lRet = ERROR_SUCCESS
  336.             
  337.             lNameSize = cNameMax + 1
  338.             sName = String$(lNameSize, 0)
  339.             
  340.             If (lNameSize = 0) Then lNameSize = 1
  341.             lRet = RegEnumValue(hKey, lIndex, sName, lNameSize, 0&, 0&, 0&, 0&)
  342.             
  343.             If (lRet = ERROR_SUCCESS) Then
  344.                 sName = Left$(sName, lNameSize)
  345.                 Count = Count + 1
  346.                 ReDim Preserve ValueNames(1 To Count) As String
  347.                 ValueNames(Count) = sName
  348.             End If
  349.             
  350.             lIndex = lIndex + 1
  351.         
  352.         Loop
  353.     
  354.     End If
  355.     
  356.     If (hKey <> 0) Then RegCloseKey hKey
  357.     GetAllValueNames = Count
  358.  
  359.     Exit Function
  360.  
  361. hComponentFailure:
  362.     If (hKey <> 0) Then RegCloseKey hKey
  363.     GetAllValueNames = False
  364.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  365. End Function
  366.  
  367.  
  368. '*****************************************************************************************
  369. '* Function    : GetValue
  370. '* Notes       : Returns the value for the specified value name stored in a registry key.
  371. '*               If there is no value stored in that key the Default value is returned.
  372. '*****************************************************************************************
  373. Public Function GetValue(ByVal KeyName As String, ByVal ValueName As String, ByVal Default As Variant) As Variant
  374.     On Error GoTo hComponentFailure
  375.     
  376.     Dim abData()     As Byte
  377.     Dim cData        As Long
  378.     Dim dwData       As Long
  379.     Dim hKey         As Long
  380.     Dim lData        As Long
  381.     Dim lRet         As Long
  382.     Dim ordType      As Long
  383.     Dim sData        As String
  384.     Dim vValue       As Variant
  385.     
  386.     vValue = Default
  387.  
  388.     lRet = RegOpenKeyEx(m_RootKey, KeyName, 0, KEY_QUERY_VALUE, hKey)
  389.     lRet = RegQueryValueExLong(hKey, ValueName, 0&, ordType, 0&, cData)
  390.     
  391.     If lRet And lRet <> ERROR_MORE_DATA Then
  392.         GetValue = vValue
  393.         Exit Function
  394.     End If
  395.     
  396.     Select Case ordType
  397.         
  398.         Case eRegValue_DWord, eRegValue_DWordLittleEndian
  399.             lRet = RegQueryValueExLong(hKey, ValueName, 0&, ordType, lData, cData)
  400.             vValue = CLng(lData)
  401.         
  402.         Case eRegValue_DWordBigEndian
  403.             lRet = RegQueryValueExLong(hKey, ValueName, 0&, ordType, dwData, cData)
  404.             vValue = SwapEndian(dwData)
  405.         
  406.         Case eRegValue_Sz, eRegValue_MultiSz
  407.             sData = String$(cData - 1, 0)
  408.             lRet = RegQueryValueExStr(hKey, ValueName, 0&, ordType, sData, cData)
  409.             vValue = sData
  410.         
  411.         Case eRegValue_ExpandSz
  412.             sData = String$(cData - 1, 0)
  413.             lRet = RegQueryValueExStr(hKey, ValueName, 0&, ordType, sData, cData)
  414.             vValue = ExpandEnvStr(sData)
  415.         
  416.         Case Else
  417.             ReDim abData(cData)
  418.             lRet = RegQueryValueExByte(hKey, ValueName, 0&, ordType, abData(0), cData)
  419.             vValue = abData
  420.     
  421.     End Select
  422.     
  423.     GetValue = vValue
  424.  
  425.     Exit Function
  426.  
  427. hComponentFailure:
  428.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  429. End Function
  430.  
  431.  
  432. '*****************************************************************************************
  433. '* Function    : KeyExists
  434. '* Notes       : Returns true if the specified registry key exists, false otherwise.
  435. '*****************************************************************************************
  436. Public Function KeyExists(KeyName As String) As Boolean
  437.     On Error GoTo hComponentFailure
  438.     
  439.     ' Returns:  true if the key exists.
  440.     Dim hKey As Long
  441.     
  442.     If Len(KeyName) = 0 Then
  443.         On Error GoTo 0
  444.         Err.Raise eErrRegistry_InvalidKeyName, App.EXEName & ".CRegistry", S_ERR_InvalidKeyName
  445.     End If
  446.     
  447.     If RegOpenKeyEx(m_RootKey, KeyName, 0, 1, hKey) = ERROR_SUCCESS Then
  448.         KeyExists = True
  449.         RegCloseKey hKey
  450.     Else
  451.         KeyExists = False
  452.     End If
  453.  
  454.     Exit Function
  455.  
  456. hComponentFailure:
  457.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  458. End Function
  459.  
  460.  
  461. '*****************************************************************************************
  462. '* Function    : SetValue
  463. '* Notes       : Sets the value for the specified value name stored in a registry key.
  464. '*               Returns true if the value is set, false otherwise.
  465. '*****************************************************************************************
  466. Public Function SetValue(ByVal KeyName As String, ByVal ValueName As String, ByVal Value As Variant, Optional ValueType As ERegValue = eRegValue_Sz) As Boolean
  467.     On Error GoTo hComponentFailure
  468.     
  469.     Dim ab()         As Byte
  470.     Dim C            As Long
  471.     Dim i            As Long
  472.     Dim iPos         As Long
  473.     Dim hKey         As Long
  474.     Dim lCreate      As Long
  475.     Dim lRet         As Long
  476.     Dim ordType      As Long
  477.     Dim tSA          As SECURITY_ATTRIBUTES
  478.     Dim s            As String
  479.     
  480.     If Len(KeyName) = 0 Then
  481.         On Error GoTo 0
  482.         Err.Raise eErrRegistry_InvalidKeyName, App.EXEName & ".CRegistry", S_ERR_InvalidKeyName
  483.     End If
  484.     
  485.     SetValue = False
  486.  
  487.     lRet = RegCreateKeyEx(m_RootKey, KeyName, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, tSA, hKey, lCreate)
  488.     
  489.     If lRet = ERROR_SUCCESS Then
  490.         
  491.         Select Case ValueType
  492.             
  493.             Case eRegValue_Binary
  494.                 If (VarType(Value) = vbArray + vbByte) Then
  495.                     ab = Value
  496.                     ordType = eRegValue_Binary
  497.                     C = UBound(ab) - LBound(ab) - 1
  498.                     
  499.                     lRet = RegSetValueExByte(hKey, ValueName, 0&, ordType, ab(0), C)
  500.                 End If
  501.             
  502.             Case eRegValue_DWord, eRegValue_DWordBigEndian, eRegValue_DWordLittleEndian
  503.                 If (VarType(Value) = vbInteger) Or (VarType(Value) = vbLong) Then
  504.                     i = Value
  505.                     ordType = eRegValue_DWord
  506.                     
  507.                     lRet = RegSetValueExLong(hKey, ValueName, 0&, ordType, i, 4)
  508.                 End If
  509.             
  510.             Case eRegValue_Sz, eRegValue_ExpandSz
  511.                 s = Value
  512.                 ordType = eRegValue_Sz
  513.                 iPos = InStr(s, "%")
  514.                 
  515.                 If iPos Then
  516.                     If InStr(iPos + 2, s, "%") Then ordType = eRegValue_ExpandSz
  517.                 End If
  518.                 
  519.                 If Len(s) > 0 Then
  520.                     C = Len(s) + 1
  521.                 Else
  522.                     s = vbNullChar
  523.                     C = Len(s)
  524.                 End If
  525.                 
  526.                 lRet = RegSetValueExStr(hKey, ValueName, 0&, ordType, s, C)
  527.             
  528.             Case Else
  529.                 lRet = ERROR_INVALID_DATA
  530.         
  531.         End Select
  532.         
  533.         If lRet = ERROR_SUCCESS Then SetValue = True
  534.         
  535.         RegCloseKey hKey
  536.     
  537.     End If
  538.  
  539.     Exit Function
  540.  
  541. hComponentFailure:
  542.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  543. End Function
  544.  
  545.  
  546. '*****************************************************************************************
  547. '* Function    : ValueType
  548. '* Notes       : Returns a number containing the type of the value stored under the
  549. '*               specified name in a registry key.
  550. '*****************************************************************************************
  551. Public Function ValueType(ByVal KeyName As String, ByVal ValueName As String) As ERegValue
  552.     On Error GoTo hComponentFailure
  553.     
  554.     Dim cData    As Long
  555.     Dim hKey     As Long
  556.     Dim lRet     As Long
  557.     Dim ordType  As Long
  558.     Dim sData    As String
  559.     Dim vValue   As Variant
  560.     
  561.     If Len(KeyName) = 0 Then
  562.         On Error GoTo 0
  563.         Err.Raise eErrRegistry_InvalidKeyName, App.EXEName & ".CRegistry", S_ERR_InvalidKeyName
  564.     End If
  565.     
  566.     lRet = RegOpenKeyEx(m_RootKey, KeyName, 0, KEY_QUERY_VALUE, hKey)
  567.  
  568.     lRet = RegQueryValueExLong(hKey, ValueName, 0&, ordType, 0&, cData)
  569.     If lRet And lRet <> ERROR_MORE_DATA Then
  570.         ValueType = eRegValue_None
  571.     Else
  572.         ValueType = ordType
  573.     End If
  574.  
  575.     Exit Function
  576.  
  577. hComponentFailure:
  578.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  579. End Function
  580.  
  581.  
  582. '*****************************************************************************************
  583. '* Function    : ExpandEnvStr
  584. '* Notes       : Expands environment-variable strings and replaces them with their defined
  585. '*               values.
  586. '*****************************************************************************************
  587. Private Function ExpandEnvStr(sData As String) As String
  588.     On Error GoTo hComponentFailure
  589.     
  590.     Dim lRet     As Long
  591.     Dim sTemp    As String
  592.     
  593.     sTemp = ""
  594.     lRet = ExpandEnvironmentStrings(sData, sTemp, lRet)
  595.     sTemp = String$(lRet - 1, 0)
  596.     lRet = ExpandEnvironmentStrings(sData, sTemp, lRet)
  597.     
  598.     ExpandEnvStr = sTemp
  599.  
  600.     Exit Function
  601.  
  602. hComponentFailure:
  603.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  604. End Function
  605.  
  606.  
  607. '*****************************************************************************************
  608. '* Function    : SwapEndian
  609. '* Notes       : Swaps the highest byte with the lowest byte for the BIG_ENDIAN format.
  610. '*****************************************************************************************
  611. Private Function SwapEndian(ByVal dw As Long) As Long
  612.     On Error GoTo hComponentFailure
  613.     
  614.     CopyMemory ByVal VarPtr(SwapEndian) + 3, dw, 1
  615.     CopyMemory ByVal VarPtr(SwapEndian) + 2, ByVal VarPtr(dw) + 1, 1
  616.     CopyMemory ByVal VarPtr(SwapEndian) + 1, ByVal VarPtr(dw) + 2, 1
  617.     CopyMemory SwapEndian, ByVal VarPtr(dw) + 3, 1
  618.  
  619.     Exit Function
  620.  
  621. hComponentFailure:
  622.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  623. End Function
  624.  
  625.  
  626. '*****************************************************************************************
  627. '* Sub         : Class_Initialize
  628. '* Notes       : Class data space initialization.
  629. '*****************************************************************************************
  630. Private Sub Class_Initialize()
  631.     On Error GoTo hComponentFailure
  632.     
  633.     m_RootKey = eRegRoot_HKeyClassesRoot
  634.  
  635.     Exit Sub
  636.  
  637. hComponentFailure:
  638.     Err.Raise eErrRegistry_ComponentFailure, App.EXEName & ".CRegistry", S_ERR_ComponentFailure
  639. End Sub
  640.