home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 6 Unleashed…sional Reference Edition) / Visual_Basic_6_Unleashed_Professional_Reference_Edition_Sams_1999.iso / Source / CHAP29 / 309X3213.TXT < prev    next >
Encoding:
Text File  |  1998-05-05  |  2.7 KB  |  87 lines

  1.  
  2. Private Function GetSubkeyHandle(sSubkey As String) As Long
  3.  
  4. Dim intSubkeyCount As Integer
  5. Dim intSubkeyLoop As Integer
  6. Dim intSubkeyPtr As Integer
  7. Dim lonHandle(1 To 32) As Long
  8. Dim lonLastHandle As Long
  9. Dim lonStatus As Long
  10. Dim strKeyPath As String
  11. Dim strSubkeys(1 To 32) As String
  12. Dim strTempKeys() As String
  13.  
  14. ' Trim any spaces from left and right sides of the
  15. ' subkey path.
  16. strKeyPath = Trim$(sSubkey)
  17.  
  18. ' Use VB's Split function to break the key's path
  19. ' into subkeys.
  20. strTempKeys = Split(strKeyPath, "\")
  21.  
  22. ' Transfer the elements of the strTempKeys()
  23. ' array to strSubkeys(), but leave out any blank
  24. ' elements.
  25. For intSubkeyLoop = 0 To UBound(strTempKeys)
  26.     If strTempKeys(intSubkeyLoop) <> "" Then
  27.         intSubkeyCount = intSubkeyCount + 1
  28.         If intSubkeyCount <= 32 Then
  29.             strSubkeys(intSubkeyCount) = strTempKeys(intSubkeyLoop)
  30.         Else
  31.             ' Error - too many subkeys in path.
  32.             GetSubkeyHandle = -1
  33.             Exit Function
  34.         End If
  35.     End If
  36. Next intSubkeyLoop
  37.  
  38. For intSubkeyPtr = 1 To intSubkeyCount - 1
  39.     ' For the first subkey, use the defined
  40.     ' constants - this is the root key.
  41.     If intSubkeyPtr = 1 Then
  42.         Select Case UCase$(strSubkeys(1))
  43.             Case "HKEY_CLASSES_ROOT":
  44.                 lonLastHandle = &H80000000
  45.             Case "HKEY_CURRENT_USER":
  46.                 lonLastHandle = &H80000001
  47.             Case "HKEY_LOCAL_MACHINE"
  48.                 lonLastHandle = &H80000002
  49.             Case "HKEY_USERS":
  50.                 lonLastHandle = &H80000003
  51.             Case "HKEY_PERFORMANCE_DATA":
  52.                 lonLastHandle = &H80000004
  53.             Case "HKEY_CURRENT_CONFIG":
  54.                 lonLastHandle = &H80000005
  55.             Case Else:
  56.                 GetSubkeyHandle = -1
  57.                 Exit Function
  58.         End Select
  59.     End If
  60.     ' Open the next subkey.
  61.     lonStatus = RegOpenKeyEx(lonLastHandle, _
  62.         strSubkeys(intSubkeyPtr + 1), 0, &H3F, _
  63.  _      lonHandle(intSubkeyPtr))
  64.     ' A non-zero value returned by the
  65.     ' RegOpenKey function indicates an error.
  66.     If lonStatus Then
  67.         ' Error in path. Close any previously
  68.         ' opened subkeys.
  69.         For intSubkeyLoop = 2 To (intSubkeyPtr - 1)
  70.             lonStatus = RegCloseKey(lonHandle(intSubkeyLoop))
  71.         Next intSubkeyLoop
  72.         GetSubkeyHandle = -1
  73.         Exit Function
  74.     End If
  75.     lonLastHandle = lonHandle(intSubkeyPtr)
  76. Next intSubkeyPtr
  77.  
  78. ' Return the value of the last subkey handle.
  79. GetSubkeyHandle = lonLastHandle
  80.  
  81. ' Close all subkeys but the last one.
  82. For intSubkeyLoop = 1 To (intSubkeyCount - 2)
  83.     lonStatus = RegCloseKey(lonHandle(intSubkeyLoop))
  84. Next intSubkeyLoop
  85.        
  86. End Function
  87.