home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / DM_AppPath2133241152008.psc / DmAppPaths / ctrl / CReg.ctl next >
Text File  |  2008-11-05  |  14KB  |  452 lines

  1. VERSION 5.00
  2. Begin VB.UserControl CReg 
  3.    ClientHeight    =   360
  4.    ClientLeft      =   0
  5.    ClientTop       =   0
  6.    ClientWidth     =   360
  7.    InvisibleAtRuntime=   -1  'True
  8.    Picture         =   "CReg.ctx":0000
  9.    ScaleHeight     =   360
  10.    ScaleWidth      =   360
  11.    ToolboxBitmap   =   "CReg.ctx":00D7
  12. End
  13. Attribute VB_Name = "CReg"
  14. Attribute VB_GlobalNameSpace = False
  15. Attribute VB_Creatable = True
  16. Attribute VB_PredeclaredId = False
  17. Attribute VB_Exposed = False
  18. Option Explicit
  19.  
  20. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
  21. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" 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, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
  22. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  23.  
  24. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  25. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
  26. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Any, ByVal cbData As Long) As Long
  27. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByRef lpData As Any, ByRef lpcbData As Long) As Long
  28. Private Declare Function ExpandEnvironmentStrings Lib "kernel32.dll" Alias "ExpandEnvironmentStringsA" (ByVal lpSrc As String, ByVal lpDst As String, ByVal nSize As Long) As Long
  29. 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
  30. Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, ByRef lpcbValueName As Long, ByVal lpReserved As Long, ByRef lpType As Long, ByRef lpData As Byte, ByRef lpcbData As Long) As Long
  31.  
  32. 'Registry keys consts
  33. Enum TKeys
  34.     HKEY_LOCAL_MACHINE = &H80000002
  35.     HKEY_CLASSES_ROOT = &H80000000
  36.     HKEY_CURRENT_CONFIG = &H80000005
  37.     HKEY_CURRENT_USER = &H80000001
  38.     HKEY_DYN_DATA = &H80000006
  39.     HKEY_USERS = &H80000003
  40.     HKEY_PERFORMANCE_DATA = &H80000004
  41. End Enum
  42.  
  43. 'Registry Datatypes
  44. Enum TDatatype
  45.     REG_SZ = 1
  46.     REG_EXPAND_SZ = 2
  47.     REG_DWORD = 4
  48.     REG_MULTI_SZ = 7
  49. End Enum
  50.  
  51. Private Const ERROR_SUCCESS = 0
  52. Private Const KEY_ALL_ACCESS = &H3F
  53. Private Const KEY_SET_VALUE = &H2
  54. Private Const REG_OPTION_NON_VOLATILE As Long = 0
  55. Private Const KEY_READ As Long = &H20019
  56.  
  57. 'The Maximum data length
  58. Private Const MAX_LENGTH As Long = 2048
  59.  
  60. Private m_Key As TKeys
  61. Private m_SubKey As String
  62.  
  63. Private Sub UserControl_Initialize()
  64.     m_Key = HKEY_LOCAL_MACHINE
  65. End Sub
  66.  
  67. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  68.     m_Key = PropBag.ReadProperty("Key", m_Key)
  69.     m_SubKey = PropBag.ReadProperty("SubKey", m_SubKey)
  70. End Sub
  71.  
  72. Private Sub UserControl_Resize()
  73.     UserControl.Size 360, 360
  74. End Sub
  75.  
  76. Public Property Get Key() As TKeys
  77.     Key = m_Key
  78. End Property
  79.  
  80. Public Property Let Key(ByVal vNewKey As TKeys)
  81.     m_Key = vNewKey
  82. End Property
  83.  
  84. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  85.     Call PropBag.WriteProperty("Key", m_Key, HKEY_LOCAL_MACHINE)
  86.     Call PropBag.WriteProperty("SubKey", m_SubKey, "")
  87. End Sub
  88.  
  89. Public Property Get SubKey() As String
  90.     SubKey = m_SubKey
  91. End Property
  92.  
  93. Public Property Let SubKey(ByVal vNewSubKey As String)
  94.     m_SubKey = vNewSubKey
  95. End Property
  96.  
  97. Public Function KeyExsists(Optional KeyValue As String = "") As Long
  98. Dim KeyHandle As Long
  99. Dim KeyPath As String
  100. Dim iRet As Long
  101.     'Check if a reg Key Exsists.
  102.     'Key to check
  103.     KeyPath = (m_SubKey & KeyValue)
  104.     'Open Reg Key
  105.     iRet = RegOpenKeyEx(m_Key, KeyPath, 0, KEY_ALL_ACCESS, KeyHandle)
  106.     KeyExsists = Abs(KeyHandle <> ERROR_SUCCESS)
  107.     'Close the open key
  108.     RegCloseKey KeyHandle
  109.     'Clear up
  110.     KeyPath = vbNullString
  111. End Function
  112.  
  113. Public Function CreateKey(Optional KeyValue As String = "") As Long
  114. 'Create a new key
  115. Dim iRet As Long
  116. Dim KeyCreate As Long
  117. Dim KeyPath As String
  118. Dim Dispose As Long
  119.  
  120.     KeyPath = (m_SubKey & KeyValue)
  121.     
  122.     'Check if the key already exsists
  123.     If RegOpenKeyEx(m_Key, KeyPath, 0, KEY_ALL_ACCESS, KeyCreate) = ERROR_SUCCESS Then
  124.         'Key aready exsists no need to create it agian.
  125.         CreateKey = 2
  126.         KeyPath = vbNullString
  127.         Call CloseRegKey(KeyCreate)
  128.         Exit Function
  129.     Else
  130.         'Create the key
  131.         If RegCreateKeyEx(m_Key, KeyPath, 0, "", _
  132.             REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, KeyCreate, Dispose) <> ERROR_SUCCESS Then
  133.             'Create key faild
  134.             CreateKey = 0
  135.         Else
  136.             'Good result
  137.             CreateKey = 1
  138.         End If
  139.         
  140.         'Close the key
  141.         Call CloseRegKey(KeyCreate)
  142.         'Clear up
  143.         KeyPath = vbNullString
  144.     End If
  145.     
  146. End Function
  147.  
  148. Function ValueExists(ValueName As String) As Boolean
  149. Dim hKeyResult As Long
  150.     'Checks if a Value exisits
  151.     'Open the key and see if it exsists
  152.     If RegOpenKeyEx(m_Key, m_SubKey, 0, KEY_ALL_ACCESS, hKeyResult) <> ERROR_SUCCESS Then
  153.         Exit Function
  154.     End If
  155.     
  156.     'Get the size of the value
  157.     If RegQueryValueEx(hKeyResult, ValueName, 0&, _
  158.          0, ByVal 0&, 0&) <> ERROR_SUCCESS Then
  159.          'Value was not found
  160.          ValueExists = False
  161.          Exit Function
  162.     Else
  163.         'Value found
  164.         ValueExists = True
  165.     End If
  166.     'Close the key
  167.     Call CloseRegKey(hKeyResult)
  168.     
  169. End Function
  170.  
  171. Public Function DeleteKey(Optional KeyValue As String = "") As Long
  172. 'Delete key
  173. Dim iRet As Long
  174. Dim KeyPath As String
  175.  
  176.     'Key to delete
  177.     KeyPath = (m_SubKey & KeyValue)
  178.     'Delete the key
  179.     iRet = RegDeleteKey(m_Key, KeyPath)
  180.     
  181.     If (iRet <> ERROR_SUCCESS) Then
  182.         DeleteKey = 0
  183.     Else
  184.         DeleteKey = 1
  185.     End If
  186.     
  187.     'Clear up
  188.     KeyPath = vbNullString
  189.     
  190. End Function
  191.  
  192. Public Function SetValue(ValueName As String, ByVal ValueData, DataType As TDatatype) As Long
  193. Dim KeyCreate As Long
  194. Dim iRet As Long
  195. Dim sBuff As String
  196. Dim vCount As Long
  197.  
  198.     'Open the key
  199.     iRet = RegOpenKeyEx(m_Key, m_SubKey, 0, KEY_ALL_ACCESS, KeyCreate)
  200.     If (iRet <> ERROR_SUCCESS) Then
  201.             Exit Function
  202.     Else
  203.         Select Case DataType
  204.             Case REG_SZ, REG_EXPAND_SZ
  205.                 'Write Reg Value for strings
  206.                 iRet = RegSetValueEx(KeyCreate, ValueName, 0&, _
  207.                 DataType, ByVal CStr(ValueData), Len(ValueData))
  208.             Case REG_DWORD
  209.                 'Write Reg Value for DWORDS
  210.                 iRet = RegSetValueEx(KeyCreate, ValueName, 0&, _
  211.                 DataType, CLng(ValueData), 4)
  212.             Case REG_MULTI_SZ
  213.                 'Writes a String list
  214.                 If IsArray(ValueData) Then
  215.                     For vCount = 0 To UBound(ValueData)
  216.                         'Build the string
  217.                         sBuff = sBuff + ValueData(vCount) + Chr(0)
  218.                     Next vCount
  219.                     'End terminator
  220.                     sBuff = sBuff + Chr(0)
  221.                     'Write REG_MULTI_SZ Value
  222.                     iRet = RegSetValueEx(KeyCreate, ValueName, 0&, _
  223.                     DataType, ByVal CStr(sBuff), Len(sBuff))
  224.                 End If
  225.         End Select
  226.     End If
  227.     
  228.     SetValue = 1
  229.     'Close the openkey
  230.     Call CloseRegKey(KeyCreate)
  231.     'Clean up
  232.     sBuff = vbNullString
  233.     vCount = 0
  234.     
  235. End Function
  236.  
  237. Function GetValue(ValueName As String, DataType As TDatatype, Optional Defaut)
  238. Dim KeyCreate As Long
  239. Dim bSize As Long
  240. Dim bStr As String
  241. Dim iRet As Long
  242. Dim RegDWord As Long
  243. Dim lType As Long
  244. Dim lpDst As String
  245. Dim tmp(0) As String
  246.  
  247.     'Type to read Strings, DWORDS
  248.     lType = DataType
  249.     
  250.     'Open the key and see if it exsists
  251.     If RegOpenKeyEx(m_Key, m_SubKey, 0, KEY_ALL_ACCESS, KeyCreate) <> ERROR_SUCCESS Then
  252.         Exit Function
  253.     End If
  254.     
  255.     'Get the size of the value
  256.     If RegQueryValueEx(KeyCreate, ValueName, 0&, _
  257.         lType, ByVal 0&, bSize) <> ERROR_SUCCESS Then
  258.         'Return Default Value
  259.         GetValue = Defaut
  260.     End If
  261.  
  262.     Select Case DataType
  263.         Case REG_SZ, REG_EXPAND_SZ
  264.             'Create string buffer
  265.             bStr = String(bSize, Chr(0))
  266.             'Read string value
  267.             If RegQueryValueEx(KeyCreate, ValueName, 0&, _
  268.                 lType, ByVal bStr, bSize) <> ERROR_SUCCESS Then
  269.                 'Return Default Value
  270.                 GetValue = Defaut
  271.             End If
  272.             'Strip away NULL Chars from the string
  273.             bStr = TrimNull(bStr)
  274.             
  275.             'Return the value
  276.             If (DataType = REG_EXPAND_SZ) Then
  277.                 'Get the size of the string
  278.                 bSize = ExpandEnvironmentStrings(bStr, lpDst, 1)
  279.                 'Create a buffer to hold the new string
  280.                 lpDst = Space(bSize)
  281.                 'Extract Environment varaible
  282.                 iRet = ExpandEnvironmentStrings(bStr, lpDst, bSize)
  283.                 'Trip away null chars
  284.                 bStr = TrimNull(lpDst)
  285.                 'Return string
  286.                 GetValue = bStr
  287.             Else
  288.                 'Return normal string
  289.                 GetValue = bStr
  290.             End If
  291.             'Close the Reg key
  292.             RegCloseKey KeyCreate
  293.         Case REG_DWORD
  294.             'Read Numeric value
  295.             If RegQueryValueEx(KeyCreate, ValueName, 0&, _
  296.                  lType, RegDWord, bSize) <> ERROR_SUCCESS Then
  297.                 GetValue = Defaut
  298.             Else
  299.                 GetValue = RegDWord
  300.             End If
  301.         Case REG_MULTI_SZ
  302.             'Set the buffer size
  303.             If (bSize - 1) < 0 Then
  304.                 'Returns an array of size zero
  305.                 GetValue = tmp
  306.             Else
  307.                 bStr = String(bSize - 1, Chr(0))
  308.                 'Get the values data
  309.                 iRet = RegQueryValueEx(KeyCreate, ValueName, 0&, lType, ByVal bStr, bSize)
  310.                 'Return Array
  311.                 GetValue = Split(bStr, Chr(0))
  312.             End If
  313.     End Select
  314.     
  315.     'Clear up
  316.     bSize = 0
  317.     Erase tmp
  318.     lpDst = vbNullString
  319.     bStr = vbNullString
  320.     
  321. End Function
  322.  
  323. Function DeleteValue(ValueName As String) As Boolean
  324. Dim iRet As Long
  325. Dim hKeyResult As Long
  326.     'Delete a regvalue
  327.     
  328.     'Open the key and see if it exsists
  329.     If RegOpenKeyEx(m_Key, m_SubKey, 0, KEY_ALL_ACCESS, hKeyResult) <> ERROR_SUCCESS Then
  330.         Exit Function
  331.     End If
  332.     
  333.     iRet = RegDeleteValue(hKeyResult, ValueName)
  334.     
  335.     If (iRet = ERROR_SUCCESS) Then
  336.         DeleteValue = True
  337.     End If
  338.     
  339.     'Close the key
  340.     Call CloseRegKey(hKeyResult)
  341. End Function
  342.  
  343. Function GetSubKeys(Optional KeyValue As String = "") As Collection
  344. Dim hKeyResult As Long
  345. Dim kName As String
  346. Dim kSize As Long
  347. Dim TmpCol As New Collection
  348. Dim kCount As Long
  349.  
  350.     'Check if the key is found
  351.     If RegOpenKeyEx(m_Key, m_SubKey & KeyValue, 0, KEY_ALL_ACCESS, hKeyResult) <> ERROR_SUCCESS Then
  352.         RegCloseKey hKeyResult
  353.         'Key was not found so we return array size of zero
  354.         Set GetSubKeys = TmpCol
  355.         'Clear up
  356.         Exit Function
  357.     End If
  358.     
  359.     'Keyname size
  360.     kSize = MAX_LENGTH
  361.     'Create Buffer
  362.     kName = Space(kSize)
  363.     'Get the subkey names
  364.     Do While RegEnumKey(hKeyResult, kCount, kName, kSize) = ERROR_SUCCESS
  365.         'Strip waway the NULL char and add the Keyname to the collection
  366.         TmpCol.Add TrimNull(kName)
  367.         kCount = kCount + 1
  368.     Loop
  369.     
  370.     'Return the colelction
  371.     Set GetSubKeys = TmpCol
  372.     
  373.     'Clear up
  374.     Set TmpCol = Nothing
  375.     kName = vbNullString
  376.     kSize = 0
  377.     'Close OpenKey
  378.     Call CloseRegKey(hKeyResult)
  379. End Function
  380.  
  381. Function GetValueNames(Optional KeyValue As String = "") As Collection
  382. Dim hKeyResult As Long
  383. Dim iRet As Long
  384. Dim vName As String
  385. Dim vSize As Long
  386. Dim vCount As Long
  387. Dim dLen As Long
  388. Dim TmpCol As New Collection
  389.  
  390.     'Check if the key is found
  391.     If RegOpenKeyEx(m_Key, m_SubKey & KeyValue, 0, KEY_READ, hKeyResult) Then
  392.         Call CloseRegKey(hKeyResult)
  393.         Exit Function
  394.     End If
  395.     
  396.     Do
  397.         'Set the value name size
  398.         vSize = 255
  399.         dLen = vSize
  400.         'Create buffer
  401.         vName = String(vSize, Chr(0))
  402.         'Get the value name
  403.         iRet = RegEnumValue(hKeyResult, vCount, vName, _
  404.         vSize, ByVal 0&, ByVal 0&, ByVal 0&, dLen)
  405.  
  406.         If (iRet = ERROR_SUCCESS) Then
  407.             'Fill the collection with the value names
  408.             TmpCol.Add Left(vName, vSize)
  409.         Else
  410.             Exit Do
  411.         End If
  412.         
  413.         vCount = vCount + 1
  414.     Loop While (iRet = ERROR_SUCCESS)
  415.     
  416.     Set GetValueNames = TmpCol
  417.     'Clear up
  418.     Call CloseRegKey(hKeyResult)
  419.     Set TmpCol = Nothing
  420.     vName = vbNullString
  421.     vSize = 0
  422.     dLen = 0
  423.     vCount = 0
  424.     
  425.     
  426. End Function
  427.  
  428. Private Function TrimNull(lpStr As String) As String
  429. Dim e_pos As Integer
  430.     'Trim away the NULL char of a string
  431.     e_pos = InStr(lpStr, Chr$(0))
  432.  
  433.     If (e_pos) Then
  434.         TrimNull = Left$(lpStr, e_pos - 1)
  435.     Else
  436.         TrimNull = lpStr
  437.     End If
  438.  
  439. End Function
  440.  
  441. Private Sub CloseRegKey(lngKey As Long)
  442. Dim Result As Long
  443. On Error Resume Next
  444.     'Close the open Regkey
  445.     Result = RegCloseKey(lngKey)
  446.     'Check that the regkey was closed
  447.     If (Result <> ERROR_SUCCESS) Then
  448.         Err.Raise 9 + vbObject, "CloseRegKey", "RegCloseKey Faild."
  449.     End If
  450.     
  451. End Sub
  452.