home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD103621022000.psc / REG_API.BAS < prev    next >
Encoding:
BASIC Source File  |  2000-08-10  |  21.2 KB  |  591 lines

  1. Attribute VB_Name = "Registry_API_Declares"
  2. ' API declarations and simple wrapper functions for the Registry
  3. '
  4. '
  5. '
  6. ' JET 2 engine required, no known JET 3 features used
  7. '
  8. '
  9. '   Modified:
  10. ' AD  4/ 9/95   Initial version
  11. '
  12.  
  13. Option Explicit
  14.  
  15.  
  16.  
  17.  
  18. #If Win16 Then
  19. '  ERROR - This code does not work under 16 bit Windows
  20.   ' The Registry does exist from W3.1 onwards,
  21.   ' but only for use with OLE registration. This
  22.   ' code does not attempt to support these functions
  23. #End If
  24.  
  25. #If Win32 Then
  26. #Else
  27. '  ERROR - This code requires 32 bit Windows
  28. #End If
  29.  
  30.  
  31. ' NB - We can't nest a #Const within a conditional compile block
  32. ' #If Win32 Then
  33.   ' Define this true to use '95 style enhanced functions with access control (RegOpenKeyEx etc.)
  34.   ' False uses the 32 bit version of the 16 bit functions
  35.   #Const UseRegExFunctions = True
  36. ' #Else
  37.   ' For 16 bits, these functions will simply be unavailable
  38. '  #Const UseRegExFunctions = False
  39. ' #End If
  40.  
  41.  
  42.  
  43.  
  44.  
  45. 'Registration functions
  46. #If Win16 Then
  47. ' Old 16 bit functions
  48. Public Declare Function RegCloseKey Lib "SHELL.DLL" (ByVal hKey As Long) As Long
  49. Public Declare Function RegCreateKey Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String, lphKey As Long) As Long
  50. Public Declare Function RegDeleteKey Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String) As Long
  51. Public Declare Function RegEnumKey Lib "SHELL.DLL" (ByVal hKey As Long, ByVal iSubKey As Long, ByVal lpszBuffer As String, ByVal cbBuffer As Long) As Long
  52. Public Declare Function RegOpenKey Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String, lphkResult As Long) As Long
  53. Public Declare Function RegQueryValue Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String, ByVal lpszValue As String, lpcbValue As Long) As Long
  54. Public Declare Function RegSetValue Lib "SHELL.DLL" (ByVal hKey As Long, ByVal lpszSubKey As String, ByVal fdwType As Long, ByVal lpszValue As String, ByVal dwLength As Long) As Long
  55. #End If
  56.  
  57.  
  58. #If Win32 Then
  59. ' 32 bit declares for Win32s
  60.  
  61. ' NB - Don't use the supplied WIN32API.TXT instead. It has bugs in the translation
  62. ' of its function headers from C to VB.
  63. ' RegEnumValue & RegSetValue (et al) have lpData declared "lpData As Byte", when it needs to be "lpData As Any"
  64. ' if we are to manipulate pointers to arrays of bytes from VB
  65.  
  66.  
  67. ' These declares will be qualified as "Private" as each new wrapper function is implemented
  68. ' Exception - we won't wrap these two, as their headers are trivially compatible with VB
  69. Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  70. Public Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  71.  
  72. Private Declare Function RegCreateKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  73. '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, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
  74. Private Declare Function RegDeleteKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  75. Private Declare Function RegDeleteValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String) As Long
  76. Private Declare Function RegEnumKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
  77. 'Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
  78. Private Declare Function RegEnumValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
  79. 'Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
  80. Private Declare Function RegOpenKeyA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  81. Private Declare Function RegOpenKeyExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
  82. 'Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, lpcbClass As Long, 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 FILETIME) As Long
  83. Private Declare Function RegQueryValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long
  84. Private Declare Function RegQueryValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
  85. Private Declare Function RegSetValueA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
  86. Private Declare Function RegSetValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
  87.  
  88.  
  89. Public Declare Function GetEnvironmentStrings Lib "Kernel32" Alias "GetEnvironmentStringsA" () As String
  90. Public Declare Function GetEnvironmentVariable Lib "Kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
  91. Public Declare Function SetEnvironmentVariable Lib "Kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
  92.  
  93. Public Declare Function ExpandEnvironmentStrings Lib "Kernel32" Alias "ExpandEnvironmentStringsA" (ByVal lpSrc As String, ByVal lpDst As String, ByVal nDstSize As Long) As Long
  94. #End If
  95.  
  96.  
  97.  
  98. ' Return codes from Registration functions
  99. Public Const ERROR_SUCCESS = 0&
  100.  
  101. #If Win16 Then
  102. ' These constants are taken from 16-bit sources
  103. ' They appear to be incompatible with the 32-bit constants
  104. Public Const ERROR_BADDB = 1&
  105. Public Const ERROR_BADKEY = 2&
  106. Public Const ERROR_CANTOPEN = 3&
  107. Public Const ERROR_CANTREAD = 4&
  108. Public Const ERROR_CANTWRITE = 5&
  109. #End If
  110.  
  111. Public Const ERROR_OUTOFMEMORY = 6&
  112. Public Const ERROR_INVALID_PARAMETER = 7&
  113. Public Const ERROR_ACCESS_DENIED = 8&
  114.  
  115.  
  116.  
  117.  
  118. #If Win32 Then
  119. '   More data is available.
  120. Public Const ERROR_MORE_DATA = 234 '  dderror
  121.  
  122.  
  123. '   The configuration registry database is corrupt.
  124. Public Const ERROR_BADDB = 1009&
  125.  
  126. '   The configuration registry key is invalid.
  127. Public Const ERROR_BADKEY = 1010&
  128.  
  129. '   The configuration registry key could not be opened.
  130. Public Const ERROR_CANTOPEN = 1011&
  131.  
  132. '   The configuration registry key could not be read.
  133. Public Const ERROR_CANTREAD = 1012&
  134.  
  135. '   The configuration registry key could not be written.
  136. Public Const ERROR_CANTWRITE = 1013&
  137.  
  138. '   One of the files in the Registry database had to be recovered
  139. '   by use of a log or alternate copy.  The recovery was successful.
  140. Public Const ERROR_REGISTRY_RECOVERED = 1014&
  141.  
  142. '   The Registry is corrupt. The structure of one of the files that contains
  143. '   Registry data is corrupt, or the system's image of the file in memory
  144. '   is corrupt, or the file could not be recovered because the alternate
  145. '   copy or log was absent or corrupt.
  146. Public Const ERROR_REGISTRY_CORRUPT = 1015&
  147.  
  148. '   An I/O operation initiated by the Registry failed unrecoverably.
  149. '   The Registry could not read in, or write out, or flush, one of the files
  150. '   that contain the system's image of the Registry.
  151. Public Const ERROR_REGISTRY_IO_FAILED = 1016&
  152.  
  153. '   The system has attempted to load or restore a file into the Registry, but the
  154. '   specified file is not in a Registry file format.
  155. Public Const ERROR_NOT_REGISTRY_FILE = 1017&
  156.  
  157. '   Illegal operation attempted on a Registry key which has been marked for deletion.
  158. Public Const ERROR_KEY_DELETED = 1018&
  159.  
  160. '   System could not allocate the required space in a Registry log.
  161. Public Const ERROR_NO_LOG_SPACE = 1019&
  162.  
  163. '   Cannot create a symbolic link in a Registry key that already
  164. '   has subkeys or values.
  165. Public Const ERROR_KEY_HAS_CHILDREN = 1020&
  166.  
  167. '   Cannot create a stable subkey under a volatile parent key.
  168. Public Const ERROR_CHILD_MUST_BE_VOLATILE = 1021&
  169. #End If
  170.  
  171.  
  172.  
  173. #If Win32 Then
  174. '   These are the generic rights.
  175.  
  176. Public Const GENERIC_READ = &H80000000
  177. Public Const GENERIC_WRITE = &H40000000
  178. Public Const GENERIC_EXECUTE = &H20000000
  179. Public Const GENERIC_ALL = &H10000000
  180. #End If
  181.  
  182.  
  183.  
  184.  
  185.  
  186. 'Registration constants
  187.  
  188.  
  189. Public Const READ_CONTROL = &H20000
  190. Public Const SYNCHRONIZE = &H100000
  191.  
  192. Public Const STANDARD_RIGHTS_READ = READ_CONTROL
  193. Public Const STANDARD_RIGHTS_WRITE = READ_CONTROL
  194. Public Const STANDARD_RIGHTS_ALL = &H1F0000
  195.  
  196.  
  197. '//
  198. '// Registry Specific Access Rights.
  199. '//
  200.  
  201. Public Const KEY_QUERY_VALUE = &H1
  202. Public Const KEY_SET_VALUE = &H2
  203. Public Const KEY_CREATE_SUB_KEY = &H4
  204. Public Const KEY_ENUMERATE_SUB_KEYS = &H8
  205. Public Const KEY_NOTIFY = &H10
  206. Public Const KEY_CREATE_LINK = &H20
  207.  
  208. Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
  209.                                   KEY_QUERY_VALUE Or _
  210.                                   KEY_ENUMERATE_SUB_KEYS Or _
  211.                                   KEY_NOTIFY) _
  212.                                   And _
  213.                                  (Not SYNCHRONIZE))
  214.  
  215.  
  216. Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or _
  217.                                   KEY_SET_VALUE Or _
  218.                                   KEY_CREATE_SUB_KEY) _
  219.                                   And _
  220.                                  (Not SYNCHRONIZE))
  221.  
  222. Public Const KEY_EXECUTE = ((KEY_READ) _
  223.                                   And _
  224.                                  (Not SYNCHRONIZE))
  225.  
  226. Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or _
  227.                                   KEY_QUERY_VALUE Or _
  228.                                   KEY_SET_VALUE Or _
  229.                                   KEY_CREATE_SUB_KEY Or _
  230.                                   KEY_ENUMERATE_SUB_KEYS Or _
  231.                                   KEY_NOTIFY Or _
  232.                                   KEY_CREATE_LINK) _
  233.                                   And _
  234.                                  (Not SYNCHRONIZE))
  235.  
  236.  
  237.  
  238. Public Const HKEY_CLASSES_ROOT = &H80000000
  239. Public Const HKEY_CURRENT_USER = &H80000001
  240. Public Const HKEY_LOCAL_MACHINE = &H80000002
  241. Public Const HKEY_USERS = &H80000003
  242. Public Const HKEY_CURRENT_CONFIG = &H80000005
  243.  
  244. Public Const MAX_PATH = 256&
  245.  
  246.  
  247. '//
  248. '//
  249. '// Predefined Value Types.
  250. '//
  251.  
  252. Public Const REG_NONE = 0                     '// No value type
  253. Public Const REG_SZ = 1                       ' // Unicode nul terminated string
  254. Public Const REG_EXPAND_SZ = 2                ' // Unicode nul terminated string
  255.                                               ' // (with environment variable references)
  256. Public Const REG_BINARY = 3                   ' // Free form binary
  257. Public Const REG_DWORD_LITTLE_ENDIAN = 4
  258. Public Const REG_DWORD_BIG_ENDIAN = 5
  259. Public Const REG_DWORD = REG_DWORD_LITTLE_ENDIAN
  260. Public Const REG_LINK = 6                     ' // Symbolic Link (unicode)
  261. Public Const REG_MULTI_SZ = 7                 ' // Multiple Unicode strings
  262. Public Const REG_RESOURCE_LIST = 8            ' // Resource list in the resource map
  263. Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9 ' // Resource list in the hardware description
  264. Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10
  265.  
  266.  
  267.  
  268.  
  269. ' This value for "largest possible Registry buffer"
  270. ' is based on Microsoft's recommendation that any entries larger
  271. ' than this should be stored as files, with a UNC to them in the Registry
  272. Private Const mREG_BUFFER_SIZE = 2048
  273.  
  274.  
  275.  
  276.  
  277.  
  278. ' Reformat the Registry "Buffer O' Bytes" data type into something more VB friendly
  279. '
  280. ' Values are returned in pValue & pBuffer will be unchanged
  281. '
  282. ' NB - pBufferLen is the size of the buffer,
  283. ' and for strings it wil include the terminating nulls
  284. ' (this is different from how Windows returns the value names)
  285. '
  286. Private Sub MakeRegistryBufferIntoVB(pValueName As String, pBuffer As String, ByVal pBufferLen As Long, ByVal pType As Long, pValue As Variant)
  287.  
  288. Dim lHack As Long
  289.  
  290. Dim lLongValue As Long
  291. Dim lStringValue As String
  292. Dim llLength As Long
  293.  
  294.  
  295.     ' We put this in a variable, not a constant, because of problems with VB
  296.     ' Constants are implicitly typed as integers, and that would cause an overflow in this case
  297.     lHack = &H100
  298.     lLongValue = 0
  299.     pValue = 0
  300.  
  301.     
  302.     Select Case pType
  303.     Case REG_BINARY
  304.     
  305.     Case REG_DWORD, REG_DWORD_LITTLE_ENDIAN
  306.       lLongValue = (&H1000000 * (Asc(Mid$(pBuffer, 4, 1)) Mod &H80)) _
  307.         + (&H10000 * Asc(Mid$(pBuffer, 3, 1))) _
  308.         + (lHack * Asc(Mid$(pBuffer, 2, 1))) _
  309.         + (&H1 * Asc(Mid$(pBuffer, 1, 1)))
  310.       If ((Asc(Mid$(pBuffer, 4, 1)) And &H80) <> 0) Then lLongValue = -(lLongValue Xor &H7FFFFFFF)
  311.       pValue = lLongValue
  312.       
  313.     Case REG_DWORD_BIG_ENDIAN
  314.       lLongValue = (&H1000000 * (Asc(Mid$(pBuffer, 1, 1)) Mod &H80)) _
  315.         + (&H10000 * Asc(Mid$(pBuffer, 2, 1))) _
  316.         + (lHack * Asc(Mid$(pBuffer, 3, 1))) _
  317.         + (&H1 * Asc(Mid$(pBuffer, 4, 1)))
  318.       If ((Asc(Mid$(pBuffer, 1, 1)) And &H80) <> 0) Then lLongValue = -(lLongValue Xor &H7FFFFFFF)
  319.       pValue = lLongValue
  320.     
  321.     Case REG_LINK
  322.     
  323.     Case REG_MULTI_SZ
  324.       ' We probably want to return these as an array of strings, now that VB4 Variants support arrays of strings
  325.       lStringValue = left$(pBuffer, InStr(pBuffer, Chr$(0) & Chr$(0)) - 1)
  326.       pValue = lStringValue
  327.     
  328.     Case REG_RESOURCE_LIST
  329.     
  330.     Case REG_SZ
  331.       ' pBufferLen is the buffer length, not the string length (i.e. it includes the terminating null)
  332.       lStringValue = left$(pBuffer, pBufferLen - 1)
  333.       pValue = lStringValue
  334.       
  335.     Case REG_EXPAND_SZ
  336.       lStringValue = String$(mREG_BUFFER_SIZE, Chr$(0))
  337.       llLength = ExpandEnvironmentStrings(ByVal pBuffer, ByVal lStringValue, mREG_BUFFER_SIZE)
  338.       pValue = left$(lStringValue, llLength)
  339.     
  340.     Case Else ' REG_NONE
  341.     End Select
  342.     
  343. End Sub
  344.  
  345. ' Reformat a VB variant into a buffer for use with the Registry
  346. '
  347. ' Values are returned in pValue & pBuffer will be unchanged
  348. '
  349. '
  350. '
  351. ' If pType = REG_NONE (0) an automatic selection will be made
  352. '
  353. Private Sub MakeVBIntoRegistryBuffer(pValue As Variant, pType As Long, pBuffer As String, pBufferLen As Long)
  354.  
  355.   ' Sort out the type of data
  356.   If (pType = REG_NONE) Then
  357.     Select Case VarType(pValue)
  358.     Case vbInteger, vbLong, vbByte, vbBoolean
  359.       pType = REG_DWORD
  360.     Case vbString
  361.       pType = REG_SZ
  362.     Case Else ' Default to a string representation for the moment - maybe should be binary if we work out how to manipulate them
  363.       pType = REG_SZ
  364.     End Select
  365.   End If
  366.     
  367.   
  368.   Select Case pType
  369.   
  370.   Case REG_DWORD, REG_DWORD_LITTLE_ENDIAN
  371.     pBuffer = Chr$((CLng(pValue)) And &HFF) _
  372.       & Chr$((CLng(pValue) \ &H100) And &HFF) _
  373.       & Chr$((CLng(pValue) \ &H10000) And &HFF) _
  374.       & Chr$((CLng(pValue) \ &H1000000) And &HFF)
  375.     pBufferLen = 4
  376.     
  377.   Case REG_DWORD_BIG_ENDIAN
  378.     pBuffer = Chr$((CLng(pValue) \ &H1000000) And &HFF) _
  379.       & Chr$((CLng(pValue) \ &H10000) And &HFF) _
  380.       & Chr$((CLng(pValue) \ &H100) And &HFF) _
  381.       & Chr$((CLng(pValue)) And &HFF)
  382.     pBufferLen = 4
  383.  
  384.   Case REG_SZ, REG_EXPAND_SZ
  385.     Select Case VarType(pValue)
  386.     Case vbString
  387.       pBuffer = pValue & Chr$(0)
  388.     Case Else
  389.       pBuffer = Format$(pValue) & Chr$(0)
  390.     End Select
  391.     pBufferLen = Len(pBuffer)
  392.     
  393.   Case Else
  394.     ' Fail - we don't recognise this type
  395.     Error -1
  396.   End Select
  397.   
  398.   
  399. End Sub
  400.  
  401. ' Wrapper function for RegCreateKey
  402. '
  403. Public Function RegCreateKey(ByVal hKey As Long, lpSubKey As String, phkResult As Long) As Long
  404.  
  405.   RegCreateKey = RegCreateKeyA(hKey, ByVal lpSubKey, phkResult)
  406. End Function
  407.  
  408. ' Wrapper function for RegDeleteKey
  409. '
  410. Public Function RegDeleteKey(ByVal hKey As Long, lpSubKey As String) As Long
  411.  
  412.   RegDeleteKey = RegDeleteKeyA(hKey, ByVal lpSubKey)
  413. End Function
  414.  
  415. ' Wrapper function for RegDeleteValue
  416. '
  417. ' Unlike the API RegDeleteValue, this function supports the deletion
  418. ' of a value from a sub-key, lpSubKey
  419. '
  420. ' lpSubKey may otherwise be empty, in which case the value to be deleted
  421. ' should be a value of hKey itself
  422. '
  423. Public Function RegDeleteValue(ByVal hKey As Long, lpSubKey As String, lpValueName As String) As Long
  424.  
  425. Dim lRetVal As Long, lMinorRetVal As Long
  426. Dim lhKey As Long
  427.  
  428.  
  429.   lRetVal = ERROR_SUCCESS
  430.   ' Open any subkeys as necessary
  431.   If (Len(lpSubKey) = 0) Then
  432.     lhKey = hKey
  433.   Else
  434. #If UseRegExFunctions Then
  435.     lRetVal = RegOpenKey(hKey, lpSubKey, KEY_WRITE, lhKey)
  436. #Else
  437.     lRetVal = RegOpenKey(hKey, lpSubKey, lhKey)
  438. #End If
  439.   End If
  440.   
  441.   ' Delete the value
  442.   If (lRetVal = ERROR_SUCCESS) Then lRetVal = RegDeleteValueA(lhKey, ByVal lpValueName)
  443.   
  444.   ' Close the key
  445.   If (lRetVal = ERROR_SUCCESS) Then
  446.     lMinorRetVal = RegCloseKey(lhKey)
  447.     If (lRetVal = ERROR_SUCCESS) Then lRetVal = lMinorRetVal
  448.   End If
  449.   
  450.   RegDeleteValue = lRetVal
  451. End Function
  452.  
  453.  
  454. ' Wrapper function for RegEnumKey
  455. '
  456. Public Function RegEnumKey(ByVal hKey As Long, ByVal dwIndex As Long, lpName As String) As Long
  457.  
  458. Dim llRetVal As Long
  459. Dim lcbName As Long
  460.  
  461.  
  462.   lcbName = mREG_BUFFER_SIZE
  463.   lpName = String$(lcbName, Chr$(0))
  464.   llRetVal = RegEnumKeyA(hKey, dwIndex, ByVal lpName, lcbName)
  465.   ' lcbName is not set on return from RegEnumKeyA, so we must search for the end of the string ourselves
  466.   ' (see RegEnumValue)
  467.   lpName = left$(lpName, InStr(lpName, Chr$(0)) - 1)
  468.   
  469.   RegEnumKey = llRetVal
  470. End Function
  471.  
  472.  
  473.  
  474.  
  475. ' Wrapper function for RegEnumValue
  476. '
  477. ' Values are returned as a VB Variant
  478. '
  479. Public Function RegEnumValue(ByVal hKey As Long, ByVal dwIndex As Long, lpValueName As String, lpType As Long, pValue As Variant) As Long
  480.  
  481. Dim lpLongValue As Long
  482.  
  483. Dim lRetVal As Long
  484. Dim cbValueName As Long
  485. Dim lpValueBuffer As String
  486. Dim cbValue As Long
  487.  
  488. Dim lpReserved As Long
  489.  
  490.  
  491.   cbValueName = mREG_BUFFER_SIZE
  492.   lpValueName = String$(cbValueName, Chr$(0))
  493.   cbValue = mREG_BUFFER_SIZE
  494.   lpValueBuffer = String$(cbValue, Chr$(0))
  495.   
  496.   lpReserved = 0
  497.   lRetVal = RegEnumValueA(hKey, dwIndex, ByVal lpValueName, cbValueName, lpReserved, lpType, ByVal lpValueBuffer, cbValue)
  498.   If lRetVal = ERROR_SUCCESS Then
  499.     ' cbValueName is set to the length of the string on return,
  500.     ' cbValue is set to the length of the buffer
  501.     ' cbValue includes the terminating null, cbValueName doesn't
  502.     ' (See RegEnumKey for a 3rd way of passing these parameters !)
  503.     lpValueName = left$(lpValueName, cbValueName)
  504.     MakeRegistryBufferIntoVB lpValueName, lpValueBuffer, cbValue, lpType, pValue
  505.   End If
  506.   
  507.   RegEnumValue = lRetVal
  508. End Function
  509.  
  510.  
  511.  
  512. ' Wrapper function for RegOpenKey
  513. '
  514. ' When calling this under 32-bit code, pass KEY_READ or KEY_WRITE (or similar) constants
  515. '
  516. #If UseRegExFunctions Then
  517. Public Function RegOpenKey(ByVal hKey As Long, lpSubKey As String, ByVal pSAM As Long, phkResult As Long) As Long
  518. #Else
  519. Public Function RegOpenKey(ByVal hKey As Long, lpSubKey As String, phkResult As Long) As Long
  520. #End If
  521.  
  522.  
  523. #If UseRegExFunctions Then
  524. Dim lReserved As Long
  525.  
  526.   RegOpenKey = RegOpenKeyExA(hKey, ByVal lpSubKey, lReserved, pSAM, phkResult)
  527. #Else
  528.   RegOpenKey = RegOpenKeyA(hKey, ByVal lpSubKey, phkResult)
  529. #End If
  530. End Function
  531.  
  532.  
  533. ' Wrapper function for RegQueryValue
  534. '
  535. Public Function RegQueryValue(ByVal phKey As Long, pSubKey As String, pValue As Variant) As Long
  536.   
  537. Dim lRetVal As Long
  538. Dim lpReserved As Long
  539. Dim lpType As Long
  540. Dim lpData As String
  541. Dim lpcbValue As Long
  542.  
  543.  
  544.   lpReserved = 0
  545.   lpcbValue = mREG_BUFFER_SIZE
  546.   lpData = String$(lpcbValue, Chr$(0))
  547.   
  548. ' We can only use RegQueryValueEx if we used RegOpenKeyEx to open it
  549. #If UseRegExFunctions Then
  550.   lRetVal = RegQueryValueExA(phKey, ByVal pSubKey, lpReserved, lpType, ByVal lpData, lpcbValue)
  551. #Else
  552.   lRetVal = RegQueryValueA(phKey, ByVal pSubKey, ByVal lpData, lpcbValue)
  553.   lpType = REG_SZ ' 16 bit Registry only supports strings
  554. #End If
  555.  
  556.   If lRetVal = ERROR_SUCCESS Then
  557.     MakeRegistryBufferIntoVB pSubKey, lpData, lpcbValue, lpType, pValue
  558.   End If
  559.   RegQueryValue = lRetVal
  560. End Function
  561.  
  562.  
  563. ' Wrapper function for RegSetValue
  564. '
  565. ' phKey   - key to a hive
  566. ' pSubKey - string for the sub-key from phKey
  567. ' pType   - data type, REG_SZ etc. as defined in Reg_API.BAS
  568. '           Use pType = REG_NONE to give an automatic selection
  569. ' pData   - data to be stored
  570. '
  571. Public Function RegSetValue(ByVal phKey As Long, pSubKey As String, ByVal pType As Long, ByVal pData As Variant) As Long
  572.  
  573. Dim lBuffer As String
  574. Dim lBufferLen As Long
  575.  
  576.  
  577.   MakeVBIntoRegistryBuffer pData, pType, lBuffer, lBufferLen
  578.  
  579.  
  580. #If UseRegExFunctions Then
  581. Dim lReserved As Long
  582.  
  583.   lReserved = 0
  584.   RegSetValue = RegSetValueExA(phKey, ByVal pSubKey, lReserved, pType, ByVal lBuffer, lBufferLen)
  585. #Else
  586.   RegSetValue = RegSetValueA(phKey, ByVal pSubKey, pType, ByVal lBuffer, lBufferLen)
  587. #End If
  588. End Function
  589.  
  590.  
  591.