home *** CD-ROM | disk | FTP | other *** search
/ PC Open 15 / PCOPEN15.ISO / Optix / DATA1.CAB / Script_Source / Registry.bas < prev    next >
Encoding:
BASIC Source File  |  1997-11-26  |  4.3 KB  |  131 lines

  1. Attribute VB_Name = "Registry"
  2. Option Explicit
  3.  
  4. Global Const REG_SZ As Long = 1
  5. Global Const REG_DWORD As Long = 4
  6. Global Const HKEY_CLASSES_ROOT = &H80000000
  7. Global Const HKEY_CURRENT_USER = &H80000001
  8. Global Const HKEY_LOCAL_MACHINE = &H80000002
  9. Global Const HKEY_USERS = &H80000003
  10. Global Const ERROR_NONE = 0
  11. Global Const ERROR_BADDB = 1
  12. Global Const ERROR_BADKEY = 2
  13. Global Const ERROR_CANTOPEN = 3
  14. Global Const ERROR_CANTREAD = 4
  15. Global Const ERROR_CANTWRITE = 5
  16. Global Const ERROR_OUTOFMEMORY = 6
  17. Global Const ERROR_INVALID_PARAMETER = 7
  18. Global Const ERROR_ACCESS_DENIED = 8
  19. Global Const ERROR_INVALID_PARAMETERS = 87
  20. Global Const ERROR_NO_MORE_ITEMS = 259
  21. Global Const KEY_ALL_ACCESS = &H3F
  22. Global Const REG_OPTION_NON_VOLATILE = 0
  23. Global Const SW_SHOWNORMAL = 1
  24.  
  25. Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  26.  
  27. Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias _
  28. "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
  29. ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _
  30. As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _
  31. As Long, phkResult As Long, lpdwDisposition As Long) As Long
  32.  
  33. Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
  34. "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
  35. ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
  36. Long) As Long
  37.  
  38. Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
  39. "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
  40. String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
  41. As String, lpcbData As Long) As Long
  42.  
  43. Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
  44. "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
  45. String, ByVal lpReserved As Long, lpType As Long, lpData As _
  46. Long, lpcbData As Long) As Long
  47.  
  48. Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
  49. "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
  50. String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
  51. As Long, lpcbData As Long) As Long
  52.  
  53. Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
  54. "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
  55. ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _
  56. String, ByVal cbData As Long) As Long
  57.  
  58. Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
  59. "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
  60. ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _
  61. ByVal cbData As Long) As Long
  62.  
  63. Public Function SetValueEx(ByVal hKey As Long, sValueName As String, _
  64.     lType As Long, vValue As Variant) As Long
  65.  
  66.     Dim lValue As Long
  67.     Dim sValue As String
  68.     Select Case lType
  69.         Case REG_SZ
  70.             sValue = vValue & Chr$(0)
  71.             SetValueEx = RegSetValueExString(hKey, sValueName, 0&, _
  72.                                            lType, sValue, Len(sValue))
  73.         Case REG_DWORD
  74.             lValue = vValue
  75.             SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, _
  76.                 lType, lValue, 4)
  77.         End Select
  78.  
  79. End Function
  80.  
  81.  
  82. Public Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As _
  83.     String, vValue As Variant) As Long
  84.     
  85.     Dim cch As Long
  86.     Dim lrc As Long
  87.     Dim lType As Long
  88.     Dim lValue As Long
  89.     Dim sValue As String
  90.  
  91.     On Error GoTo QueryValueExError
  92.  
  93.     ' Determine the size and type of data to be read
  94.     lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
  95.     If lrc <> ERROR_NONE Then Error 5
  96.  
  97.     Select Case lType
  98.         ' For strings
  99.         Case REG_SZ:
  100.             sValue = String(cch, 0)
  101.  
  102.             lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
  103.             If lrc = ERROR_NONE Then
  104.                 vValue = Left$(sValue, cch)
  105.             Else
  106.                 vValue = Empty
  107.             End If
  108.         ' For DWORDS
  109.         
  110.         Case REG_DWORD:
  111.  
  112.  
  113.             lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
  114.             If lrc = ERROR_NONE Then vValue = lValue
  115.             
  116.         Case Else
  117.             'all other data types not supported
  118.             lrc = -1
  119.     End Select
  120.  
  121. QueryValueExExit:
  122.     QueryValueEx = lrc
  123.     Exit Function
  124.  
  125. QueryValueExError:
  126.     Resume QueryValueExExit
  127.  
  128. End Function
  129.  
  130.  
  131.