home *** CD-ROM | disk | FTP | other *** search
-
- Private Function GetSubkeyHandle(sSubkey As String) As Long
-
- Dim intSubkeyCount As Integer
- Dim intSubkeyLoop As Integer
- Dim intSubkeyPtr As Integer
- Dim lonHandle(1 To 32) As Long
- Dim lonLastHandle As Long
- Dim lonStatus As Long
- Dim strKeyPath As String
- Dim strSubkeys(1 To 32) As String
- Dim strTempKeys() As String
-
- ' Trim any spaces from left and right sides of the
- ' subkey path.
- strKeyPath = Trim$(sSubkey)
-
- ' Use VB's Split function to break the key's path
- ' into subkeys.
- strTempKeys = Split(strKeyPath, "\")
-
- ' Transfer the elements of the strTempKeys()
- ' array to strSubkeys(), but leave out any blank
- ' elements.
- For intSubkeyLoop = 0 To UBound(strTempKeys)
- If strTempKeys(intSubkeyLoop) <> "" Then
- intSubkeyCount = intSubkeyCount + 1
- If intSubkeyCount <= 32 Then
- strSubkeys(intSubkeyCount) = strTempKeys(intSubkeyLoop)
- Else
- ' Error - too many subkeys in path.
- GetSubkeyHandle = -1
- Exit Function
- End If
- End If
- Next intSubkeyLoop
-
- For intSubkeyPtr = 1 To intSubkeyCount - 1
- ' For the first subkey, use the defined
- ' constants - this is the root key.
- If intSubkeyPtr = 1 Then
- Select Case UCase$(strSubkeys(1))
- Case "HKEY_CLASSES_ROOT":
- lonLastHandle = &H80000000
- Case "HKEY_CURRENT_USER":
- lonLastHandle = &H80000001
- Case "HKEY_LOCAL_MACHINE"
- lonLastHandle = &H80000002
- Case "HKEY_USERS":
- lonLastHandle = &H80000003
- Case "HKEY_PERFORMANCE_DATA":
- lonLastHandle = &H80000004
- Case "HKEY_CURRENT_CONFIG":
- lonLastHandle = &H80000005
- Case Else:
- GetSubkeyHandle = -1
- Exit Function
- End Select
- End If
- ' Open the next subkey.
- lonStatus = RegOpenKeyEx(lonLastHandle, _
- strSubkeys(intSubkeyPtr + 1), 0, &H3F, _
- _ lonHandle(intSubkeyPtr))
- ' A non-zero value returned by the
- ' RegOpenKey function indicates an error.
- If lonStatus Then
- ' Error in path. Close any previously
- ' opened subkeys.
- For intSubkeyLoop = 2 To (intSubkeyPtr - 1)
- lonStatus = RegCloseKey(lonHandle(intSubkeyLoop))
- Next intSubkeyLoop
- GetSubkeyHandle = -1
- Exit Function
- End If
- lonLastHandle = lonHandle(intSubkeyPtr)
- Next intSubkeyPtr
-
- ' Return the value of the last subkey handle.
- GetSubkeyHandle = lonLastHandle
-
- ' Close all subkeys but the last one.
- For intSubkeyLoop = 1 To (intSubkeyCount - 2)
- lonStatus = RegCloseKey(lonHandle(intSubkeyLoop))
- Next intSubkeyLoop
-
- End Function
-