Advertisement
PikaNikz

clsRegistry.cls

Feb 10th, 2012
220
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 = "clsRegistry"
  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. ' Class:            clsRegistry
  18. ' Author:           JA
  19. '__________________________________________________________
  20.  
  21. 'Registry Specific Access Rights
  22. Private Const KEY_QUERY_VALUE = &H1
  23. Private Const KEY_SET_VALUE = &H2
  24. Private Const KEY_CREATE_SUB_KEY = &H4
  25. Private Const KEY_ENUMERATE_SUB_KEYS = &H8
  26. Private Const KEY_NOTIFY = &H10
  27. Private Const KEY_CREATE_LINK = &H20
  28. Private Const KEY_ALL_ACCESS = &H3F
  29.  
  30. 'Open/Create Options
  31. Private Const REG_OPTION_NON_VOLATILE = 0&
  32. Private Const REG_OPTION_VOLATILE = &H1
  33.  
  34. 'Key creation/open disposition
  35. Private Const REG_CREATED_NEW_KEY = &H1
  36. Private Const REG_OPENED_EXISTING_KEY = &H2
  37.  
  38. 'masks for the predefined standard access types
  39. Private Const STANDARD_RIGHTS_ALL = &H1F0000
  40. Private Const SPECIFIC_RIGHTS_ALL = &HFFFF
  41.  
  42. 'Define severity codes
  43. Private Const ERROR_SUCCESS = 0&
  44. Private Const ERROR_ACCESS_DENIED = 5
  45. Private Const ERROR_INVALID_DATA = 13&
  46. Private Const ERROR_MORE_DATA = 234 '  dderror
  47. Private Const ERROR_NO_MORE_ITEMS = 259
  48.  
  49. 'Structures Needed For Registry Prototypes
  50. Private Type SECURITY_ATTRIBUTES
  51.     nLength                     As Long
  52.     lpSecurityDescriptor        As Long
  53.     bInheritHandle              As Boolean
  54. End Type
  55.  
  56. Private Type FILETIME
  57.     dwLowDateTime               As Long
  58.     dwHighDateTime              As Long
  59. End Type
  60.  
  61. 'Registry Function Prototypes
  62. Private Declare Function RegOpenKeyEx _
  63.                          Lib "advapi32" _
  64.                          Alias "RegOpenKeyExA" _
  65.                          (ByVal HKEY As Long, _
  66.                           ByVal lpSubKey As String, _
  67.                           ByVal ulOptions As Long, _
  68.                           ByVal samDesired As Long, _
  69.                           phkResult As Long _
  70.                          ) As Long
  71.  
  72. Private Declare Function RegSetValueExStr _
  73.                          Lib "advapi32" _
  74.                          Alias "RegSetValueExA" _
  75.                          (ByVal HKEY As Long, _
  76.                           ByVal lpValueName As String, _
  77.                           ByVal Reserved As Long, _
  78.                           ByVal dwType As Long, _
  79.                           ByVal szData As String, _
  80.                           ByVal cbData As Long _
  81.                          ) As Long
  82.  
  83. Private Declare Function RegSetValueExLong _
  84.                          Lib "advapi32" _
  85.                          Alias "RegSetValueExA" _
  86.                          (ByVal HKEY As Long, _
  87.                           ByVal lpValueName As String, _
  88.                           ByVal Reserved As Long, _
  89.                           ByVal dwType As Long, _
  90.                           szData As Long, _
  91.                           ByVal cbData As Long _
  92.                          ) As Long
  93.  
  94. Private Declare Function RegSetValueExByte _
  95.                          Lib "advapi32" _
  96.                          Alias "RegSetValueExA" _
  97.                          (ByVal HKEY As Long, _
  98.                           ByVal lpValueName As String, _
  99.                           ByVal Reserved As Long, _
  100.                           ByVal dwType As Long, _
  101.                           szData As Byte, _
  102.                           ByVal cbData As Long _
  103.                          ) As Long
  104.  
  105. Private Declare Function RegCloseKey _
  106.                          Lib "advapi32" _
  107.                          (ByVal HKEY As Long _
  108.                          ) As Long
  109.  
  110. Private Declare Function RegQueryValueExStr _
  111.                          Lib "advapi32" _
  112.                          Alias "RegQueryValueExA" _
  113.                          (ByVal HKEY As Long, _
  114.                           ByVal lpValueName As String, _
  115.                           ByVal lpReserved As Long, _
  116.                           ByRef lpType As Long, _
  117.                           ByVal szData As String, _
  118.                           ByRef lpcbData As Long _
  119.                          ) As Long
  120.  
  121. Private Declare Function RegQueryValueExLong _
  122.                          Lib "advapi32" _
  123.                          Alias "RegQueryValueExA" _
  124.                          (ByVal HKEY As Long, _
  125.                           ByVal lpValueName As String, _
  126.                           ByVal lpReserved As Long, _
  127.                           ByRef lpType As Long, _
  128.                           szData As Long, _
  129.                           ByRef lpcbData As Long _
  130.                          ) As Long
  131.  
  132. Private Declare Function RegQueryValueExByte _
  133.                          Lib "advapi32" _
  134.                          Alias "RegQueryValueExA" _
  135.                          (ByVal HKEY As Long, _
  136.                           ByVal lpValueName As String, _
  137.                           ByVal lpReserved As Long, _
  138.                           ByRef lpType As Long, _
  139.                           szData As Byte, _
  140.                           ByRef lpcbData As Long _
  141.                          ) As Long
  142.  
  143. Private Declare Function RegCreateKeyEx _
  144.                          Lib "advapi32" _
  145.                          Alias "RegCreateKeyExA" _
  146.                          (ByVal HKEY As Long, _
  147.                           ByVal lpSubKey As String, _
  148.                           ByVal Reserved As Long, _
  149.                           ByVal lpClass As String, _
  150.                           ByVal dwOptions As Long, _
  151.                           ByVal samDesired As Long, _
  152.                           lpSecurityAttributes As SECURITY_ATTRIBUTES, _
  153.                           phkResult As Long, _
  154.                           lpdwDisposition As Long _
  155.                          ) As Long
  156.  
  157. Private Declare Function RegEnumKeyEx _
  158.                          Lib "advapi32.dll" _
  159.                          Alias "RegEnumKeyExA" _
  160.                          (ByVal HKEY As Long, _
  161.                           ByVal dwIndex As Long, _
  162.                           ByVal lpName As String, _
  163.                           lpcbName As Long, _
  164.                           ByVal lpReserved As Long, _
  165.                           ByVal lpClass As String, _
  166.                           lpcbClass As Long, _
  167.                           lpftLastWriteTime As FILETIME _
  168.                          ) As Long
  169.  
  170. Private Declare Function RegEnumKey _
  171.                          Lib "advapi32.dll" _
  172.                          Alias "RegEnumKeyA" _
  173.                          (ByVal HKEY As Long, _
  174.                           ByVal dwIndex As Long, _
  175.                           ByVal lpName As String, _
  176.                           ByVal cbName As Long _
  177.                          ) As Long
  178.  
  179. Private Declare Function RegEnumValue _
  180.                          Lib "advapi32.dll" _
  181.                          Alias "RegEnumValueA" _
  182.                          (ByVal HKEY As Long, _
  183.                           ByVal dwIndex As Long, _
  184.                           ByVal lpValueName As String, _
  185.                           lpcbValueName As Long, _
  186.                           ByVal lpReserved As Long, _
  187.                           ByVal lpType As Long, _
  188.                           ByVal lpData As Long, _
  189.                           ByVal lpcbData As Long _
  190.                          ) As Long
  191.  
  192. Private Declare Function RegEnumValueLong _
  193.                          Lib "advapi32.dll" _
  194.                          Alias "RegEnumValueA" _
  195.                          (ByVal HKEY As Long, _
  196.                           ByVal dwIndex As Long, _
  197.                           ByVal lpValueName As String, _
  198.                           lpcbValueName As Long, _
  199.                           ByVal lpReserved As Long, _
  200.                           lpType As Long, _
  201.                           lpData As Long, _
  202.                           lpcbData As Long _
  203.                          ) As Long
  204.  
  205. Private Declare Function RegEnumValueStr _
  206.                          Lib "advapi32.dll" _
  207.                          Alias "RegEnumValueA" _
  208.                          (ByVal HKEY As Long, _
  209.                           ByVal dwIndex As Long, _
  210.                           ByVal lpValueName As String, _
  211.                           lpcbValueName As Long, _
  212.                           ByVal lpReserved As Long, _
  213.                           lpType As Long, _
  214.                           ByVal lpData As String, _
  215.                           lpcbData As Long _
  216.                          ) As Long
  217.  
  218. Private Declare Function RegEnumValueByte _
  219.                          Lib "advapi32.dll" _
  220.                          Alias "RegEnumValueA" _
  221.                          (ByVal HKEY As Long, _
  222.                           ByVal dwIndex As Long, _
  223.                           ByVal lpValueName As String, _
  224.                           lpcbValueName As Long, _
  225.                           ByVal lpReserved As Long, _
  226.                           lpType As Long, _
  227.                           lpData As Byte, _
  228.                           lpcbData As Long _
  229.                          ) As Long
  230.  
  231. Private Declare Function RegQueryInfoKey _
  232.                          Lib "advapi32.dll" _
  233.                          Alias "RegQueryInfoKeyA" _
  234.                          (ByVal HKEY As Long, _
  235.                           ByVal lpClass As String, _
  236.                           lpcbClass As Long, _
  237.                           ByVal lpReserved As Long, _
  238.                           lpcSubKeys As Long, _
  239.                           lpcbMaxSubKeyLen As Long, _
  240.                           lpcbMaxClassLen As Long, _
  241.                           lpcValues As Long, _
  242.                           lpcbMaxValueNameLen As Long, _
  243.                           lpcbMaxValueLen As Long, _
  244.                           lpcbSecurityDescriptor As Long, _
  245.                           lpftLastWriteTime As Any _
  246.                          ) As Long
  247.  
  248. Private Declare Function RegDeleteKey _
  249.                          Lib "advapi32.dll" _
  250.                          Alias "RegDeleteKeyA" _
  251.                          (ByVal HKEY As Long, _
  252.                           ByVal lpSubKey As String _
  253.                          ) As Long
  254.  
  255. Private Declare Function RegDeleteValue _
  256.                          Lib "advapi32.dll" _
  257.                          Alias "RegDeleteValueA" _
  258.                          (ByVal HKEY As Long, _
  259.                           ByVal lpValueName As String _
  260.                          ) As Long
  261.  
  262. ' Other declares:
  263. Private Declare Sub CopyMemory _
  264.                     Lib "kernel32" _
  265.                     Alias "RtlMoveMemory" _
  266.                     (lpvDest As Any, _
  267.                      lpvSource As Any, _
  268.                      ByVal cbCopy As Long _
  269.                     )
  270.  
  271. Private Declare Function ExpandEnvironmentStrings _
  272.                          Lib "kernel32" _
  273.                          Alias "ExpandEnvironmentStringsA" _
  274.                          (ByVal lpSrc As String, _
  275.                           ByVal lpDst As String, _
  276.                           ByVal nSize As Long _
  277.                          ) As Long
  278.  
  279. Public Enum ERegistryClassConstants
  280.     HKEY_CLASSES_ROOT = &H80000000
  281.     HKEY_CURRENT_USER = &H80000001
  282.     HKEY_LOCAL_MACHINE = &H80000002
  283.     HKEY_USERS = &H80000003
  284. End Enum
  285.  
  286. 'Predefined Value Types
  287. Public Enum ERegistryValueTypes
  288.     REG_NONE = (0)                         'No value type
  289.    REG_SZ = (1)                           'Unicode nul terminated string
  290.    REG_EXPAND_SZ = (2)                    'Unicode nul terminated string w/enviornment var
  291.    REG_BINARY = (3)                       'Free form binary
  292.    REG_DWORD = (4)                        '32-bit number
  293.    REG_DWORD_LITTLE_ENDIAN = (4)          '32-bit number (same as REG_DWORD)
  294.    REG_DWORD_BIG_ENDIAN = (5)             '32-bit number
  295.    REG_LINK = (6)                         'Symbolic Link (unicode)
  296.    REG_MULTI_SZ = (7)                     'Multiple Unicode strings
  297.    REG_RESOURCE_LIST = (8)                'Resource list in the resource map
  298.    REG_FULL_RESOURCE_DESCRIPTOR = (9)     'Resource list in the hardware description
  299.    REG_RESOURCE_REQUIREMENTS_LIST = (10)
  300. End Enum
  301.  
  302. Private m_hClassKey         As Long
  303. Private m_sSectionKey       As String
  304. Private m_sValueKey         As String
  305. Private m_vValue            As Variant
  306. Private m_sSetValue         As String
  307. Private m_vDefault          As Variant
  308. Private m_eValueType        As ERegistryValueTypes
  309.  
  310. Public Property Get ClassKey( _
  311.                             ) As ERegistryClassConstants
  312.    
  313.     ClassKey = m_hClassKey
  314.    
  315. End Property
  316.  
  317. Public Property Let ClassKey(ByVal eKey As ERegistryClassConstants)
  318.    
  319.     m_hClassKey = eKey
  320.    
  321. End Property
  322.  
  323. Public Sub CreateAdditionalEXEAssociations(ByVal sClassName As String, _
  324.                                            ParamArray vItems() As Variant _
  325.                                           )
  326. On Error Resume Next
  327.    
  328.     Dim iItems      As Long
  329.     Dim iItem       As Long
  330.    
  331.     iItems = UBound(vItems) + 1
  332.     If (iItems Mod 3) <> 0 Or (Err.Number <> 0) Then
  333.         Err.Raise vbObjectError + 1048 + 26004, _
  334.                   App.EXEName & ".clsRegistry", _
  335.                   "Invalid parameter list passed to CreateAdditionalEXEAssociations - expected Name/Text/Command"
  336.     Else
  337.        
  338.         ' Check if it exists:
  339.        SectionKey = sClassName
  340.         If Not (KeyExists) Then
  341.             Err.Raise vbObjectError + 1048 + 26005, _
  342.                       App.EXEName & ".clsRegistry", _
  343.                       "Error - attempt to create additional associations before class defined."
  344.         Else
  345.            
  346.             For iItem = 0 To iItems - 1 Step 3
  347.                 ValueType = REG_SZ
  348.                 SectionKey = sClassName & "\shell\" & vItems(iItem)
  349.                 ValueKey = ""
  350.                 pSetClassValue vItems(iItem + 1)
  351.                 SectionKey = sClassName & "\shell\" & vItems(iItem) & "\command"
  352.                 ValueKey = ""
  353.                 pSetClassValue vItems(iItem + 2)
  354.             Next iItem
  355.         End If
  356.     End If
  357.    
  358. End Sub
  359.  
  360. Public Sub CreateEXEAssociation( _
  361.            ByVal sExePath As String, _
  362.            ByVal sClassName As String, _
  363.            ByVal sClassDescription As String, _
  364.            ByVal sAssociation As String, _
  365.            Optional ByVal sOpenMenuText As String = "&Open", _
  366.            Optional ByVal bSupportPrint As Boolean = False, _
  367.            Optional ByVal sPrintMenuText As String = "&Print", _
  368.            Optional ByVal bSupportNew As Boolean = False, _
  369.            Optional ByVal sNewMenuText As String = "&New", _
  370.            Optional ByVal bSupportInstall As Boolean = False, _
  371.            Optional ByVal sInstallMenuText As String = "", _
  372.            Optional ByVal lDefaultIconIndex As Long = -1)
  373.    
  374.     ' Check if path is wrapped in quotes:
  375.    sExePath = Trim$(sExePath)
  376.    
  377.     If (Left$(sExePath, 1) <> """") Then sExePath = """" & sExePath
  378.     If (Right$(sExePath, 1) <> """") Then sExePath = sExePath & """"
  379.    
  380.     ' Create the .File to Class association:
  381.    SectionKey = "." & sAssociation
  382.     ValueType = REG_SZ
  383.     ValueKey = ""
  384.     pSetClassValue sClassName
  385.    
  386.     ' Create the Class shell open command:
  387.    SectionKey = sClassName
  388.     pSetClassValue sClassDescription
  389.    
  390.     SectionKey = sClassName & "\shell\open"
  391.     If (sOpenMenuText = "") Then sOpenMenuText = "&Open"
  392.     ValueKey = ""
  393.     pSetClassValue sOpenMenuText
  394.     SectionKey = sClassName & "\shell\open\command"
  395.     ValueKey = ""
  396.     pSetClassValue sExePath & " ""%1"""
  397.    
  398.     If (bSupportPrint) Then
  399.         SectionKey = sClassName & "\shell\print"
  400.         If (sPrintMenuText = "") Then sPrintMenuText = "&Print"
  401.         ValueKey = ""
  402.         pSetClassValue sPrintMenuText
  403.         SectionKey = sClassName & "\shell\print\command"
  404.         ValueKey = ""
  405.         pSetClassValue sExePath & " /p ""%1"""
  406.     End If
  407.    
  408.     If (bSupportInstall) Then
  409.         If (sInstallMenuText = "") Then sInstallMenuText = "&Install " & sAssociation
  410.         SectionKey = sClassName & "\shell\add"
  411.         ValueKey = ""
  412.         pSetClassValue sInstallMenuText
  413.         SectionKey = sClassName & "\shell\add\command"
  414.         ValueKey = ""
  415.         pSetClassValue sExePath & " /a ""%1"""
  416.     End If
  417.    
  418.     If (bSupportNew) Then
  419.         SectionKey = sClassName & "\shell\new"
  420.         ValueKey = ""
  421.         If (sNewMenuText = "") Then sNewMenuText = "&New"
  422.         pSetClassValue sNewMenuText
  423.         SectionKey = sClassName & "\shell\new\command"
  424.         ValueKey = ""
  425.         pSetClassValue sExePath & " /n ""%1"""
  426.     End If
  427.    
  428.     If lDefaultIconIndex > -1 Then
  429.         SectionKey = sClassName & "\DefaultIcon"
  430.         ValueKey = ""
  431.         pSetClassValue sExePath & "," & CStr(lDefaultIconIndex)
  432.     End If
  433.    
  434. End Sub
  435.  
  436. Public Function CreateKey( _
  437.                          ) As Boolean
  438.    
  439.     Dim tSA             As SECURITY_ATTRIBUTES
  440.     Dim HKEY            As Long
  441.     Dim lCreate         As Long
  442.     Dim e               As Long
  443.    
  444.     'Open or Create the key
  445.    e = RegCreateKeyEx(m_hClassKey, m_sSectionKey, 0, "", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, tSA, HKEY, lCreate)
  446.    
  447.     If e Then
  448.         Err.Raise 26001, App.EXEName & ".clsRegistry", "Failed to create registry Key: '" & m_sSectionKey
  449.     Else
  450.         CreateKey = (e = ERROR_SUCCESS)
  451.         'Close the key
  452.        RegCloseKey HKEY
  453.     End If
  454.    
  455. End Function
  456.  
  457. Public Property Get Default( _
  458.                            ) As Variant
  459.    
  460.     Default = m_vDefault
  461.    
  462. End Property
  463.  
  464. Public Property Let Default(ByVal vDefault As Variant)
  465.    
  466.     m_vDefault = vDefault
  467.    
  468. End Property
  469.  
  470. Public Function DeleteKey( _
  471.                          ) As Boolean
  472.    
  473.     Dim e       As Long
  474.    
  475.     e = RegDeleteKey(m_hClassKey, m_sSectionKey)
  476.    
  477.     If e Then
  478.         'Err.Raise 26001, App.EXEName & ".clsRegistry", "Failed to delete registry Key: '" & m_hClassKey & "',Section: '" & m_sSectionKey
  479.    Else
  480.         DeleteKey = (e = ERROR_SUCCESS)
  481.     End If
  482.    
  483. End Function
  484.  
  485. Public Function DeleteValue( _
  486.                            ) As Boolean
  487.    
  488.     Dim e           As Long
  489.     Dim HKEY        As Long
  490.    
  491.     e = RegOpenKeyEx(m_hClassKey, m_sSectionKey, 0, KEY_ALL_ACCESS, HKEY)
  492.    
  493.     If e Then
  494.         'Err.Raise 26001, App.EXEName & ".clsRegistry", "Failed to open key '" & m_hClassKey & "',Section: '" & m_sSectionKey & "' for delete access"
  495.    Else
  496.         e = RegDeleteValue(HKEY, m_sValueKey)
  497.        
  498.         If e Then
  499.             'Err.Raise 26001, App.EXEName & ".clsRegistry", "Failed to delete registry Key: '" & m_hClassKey & "',Section: '" & m_sSectionKey & "',Key: '" & m_sValueKey
  500.        Else
  501.             DeleteValue = (e = ERROR_SUCCESS)
  502.         End If
  503.     End If
  504.    
  505. End Function
  506.  
  507. Public Function EnumerateSections(ByRef sSect() As String, _
  508.                                   ByRef iSectCount As Long _
  509.                                  ) As Boolean
  510. On Error GoTo EnumerateSectionsError
  511.    
  512.     Dim lResult         As Long
  513.     Dim HKEY            As Long
  514.     Dim dwReserved      As Long
  515.     Dim szBuffer        As String
  516.     Dim lBuffSize       As Long
  517.     Dim lIndex          As Long
  518.     Dim lType           As Long
  519.     Dim sCompKey        As String
  520.     Dim iPos            As Long
  521.    
  522.     iSectCount = 0
  523.     Erase sSect
  524.    
  525.     lIndex = 0
  526.    
  527.     lResult = RegOpenKeyEx(m_hClassKey, m_sSectionKey, 0, KEY_ENUMERATE_SUB_KEYS, HKEY)
  528.    
  529.     Do While lResult = ERROR_SUCCESS
  530.         'Set buffer space
  531.        szBuffer = String$(255, 0)
  532.         lBuffSize = Len(szBuffer)
  533.        
  534.         'Get next value
  535.        lResult = RegEnumKey(HKEY, lIndex, szBuffer, lBuffSize)
  536.        
  537.         If (lResult = ERROR_SUCCESS) Then
  538.             iSectCount = iSectCount + 1
  539.             ReDim Preserve sSect(1 To iSectCount) As String
  540.             iPos = InStr(szBuffer, Chr$(0))
  541.             If (iPos > 0) Then
  542.                 sSect(iSectCount) = Left(szBuffer, iPos - 1)
  543.             Else
  544.                 sSect(iSectCount) = Left(szBuffer, lBuffSize)
  545.             End If
  546.         End If
  547.        
  548.         lIndex = lIndex + 1
  549.     Loop
  550.    
  551.     If (HKEY <> 0) Then RegCloseKey HKEY
  552.    
  553. EnumerateSections = True
  554. Exit Function
  555. EnumerateSectionsError:
  556.     If (HKEY <> 0) Then
  557.         RegCloseKey HKEY
  558.     End If
  559.         Err.Raise vbObjectError + 1048 + 26002, _
  560.                   App.EXEName & ".clsRegistry", _
  561.                   Err.Description
  562.     Exit Function
  563.    
  564. End Function
  565.  
  566. Public Function EnumerateValues(ByRef sKeyNames() As String, _
  567.                                 ByRef iKeyCount As Long _
  568.                                ) As Boolean
  569.    
  570.     Dim lResult         As Long
  571.     Dim HKEY            As Long
  572.     Dim sName           As String
  573.     Dim lNameSize       As Long
  574.     Dim sData           As String
  575.     Dim lIndex          As Long
  576.     Dim cJunk           As Long
  577.     Dim cNameMax        As Long
  578.     Dim ft              As Currency
  579.    
  580.     ' Log "EnterEnumerateValues"
  581.    
  582.     iKeyCount = 0
  583.     Erase sKeyNames()
  584.    
  585.     lIndex = 0
  586.     lResult = RegOpenKeyEx(m_hClassKey, m_sSectionKey, 0, KEY_QUERY_VALUE, HKEY)
  587.    
  588.     If (lResult = ERROR_SUCCESS) Then
  589.         ' Log "OpenedKey:" & m_hClassKey & "," & m_sSectionKey
  590.        lResult = RegQueryInfoKey(HKEY, "", cJunk, 0, _
  591.         cJunk, cJunk, cJunk, cJunk, _
  592.         cNameMax, cJunk, cJunk, ft)
  593.        
  594.         Do While lResult = ERROR_SUCCESS
  595.             'Set buffer space
  596.            lNameSize = cNameMax + 1
  597.             sName = String$(lNameSize, 0)
  598.             If (lNameSize = 0) Then lNameSize = 1
  599.            
  600.             ' Log "Requesting Next Value"
  601.            
  602.             'Get value name:
  603.            lResult = RegEnumValue(HKEY, lIndex, sName, lNameSize, 0&, 0&, 0&, 0&)
  604.            
  605.             ' Log "RegEnumValue returned:" & lResult
  606.            If (lResult = ERROR_SUCCESS) Then
  607.                 ' Although in theory you can also retrieve the actual
  608.                ' value and type here, I found it always (ultimately) resulted in
  609.                ' a GPF, on Win95 and NT.  Why?  Can anyone help?
  610.                
  611.                 sName = Left$(sName, lNameSize)
  612.                
  613.                 ' Log "Enumerated value:" & sName
  614.                iKeyCount = iKeyCount + 1
  615.                
  616.                 ReDim Preserve sKeyNames(1 To iKeyCount) As String
  617.                
  618.                 sKeyNames(iKeyCount) = sName
  619.                
  620.             End If
  621.            
  622.             lIndex = lIndex + 1
  623.         Loop
  624.     End If
  625.    
  626.     If (HKEY <> 0) Then RegCloseKey HKEY
  627.    
  628.     ' Log "Exit Enumerate Values"
  629.    
  630. EnumerateValues = True
  631. Exit Function
  632. EnumerateValuesError:
  633.     If (HKEY <> 0) Then
  634.         RegCloseKey HKEY
  635.     End If
  636.         Err.Raise vbObjectError + 1048 + 26003, _
  637.                   App.EXEName & ".clsRegistry", _
  638.                   Err.Description
  639.     Exit Function
  640.    
  641. End Function
  642.  
  643. Private Function ExpandEnvStr(sData As String _
  644.                              ) As String
  645.    
  646.     Dim c       As Long
  647.     Dim s       As String
  648.    
  649.     ' Get the length
  650.    s = "" ' Needed to get around Windows 95 limitation
  651.    c = ExpandEnvironmentStrings(sData, s, c)
  652.    
  653.     ' Expand the string
  654.    s = String$(c - 1, 0)
  655.     c = ExpandEnvironmentStrings(sData, s, c)
  656.    
  657.     ExpandEnvStr = s
  658.    
  659. End Function
  660.  
  661. 'KeyExists = bCheckKeyExists(m_hClassKey, m_sSectionKey)
  662. Public Property Get KeyExists( _
  663.                              ) As Boolean
  664.    
  665.     Dim HKEY        As Long
  666.    
  667.     If RegOpenKeyEx(m_hClassKey, m_sSectionKey, 0, 1, HKEY) = ERROR_SUCCESS Then
  668.         KeyExists = True
  669.         RegCloseKey HKEY
  670.     Else
  671.         KeyExists = False
  672.     End If
  673.    
  674. End Property
  675.  
  676. Private Sub pSetClassValue(ByVal sValue As String)
  677.    
  678.     Dim sSection        As String
  679.    
  680.     ClassKey = HKEY_CLASSES_ROOT
  681.     Value = sValue
  682.     sSection = SectionKey
  683.     ClassKey = HKEY_LOCAL_MACHINE
  684.     SectionKey = "SOFTWARE\Classes\" & sSection
  685.     Value = sValue
  686.     SectionKey = sSection
  687.    
  688. End Sub
  689.  
  690. Public Property Get SectionKey( _
  691.                               ) As String
  692.    
  693.     SectionKey = m_sSectionKey
  694.    
  695. End Property
  696.  
  697. Public Property Let SectionKey(ByVal sSectionKey As String)
  698.    
  699.     m_sSectionKey = sSectionKey
  700.    
  701. End Property
  702.  
  703. Private Function SwapEndian(ByVal dw As Long _
  704.                            ) As Long
  705.    
  706.     CopyMemory ByVal VarPtr(SwapEndian) + 3, dw, 1
  707.     CopyMemory ByVal VarPtr(SwapEndian) + 2, ByVal VarPtr(dw) + 1, 1
  708.     CopyMemory ByVal VarPtr(SwapEndian) + 1, ByVal VarPtr(dw) + 2, 1
  709.     CopyMemory SwapEndian, ByVal VarPtr(dw) + 3, 1
  710.    
  711. End Function
  712.  
  713. Public Property Get Value( _
  714.                          ) As Variant
  715.    
  716.     Dim vValue          As Variant
  717.     Dim cData           As Long
  718.     Dim sData           As String
  719.     Dim ordType         As Long
  720.     Dim e               As Long
  721.     Dim HKEY            As Long
  722.     Dim iData           As Long
  723.     Dim dwData          As Long
  724.     Dim abData()        As Byte
  725.    
  726.     e = RegOpenKeyEx(m_hClassKey, m_sSectionKey, 0, KEY_QUERY_VALUE, HKEY)
  727.     'Debug.Print "e = RegOpenKeyEx(" & m_hClassKey & "," & m_sSectionKey & ", 0, KEY_QUERY_VALUE, HKEY)"
  728.    'ApiRaiseIf e
  729.    
  730.     e = RegQueryValueExLong(HKEY, m_sValueKey, 0&, ordType, 0&, cData)
  731.    
  732.     If e And e <> ERROR_MORE_DATA Then
  733.         Value = m_vDefault
  734.         Exit Property
  735.     End If
  736.    
  737.     m_eValueType = ordType
  738.    
  739.     Select Case ordType
  740.         Case REG_DWORD, REG_DWORD_LITTLE_ENDIAN
  741.             e = RegQueryValueExLong(HKEY, m_sValueKey, 0&, ordType, iData, cData)
  742.             vValue = CLng(iData)
  743.        
  744.         Case REG_DWORD_BIG_ENDIAN  ' Unlikely, but you never know
  745.            e = RegQueryValueExLong(HKEY, m_sValueKey, 0&, ordType, dwData, cData)
  746.             vValue = SwapEndian(dwData)
  747.        
  748.         Case REG_SZ, REG_MULTI_SZ ' Same thing to Visual Basic
  749.            sData = String$(cData - 1, 0)
  750.             e = RegQueryValueExStr(HKEY, m_sValueKey, 0&, ordType, sData, cData)
  751.             vValue = sData
  752.        
  753.         Case REG_EXPAND_SZ
  754.             sData = String$(cData - 1, 0)
  755.             e = RegQueryValueExStr(HKEY, m_sValueKey, 0&, ordType, sData, cData)
  756.             vValue = ExpandEnvStr(sData)
  757.        
  758.         ' Catch REG_BINARY and anything else
  759.        Case Else
  760.             ReDim abData(cData)
  761.             e = RegQueryValueExByte(HKEY, m_sValueKey, 0&, ordType, abData(0), cData)
  762.             vValue = abData
  763.        
  764.     End Select
  765.    
  766.     Value = vValue
  767.    
  768. End Property
  769.  
  770. Public Property Let Value(ByVal vValue As Variant)
  771.    
  772.     Dim ordType         As Long
  773.     Dim c               As Long
  774.     Dim HKEY            As Long
  775.     Dim e               As Long
  776.     Dim lCreate         As Long
  777.     Dim tSA             As SECURITY_ATTRIBUTES
  778.     Dim ab()            As Byte
  779.     Dim i               As Long
  780.     Dim s               As String
  781.     Dim iPos            As Long
  782.    
  783.     'Open or Create the key
  784.    e = RegCreateKeyEx(m_hClassKey, m_sSectionKey, 0, "", REG_OPTION_NON_VOLATILE, _
  785.     KEY_ALL_ACCESS, tSA, HKEY, lCreate)
  786.    
  787.     If e Then
  788.         Err.Raise 26001, _
  789.                   App.EXEName & ".clsRegistry", _
  790.                   "Failed to set registry value Key: '" & m_hClassKey & _
  791.                   "',Section: '" & m_sSectionKey & _
  792.                   "',Key: '" & m_sValueKey & _
  793.                   "' to value: '" & m_vValue & "'"
  794.     Else
  795.        
  796.         Select Case m_eValueType
  797.             Case REG_BINARY
  798.                 If (VarType(vValue) = vbArray + vbByte) Then
  799.                     ab = vValue
  800.                     ordType = REG_BINARY
  801.                     c = UBound(ab) - LBound(ab) - 1
  802.                     e = RegSetValueExByte(HKEY, m_sValueKey, 0&, ordType, ab(0), c)
  803.                 Else
  804.                     Err.Raise 26001
  805.                 End If
  806.            
  807.             Case REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_DWORD_LITTLE_ENDIAN
  808.                 If (VarType(vValue) = vbInteger) Or (VarType(vValue) = vbLong) Then
  809.                     i = vValue
  810.                     ordType = REG_DWORD
  811.                     e = RegSetValueExLong(HKEY, m_sValueKey, 0&, ordType, i, 4)
  812.                 End If
  813.            
  814.             Case REG_SZ, REG_EXPAND_SZ
  815.                 s = vValue
  816.                 ordType = REG_SZ
  817.                 ' Assume anything with two non-adjacent percents is expanded string
  818.                iPos = InStr(s, "%")
  819.                 If iPos Then If InStr(iPos + 2, s, "%") Then ordType = REG_EXPAND_SZ
  820.                 c = Len(s) + 1
  821.                 e = RegSetValueExStr(HKEY, m_sValueKey, 0&, ordType, s, c)
  822.            
  823.             ' User should convert to a compatible type before calling
  824.            Case Else
  825.                 e = ERROR_INVALID_DATA
  826.            
  827.         End Select
  828.        
  829.         If Not e Then
  830.             m_vValue = vValue
  831.         Else
  832.             Err.Raise vbObjectError + 1048 + 26001, _
  833.                      App.EXEName & ".clsRegistry", _
  834.                      "Failed to set registry value Key: '" & m_hClassKey & _
  835.                      "',Section: '" & m_sSectionKey & _
  836.                      "',Key: '" & m_sValueKey & _
  837.                      "' to value: '" & m_vValue & "'"
  838.         End If
  839.        
  840.         'Close the key
  841.        RegCloseKey HKEY
  842.        
  843.     End If
  844.    
  845. End Property
  846.  
  847. Public Property Get ValueKey( _
  848.                             ) As String
  849.    
  850.     ValueKey = m_sValueKey
  851.    
  852. End Property
  853.  
  854. Public Property Let ValueKey(ByVal sValueKey As String)
  855.    
  856.     m_sValueKey = sValueKey
  857.    
  858. End Property
  859.  
  860. Public Property Get ValueType( _
  861.                              ) As ERegistryValueTypes
  862.    
  863.     ValueType = m_eValueType
  864.    
  865. End Property
  866.  
  867. Public Property Let ValueType(ByVal eValueType As ERegistryValueTypes)
  868.    
  869.     m_eValueType = eValueType
  870.    
  871. End Property
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement