Hej alla, varför får jag detta att funka endast i win98? Omöjligt at svara vad som är fel när alla information saknas. Hej, då jag inte e så haj på reg funktioner så har jag en gång på 98:ans tid tagit denna kod från nätet och den funkar däri, har sedan inte brytt mig i det.RegEnumKeys
Har testat NT4 /2000 & xp men då blir "retval" 234 när den hittar en nyckel, inte 0 som den vill ha.
<code>
Public Function RegEnumKeys()
' Enumerate the subkeys under HKEY_LOCAL_MACHINE\Software. The name
' and class of each subkey is displayed for the user. Note the use of the loop which
' starts at 0 and keeps incrementing the index until no more subkeys exist.
Dim KeyName As String ' receives name of each subkey
Dim keylen As Long ' length of keyname
Dim classname As String ' receives class of each subkey
Dim classlen As Long ' length of classname
Dim lastwrite As FILETIME ' receives last-write-to time, but we ignore it here
Dim index As Long ' counter variable for index
Dim retval As Long ' function's return value
' Open the desired registry key. Note the access level requested.
retval = RegOpenKeyEx(hKey, SubKey, 0, KEY_ENUMERATE_SUB_KEYS, hKey)
' Test to make sure the key was opened successfully.
If retval <> 0 Then
'error kod
End If
' List through each possible subkey. Note how the strings receiving the information
' must be reinitialized each loop iteration.
index = 0 ' initial index value
While retval = 0 ' while we keep having success (retval equals 0 from the above API call)
KeyName = Space(255): classname = Space(255) ' make room in string buffers
keylen = 255: classname = 255 ' identify the allocated space
' Get information about the next subkey, if one exists.
retval = RegEnumKeyEx(hKey, index, KeyName, keylen, ByVal 0, classname, classlen, lastwrite)
If retval = 0 Then ' only display info if another subkey was found 'HÄR BLIR DET FEL
' Extract the useful information from the string buffers.
KeyName = Left(KeyName, keylen) ' trim off the excess space
classname = Left(classname, classlen)
' Display the returned information.
'****************************************
OutPut = KeyName
'****************************************
End If
index = index + 1 ' increment the index counter
Wend ' end the loop
' Close the registry key after enumeration is complete.
retval = RegCloseKey(hKey)
End Function
</code>Sv: RegEnumKeys
Gissar på att det kan vara fel i hKey eller SubKey. Men vet inte var det är deklarerade eller vilka dess värde är.
Blir inte bättre av att du skriver över hKey vid anropet till RegOpenKeyEx.
Sedan är frågan vad Output gör för något. Är det en egenskap eller en variabel? Vad händer då om du vill ha ut flera värden?
Jag använder win2000. Och följande fungerar hoss mig:
<code>
Option Explicit
Private Const ERROR_SUCCESS As Long = 0&
Private Const ERROR_NO_MORE_ITEMS As Long = 259&
Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
Public Enum EnumHKEY
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
HKEY_PERFORMANCE_DATA = &H80000004
HKEY_CURRENT_CONFIG = &H80000005
HKEY_DYN_DATA = &H80000006
End Enum
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long
Public Sub RegEnumKeys(Root As EnumHKEY, SubKey As String)
Dim hKey As Long
Dim Index As Long
Dim lReturn As Long
Dim KeyLen As Long
Dim KeyName As String
Dim ClassLen As Long
Dim ClassName As String
Dim LastWrite As FILETIME
Dim strResult As String
Const BUFFER_SIZE As Long = 255
lReturn = RegOpenKeyEx(Root, SubKey, 0, KEY_ENUMERATE_SUB_KEYS, hKey)
If lReturn = ERROR_SUCCESS Then
Do
KeyLen = BUFFER_SIZE
KeyName = String(KeyLen, 0)
ClassLen = BUFFER_SIZE
ClassName = String(ClassLen, 0)
lReturn = RegEnumKeyEx(hKey, Index, KeyName, KeyLen, ByVal 0&, ClassName, ClassLen, LastWrite)
If lReturn = ERROR_SUCCESS Then
KeyName = Left$(KeyName, KeyLen)
ClassName = Left$(ClassName, ClassLen)
Debug.Print "KeyName: " & KeyName & ", ClassName: " & ClassName
ElseIf lReturn = ERROR_NO_MORE_ITEMS Then
Exit Do
Else
Err.Raise vbObjectError + 2, "RegEnumKeys.RegEnumKeyEx", "Unknown result: " & lReturn
End If
Index = Index + 1
Loop
RegCloseKey hKey
Else
Err.Raise vbObjectError + 1, "RegEnumKeys.RegOpenKeyEx", "Unable to open Key"
End If
End Sub
Private Sub Form_Load()
RegEnumKeys HKEY_LOCAL_MACHINE, "Software"
End Sub
</code>Sv: RegEnumKeys
Output är en public variabel.
Men jag ska kolla din grej, funkar säkert bra:-)